Bug 36433 - Linq range variable incorrect value
Summary: Linq range variable incorrect value
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: JIT ()
Version: 4.2.0 (C6)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2015-12-01 16:43 UTC by greg
Modified: 2015-12-07 21:30 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description greg 2015-12-01 16:43:59 UTC
The following code produces accurate results with all MS parts on windows but produces incorrect results with mono on linux. The variable in question is the binsize. The output from the query has binsize correct but when multiplied with a.Index the result is incorrect. Compiled on linux with mcs:

Mono JIT compiler version 4.0.2 (Stable 4.0.2.5/c99aa0c Wed Jun 24 10:04:37 UTC 2015)


using System;
using System.Linq;
using System.Collections.Generic;

namespace testbug {
class testrangevar {
static void Main(string[] args)
{
        var partitions = new List<double[]>();
        var r = new Random();
        Func<double[]> GetRandomDoubles = () => Enumerable.Range(0, 100).Select(s => r.NextDouble()).ToArray();
        var buckets = 8;

        partitions.Add(GetRandomDoubles());
        partitions.Add(GetRandomDoubles());
        partitions.Add(GetRandomDoubles());

        var partitionedSet =
                 from part in partitions
                 let minimum = part.Min()
                 let maximum = part.Max()
                 select new
                 {
                         Min = minimum,
                         Max = maximum,
                         Bins = Bucketize(part.ToList(), buckets).Select((sb, index) => new { Bin = sb, Index = index })
                 };
        var x =
                (from pItem in partitionedSet
                let binsize = Math.Round((pItem.Max - pItem.Min) / buckets, 3)
                let binentries = pItem.Bins.Select(a => $"{a.Bin,5} ({(binsize * a.Index):F2})")
                select $"{binsize:F3}" + string.Join(" ", binentries) + Environment.NewLine).Aggregate((a, b) => a + b);

        Console.WriteLine(x);

}

public enum BucketizeDirectionEnum
{
        LowerBoundInclusive,
        UpperBoundInclusive
}
// Define other methods and classes here
static public int[] Bucketize(IList<double> source, int totalBuckets)
{
        BucketizeDirectionEnum inclusivity = BucketizeDirectionEnum.LowerBoundInclusive;
        var min = source.Min();
        var max = source.Max();
        var buckets = new int[totalBuckets];
        var bucketSize = (max - min) / totalBuckets;
        if (max == min)
                bucketSize = 1;
        if (inclusivity == BucketizeDirectionEnum.LowerBoundInclusive)
        {
                foreach (var value in source)
                {
                        int bucketIndex = (int)((value - min) / bucketSize);
                        if (bucketIndex == totalBuckets)
                                continue;
                        buckets[bucketIndex]++;
                }
        }
        else
        {
                foreach (var value in source)
                {
                        int bucketIndex = (int)Math.Ceiling((value - min) / bucketSize) - 1;
                        if (bucketIndex < 0)
                                continue;
                        buckets[bucketIndex]++;
                }
        }

        return buckets;
}
}
}

Output on windows with Linqpad:

0.122    8 (0.00)    15 (0.12)     9 (0.24)    12 (0.37)    12 (0.49)    11 (0.61)    16 (0.73)    16 (0.85)
0.122   11 (0.00)     7 (0.12)    13 (0.24)    19 (0.37)    15 (0.49)    10 (0.61)    14 (0.73)    10 (0.85)
0.123   16 (0.00)    16 (0.12)     7 (0.25)     6 (0.37)    11 (0.49)    12 (0.62)    15 (0.74)    16 (0.86)

Output on Linux with mono:
0.121   10 (0.00)    10 (0.00)    13 (0.00)    19 (0.00)    18 (0.00)    12 (0.00)     9 (0.00)     8 (0.00)
0.123   13 (0.00)    10 (0.00)    10 (0.00)    19 (0.00)    12 (0.00)    13 (0.00)    13 (0.00)     9 (0.00)
0.124   15 (0.00)    10 (0.00)    14 (0.00)    13 (0.00)    11 (0.00)    13 (0.00)    11 (0.00)    12 (0.00)
Comment 1 João Matos 2015-12-07 19:51:41 UTC
I have tested this on both Mono 4.2 and master on OSX.

0.122   14 (0.00)    12 (0.12)    12 (0.24)     7 (0.37)     9 (0.49)     9 (0.61)    17 (0.73)    19 (0.85)
0.121   13 (0.00)    13 (0.12)    17 (0.24)     9 (0.36)    15 (0.48)    13 (0.61)     9 (0.73)    10 (0.85)
0.121    9 (0.00)    11 (0.12)    10 (0.24)    11 (0.36)    17 (0.48)    10 (0.61)    14 (0.73)    17 (0.85)


Could you please test with a more recent Mono version? Since this bug is in a new compiler feature, I would not be surprised if it was already fixed since Mono 4.0.