float and decimals

Started by
8 comments, last by DevFred 16 years, 3 months ago
Hi I know how to do %.2f in a string to write 2 decimals of a float. But what if i wanna write all decimals with "a value" like f=1.000; //use this to write "1" f=1.320; //use this to write "1.32" Any way to do it? Thanks Erik
Advertisement
What you seem to be trying to do is impossible since it assumes floating point values are exact representations.

What Every Computer Scientist Should Know About Floating-Point Arithmetic
really? but lets say the value of f can only be 1 - 1.5 - 2 - 2.5 and so on.

Isnt there a workaround? I realise i subtract a int-casting of it and see if something remains but that seems so ugly.
Quote:Original post by suliman
really? but lets say the value of f can only be 1 - 1.5 - 2 - 2.5 and so on.


Fixed-point math. google for it.

Quote:Isnt there a workaround? I realise i subtract a int-casting of it and see if something remains but that seems so ugly.


Nope - floating point numbers are inaccurate by definition, regardless of what operations you perform on them.

You could also try %g formatting.
Quote:Original post by Antheus
floating point numbers are inaccurate by definition

Not at all. Floating point numbers have a precision of 24 bits.

That just means you can't represent numbers which require more than said 24 bits. Most decimal numbers require an infinite amount of bits in binary representation. And since infinite is more than 24, you can't represent numbers such as 0.1 precisely. Nevertheless, the actual value that resembles 0.1 the most (which is 0.100000001490116119384765625) is stored accurately within the 24 available bits.
Quote:Original post by suliman
lets say the value of f can only be 1 - 1.5 - 2 - 2.5 and so on.

BTW these numbers actually can be represented exactly by floating point numbers, because 0.5 is 1/2.
why 24 bits?
i thought both float and int were 32 bits?

The inaccuaracy means that if i take 0.1 times a million it will not be excact? I can probably live with that :)
Quote:Original post by DevFred
the actual value that resembles 0.1 the most (which is 0.100000001490116119384765625)


Why that number and not 0.100000000000000000000? Maybe I misunderstood but that kind of confused me.. :P
There was an article linked to in the very first reply to the thread. Read it.
Quote:Original post by Arjan B
Why that number and not 0.100000000000000000000?

In a nutshell, a float consists of

- 1 bit for the sign (0 means positive, 1 means negative)
- 8 bit for the exponent
- 23 bit for the mantissa (a 24th bit is not stored and is implicitly 1, thus 24 bits of accuracy)


These three components are interpeted like this in binary:

(-1)^sign * 10^(exponent - 111111) * 1.mantissa

Note that "10" denotes the binary base 2, and "111111" denotes the value 127.


When you want to store the decimal value 0.1, it would look like this:

(-1)^0 * 10^(01111011 - 111111) * 1.10011001100110011001100110011... (0011 ad infinitum)

But since you cannot store an infinite number of bits in a float, this gets rounded to 24 bits:

(-1)^0 * 10^(01111011 - 111111) * 1.10011001100110011001101

And this cut-off float represents the value 0.100000001490116119384765625


Just remember this: floats work in binary, thus most decimal values cannot be represented with a finite amount of bits available. Just as 1/3 cannot be represented in decimal with a finite amount of digits, because 0.333333333333333 is not the same as 1/3.

This topic is closed to new replies.

Advertisement