Trax
Trax

Reputation: 1033

C# Converting Double to Float Loses So Much Precision Why?

When I'm debugging my program using Visual Studio;

When I convert a double variable "91.151497095188446" to a float by using

(float)double_variable

I see resulting float variable is "91.1515".

Why I'm losing so much precision? Do I need to use another data type?

Upvotes: 0

Views: 2281

Answers (3)

Basit
Basit

Reputation: 297

Floating-point types store fractional parts as inverse powers of two. For this reason, they can only represent exact values such as 10, 10.25, 10.5, and so on. Other numbers, such as 1.23 or 19.99, cannot be represented exactly and are only an approximation. Even if double has 15 decimal digits of precision, as compared to only 7 for float, precision loss starts to accumulate when performing repeated calculations.

This makes double and float difficult or even inappropriate to use in certain types of applications, such as financial applications, where precision is key. For this purpose, the decimal type is provided

Reference : Learn C# Programming, A guide to building a solid foundation in C# language for writing efficient programs By Marius Bancila, Raffaele Rialdi, Ankit Sharma

Upvotes: 0

Wai Ha Lee
Wai Ha Lee

Reputation: 8815

C# and many other languages use IEEE 754 as a specification for their floating-point data types. Floating-point numbers are expressed as a significand and an exponent, similar to how a decimal number, in scientific notation, is expressed as

1.234567890 x 10^12
     ^          ^
  mantissa   exponent

I won't go into the details (the Wikipedia article goes into that better than I can), but IEEE 754 specifies that:

  • for a 32-bit floating point number, such as the C# float data type, has 24 bits of precision for the significand, and 8 bits for the exponent.

  • for a 64-bit floating point number, such as the C# double data type, has 53 bits of precision for the significand, and 11 bits for the exponent.

Because a float only has 24 bits of precision, it can only express 7-8 digits of precision. Conversely, a double has 53 bits of precision so has about 15-16 digits of precision.

As has been said in the comments, if you don't want to lose precision, don't go from a double (64 bits in total) to a float (32 bits in total). Depending on your application, you could perhaps use the decimal data type which has 28-29 decimal digits of precision - but will come with penalties because (a) calculations involving it are slower than for double or float, and (b) it's typically far less supported by external libraries.


Note that you're talking about 91.15149709518846 which will actually be interpreted as 91.1514970951884 by the compiler - see, for example, this:

double value = 91.151497095188446;
Console.WriteLine(value);
// prints 91.1514970951884

Upvotes: 1

Krunx
Krunx

Reputation: 256

You will find detailed explanation here: https://stackoverflow.com/a/2386882/10863059

Basically, float takes smaller memory space (4 bytes) when compared to double (8 bytes), but that's just one part of the story.

Upvotes: 0

Related Questions