[Mono-list] Bug in the scale of the result of decimal division

Marc Bettex bettex at epsitec.ch
Fri Feb 10 10:07:15 UTC 2012


Hi,

While porting an application from .Net to Mono, I bumped into a problem with
decimal division and the scale of their result. Consider the following code
:

decimal v10 = 10m;
decimal v100 = 100m;
decimal v1000 = 1000m;

// Mono prints 1
// .Net prints 1
Console.WriteLine (10m / 10m);

// Mono prints 1
// .Net prints 1
Console.WriteLine (v10 / 10m);

// Mono prints 1
// .Net prints 1		
Console.WriteLine (10m / v10);

// Mono prints 1
// .Net prints 1		
Console.WriteLine (v10 / v10);

// Mono prints 0.10
// .Net prints 0.10
Console.WriteLine (10m / 100m);

// Mono prints 0.1000000000000000000000000000
// .Net prints 0.10
Console.WriteLine (v10 / 100m);

// Mono prints 0.1000000000000000000000000000
// .Net prints 0.10
Console.WriteLine (10m / v100);

// Mono prints 0.1000000000000000000000000000
// .Net prints 0.10
Console.WriteLine (v10 / v100);

// Mono prints 0.01
// .Net prints 0.01
Console.WriteLine (10m / 1000m);

// Mono prints 0.0100000000000000000000000000
// .Net prints 0.01
Console.WriteLine (v10 / 1000m);

// Mono prints 0.0100000000000000000000000000
// .Net prints 0.01
Console.WriteLine (10m / v1000);

// Mono prints 0.0100000000000000000000000000
// .Net prints 0.01
Console.WriteLine (v10 / v1000);

While .Net never introduces trailing zeroes, Mono introduces them when the
division has a variable in it and if the result is not an integer value. If
the division is made of two constant values, the result does not have
trailing zeroes.

I suspect that there is a difference if the division has two constant or not
because it probably occurs at compile time when there are only constants
involved and it probably occurs at runtime when there is a variable
involved. But this is simply a supposition.

Anyway, according to the c# specification §7.8.2 which describes the
division operator for decimal types: "The scale of the result is the
smallest scale that will preserve a result equal to the nearest
representantable decimal value to the true mathematical result." My
understanding of this paragraph is that in these cases, trailing zeroes
should not be introduced.

This make me think that this is a bug in the implementation of the decimal
divison operator in Mono, but maybe I'm missing something, or maybe there is
a design reason for this. But even if this is a concious decison not to
behave like .Net, I don't think it is right that the result of the operation
is not the same if there are only constants involved or if there is a
variable involved. Does anybody have an idea on this ?

--
View this message in context: http://mono.1490590.n4.nabble.com/Bug-in-the-scale-of-the-result-of-decimal-division-tp4375794p4375794.html
Sent from the Mono - General mailing list archive at Nabble.com.


More information about the Mono-list mailing list