Why my precision getting loss while converting double to float in c#?

Viewed 229

I' having double value as 10293.01416015625 in c# and i'm trying to convert to float. Since float has only 24 bits, i suppose to get the result as 10293.0141. But i'm getting value as 10293.0137

double value = 10293.01416015625;
float converted = (float)value;

Expected value - 10293.0141

Value am getting - 10293.0137

Thanks in advance

1 Answers

From the System.Single documentation:

A Single value has up to 7 decimal digits of precision, although a maximum of 9 digits is maintained internally.

Your result is correct up to 7 significant digits (10293.01). You shouldn't expect to be able to get more than that with float.

The exact value of the floats closest to 10293.01416015625 are 10293.013671875 and 10293.0146484375. Both are exactly 0.00048828125 away from the value you're trying to represent.

Related