Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Arithmetic Question

Tags:

c#

math

overflow

Good afternoon,

Having never used C# to do serious mathematical work, I have just noticed something which left me confused... If it is true that

double Test = Math.Sqrt(UInt64.MaxValue)

is equal to 4294967296.0, that is, UInt32.MaxValue + 1, why is it that

ulong Test2 = UInt32.MaxValue * UInt32.MaxValue;

is equal to 1? At first sight it seems to me that overflow occurs here... But why is that since that product should fit a UInt64?

Thank you very much.

like image 386
Miguel Avatar asked Dec 09 '22 10:12

Miguel


1 Answers

the first one happens because double doesn't have 64 mantissa bits, but only around 53. So UInt64.MaxValue will be rounded to UInt64.MaxValue+1 during the conversion to double. And the Sqrt of that is obviously 2^32. double can represent any value from (U)Int32 exactly, but some of the larger 64 bit integers can't be represented as double.

The second one happens because you do the multiplication before casting to UInt64, i.e. it happens as UInt32, which obviously overflows. Cast at least one of your operands to UInt64 and the problem will disappear.

like image 119
CodesInChaos Avatar answered Dec 11 '22 23:12

CodesInChaos