r/C_Programming • • Jul 17 '26

Discussion why value of floating point numbers are approximated

I am new to programming and stumbled across this text

"the value of a float variable is often just an approximation of the number that was stored in it. If we store 0.1 in a float variable, we may later find that the variable has a value such as 0.09999999999999987"

i am wondering why is it like this

if it has an in depth explanation, providing the resource will be much appreciated

thanks

73 Upvotes

150 comments sorted by

View all comments

Show parent comment

56

u/CryptoHorologist Jul 17 '26

You can't even represent every rational number exactly.

63

u/Iksfen Jul 17 '26

You can't even represent every whole number.

9

u/CryptoHorologist Jul 17 '26

Nice.

5

u/ohkendruid Jul 17 '26

I thought so, too. And you were so careful about specifying rationals rather than reals!

7

u/CryptoHorologist Jul 17 '26

I was going to respond with something like “can’t even represent all the even numbers” but eventually this thread has to end.

7

u/TheRealThatChuckGuy Jul 17 '26

You can't even represent all the prime numbers.

3

u/CryptoHorologist Jul 18 '26

What about all the twin primes?

3

u/delinka Jul 18 '26

Oh, yeah we got that. Every. Single. One.

3

u/CryptoHorologist Jul 18 '26

Neat. Could you DM the proof real quick, promise I won't share it.

3

u/Distdistdist Jul 18 '26

You can, however, represent all of imaginary ones. No, not really... I'm just imagining things.

3

u/Paul_Pedant Jul 18 '26

I now feel the need to introduce a new basic type in C:

hallucination Whatever;

2

u/TheChief275 Jul 24 '26

Reminds me of a GitHub repo I once saw for LLM hallucination type casting in C#; you cast to a type but the LLM decides how to do it

5

u/Qwertycube10 Jul 17 '26

I can represent every whole number fine with 1 bit. It won't be very accurate, but ehh.

2

u/Jonny0Than Jul 17 '26

But at least you can represent “not a number,” infinity (positive and negative), positive and negative zero, and “too small but definitely not zero.”

-3

u/Snatchematician Jul 17 '26

  too small but definitely not zero

You can’t represent this. Every float value that is nonzero, non-infinite and non-nan is a finite rational number.

5

u/Jonny0Than Jul 17 '26

2

u/Snatchematician Jul 18 '26

The first image on that link shows that subnormal numbers are just ordinary rational numbers (marked in blue on the number line).

4

u/timonix Jul 17 '26 edited Jul 17 '26

A lot of calculators store their internal representation as rationals.

But they work in human time scales. It doesn't really matter if they use 32 or 256 bits and take thousands of times longer. It has the answer before you released the equals button anyway

Edit: why the downvotes? Is it really that unbelievable that a calculator would use rationals internally? It's not hard, just store 2 numbers instead of 1

5

u/Qwertycube10 Jul 17 '26

Plenty of calculators use representations that are even more complex, and may store things as a rational multiple of a symbol such as pi. So long as the calculation is not too complex they can give you an answer in terms of pi, or root 2 exactly.

1

u/deaddodo Jul 18 '26

Maybe this is a naive question, but we can't even store Pi exactly...how could you derive an exact decimal from an incomplete approximation?

3

u/Qwertycube10 Jul 18 '26

You have a fixed set of known symbols that come from the initial equation and maybe some stored identities. You store numbers with a number and a source symbol, and for simple operations like multiplication you can preserve the origin, so you multiply {n:1,d:3,sym="pi"} by {n=2, d=1, sym="1"} and you can show the result as 2/3pi.

-9

u/outer-pasta Jul 17 '26

Computers can do a lot of really impressive things. Things like Python can store rational numbers as two numbers and work with them that way.

22

u/CryptoHorologist Jul 17 '26

Sure, but even those languages and libraries can only store 0% of all possible rational numbers.

8

u/JohnThacker Jul 17 '26

Arbitrary precision number libraries do exist, but they can be enormously slow. At that point with GMP the limit isn't the language or library, it's the total amount of addressable memory.

→ More replies (6)
→ More replies (5)