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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With