Public Group

# float and decimals

This topic is 3987 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by sulimanreally? 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.

##### Share on other sites
Quote:
 Original post by Antheusfloating 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.

##### Share on other sites
Quote:
 Original post by sulimanlets 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.

##### Share on other sites
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 :)

##### Share on other sites
Quote:
 Original post by DevFredthe 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

##### Share on other sites
There was an article linked to in the very first reply to the thread. Read it.

##### Share on other sites
Quote:
 Original post by Arjan BWhy 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.

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 13
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633692
• Total Posts
3013355
×