Bug 28316 - decimal operation gives incorrect results on mono
Summary: decimal operation gives incorrect results on mono
Status: RESOLVED NOT_REPRODUCIBLE
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2015-03-23 03:22 UTC by vijaya.d.sajjan
Modified: 2015-03-25 10:21 UTC (History)
2 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 NOT_REPRODUCIBLE

Description vijaya.d.sajjan 2015-03-23 03:22:06 UTC
I could see decimal operations gives in correct results on mono.
 
using System;
public class dec
{
  public static void Main() {
  
    decimal decX = (new decimal(123456, 0, 0, false, 0) /
           (decimal)Math.Pow(10, 3));
    Console.WriteLine("decX=" + decX);
    int[] ints = decimal.GetBits(decX);
    for (int j = 0; j < ints.Length; j++)
    {
      Console.WriteLine("ints[" + j + "]=" + ints[j]);
    }
  
   int decInfo = ints[3];
       
   int scale = (byte)((decInfo & 0xff0000) >> 0x10);
    Console.WriteLine("scale =" + scale);
    }
}

Linux machine (Mono)
~~~~~~~~~~~~~~~~~~~
$ mono dotnet.exe
decX=123.45600000000000000000000000
ints[0]=-1610612736
ints[1]=136804058
ints[2]=669256317
ints[3]=1703936
scale =26

Windows machine
~~~~~~~~~~~~~~~
decX=123.456
ints[0]=123456
ints[1]=0
ints[2]=0
ints[3]=196608
scale =3

It seems like above logic is used to eliminate trailing zeros(decimal might have trailing zeros) but some how on Mono machine, trailing zeros are added.
Comment 1 vijaya.d.sajjan 2015-03-23 06:18:44 UTC
it seems like on mono m/c division operation appends '0' at right 
that is the one which is causing the issue.



public static void Main() {
decimal d1 =new decimal(123456, 0, 0, false, 0);
 Console.WriteLine("d1=" + d1);
decimal d2 =(decimal)Math.Pow(10, 3);
 Console.WriteLine("d2=" + d2);
    decimal decX = d1/d2;
    Console.WriteLine("decX=" + decX);
}

output
d1=123456
d2=1000
decX=123.45600000000000000000000000

I need the output 123.456 instead of '123.45600000000000000000000000'.
how to achieve this on mono ?
Comment 2 vijaya.d.sajjan 2015-03-24 06:03:15 UTC
it looks like Bug 655780 - Decimal.ToString sometimes produces excess trailing zeros.. 


but I could see the issue is still reproducible with  all the Mono C# compiler

Can you please tell us how to verify issue is resolved in latest versions
Comment 3 Marek Safar 2015-03-25 10:21:01 UTC
Works for me with Mono 4.0 and newer