Ambar Kulkarni
Ambar Kulkarni

Reputation: 3

Loss of data converting a UInt64 to float in C#

I am attempting to do some mat on two UInt64 values and store the result in a float:

UInt64 val64 = 18446744073709551615;
UInt64 val64_2 = 18446744073709551000;

float val = (float)val64 - val64_2;
Console.WriteLine(val64);
Console.WriteLine(val.ToString("f"));

Console.ReadKey();

I am expecting the val to be 615.0 but instead I am getting 0.0! Using double instead for val seems to work but surely float is capable of storing 615.0. What am I missing here?

Upvotes: 0

Views: 141

Answers (2)

qed-
qed-

Reputation: 1010

Float is an approximation that can store only 6 or 7 significant digits (see https://msdn.microsoft.com/en-us/library/hd7199ke.aspx)

In your code both UInt64s end up as 1.84467441E+19 when cast to float.

As various people have already mentioned the solution is to keep the values as UInt64s for the subtraction.

Upvotes: 0

AndySavage
AndySavage

Reputation: 1769

It's not the result that is being truncated, it's the values used in the calculation. You are casting val64 to a float in your sum. This also means val64_2 will be cast to a float (to match val64). Both have lost enough precision that they are the same value when represted as a float, and the difference is 0.

You want to keep them as UInt64 for the subtraction, and have the result as a float. i.e.

float val = (float)(val64 - val64_2);

Upvotes: 1

Related Questions