dirichlet-numerics
dirichlet-numerics copied to clipboard
Possible bug in ConvertToFloat, ConvertToDouble
I've just had a quick look at the code and saw a possible error:
public static float ConvertToFloat(ref UInt128 a)
{
if (a.s1 == 0)
return a.s0;
return a.s1 * (float)ulong.MaxValue + a.s0; // looks wrong, should be:
// return a.s1 * (float)Math.Pow(2,64) + a.s0;
}
public static double ConvertToDouble(ref UInt128 a)
{
if (a.s1 == 0)
return a.s0;
return a.s1 * (double)ulong.MaxValue + a.s0; // same here:
// return a.s1 * Math.Pow(2,64) + a.s0;
}
And better extract Math.Pow to a constant.
Subtle, but you are correct! I will take a look at this. Thanks
It appears a bug but in fact it's not. Run the following code and you'll see that these numbers are the same because of the precision limitations of float and double.
var a = (double)ulong.MaxValue;
var b = (double)ulong.MaxValue + 1.0;
var c = Math.Pow(2, 64);
var eqab = a == b;
var eqbc = b == c;
var eqca = c == a;
Console.WriteLine($"{a}, {b}, {eqab}, {eqbc}, {eqca}");
Approved, ulong.MaxValue as a float or double rounds up to ulong.MaxValue+1 which is pow(2,64).