[Solved] Int64 arithmetic error


Statement (long)0.75 will return 0 (converting double to long will take greatest integer value, that is lower that converting double). So you have 0 * 9223372036854775807 which is hopefully 0. By the way long is alias for Int64, which means they are the same underlying types, so by (long)Int64.MaxValue you casting long to long

to fix it simply use double * long multiplication

long small=  (long)(0.25 * long.MaxValue);
long large = (long)(0.75 * long.MaxValue);

Console.WriteLine ("{0:N}", small); //print 2,305,843,009,213,693,952.00
Console.WriteLine ("{0:N}", large); //print 6,917,529,027,641,081,856.00

don’t bother with "{0:N}" literal, it’s simply a format to give the output some visual beauty. By default double is printed in scientific notation, which is not so demonstrable (for example 2.30584300921369E+18 for first operation)

0

solved Int64 arithmetic error