Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
AndrewB

0.1 + 0.1 + 0.1 does not equal 0.3

Recommended Posts

... at least according to most programming languages.

I've actually known this for a while, but I felt like saying it now.

Share this post


Link to post

0.30000000000000000004 or something like that. Therefore, the language will tell you that they're not equal.

Share this post


Link to post

Meanwhile, in "learn your tech" land, most programmers attribute that to floating point inaccuracies. The FPU is great and fast for the most part, but is known to lose accuracy. In the case of the amount of digits there though, it appears to be a double precision floating point number, which is done in software (unless you're a shiny new 64 bit processor). If you really want accuracy with your fractional numbers, try fixed point processing (stability traded off with flexibility).

Share this post


Link to post

The most common implementations are divided by powers of two. There are a few other methods some of which are explained in here.

Fun fact: the PS2's main processor (the EE) has no FPU. All operations are done in fixed point arithmetic. Only the Vector Units perform operations in floating point arithmetic. I doubt most of you have access to a PS2, but you'll ********ly notice that this is true if you try something like 1.f / 60.f * 60.f on the EE as opposed to trying it on your PC.

EDIT: Why the fuck is D E F I N A T E being starred out?

Share this post


Link to post
GooberMan said:

In the case of the amount of digits there though, it appears to be a double precision floating point number, which is done in software (unless you're a shiny new 64 bit processor).

Uhh no, 32-bit x86 processors (and many others) do floating-point math with 80 bits precision in hardware.

And of course, since 0.1 can't be represented accurately as a binary fraction no matter how many bits you use, it doesn't matter if a float or double or quad is used. If precision is needed, use ints (multiplying all values by 100 or whatever first) or a decimal type instead.

Share this post


Link to post

The IEEE 754 standard is a method of storing floating-point numbers in a compact way that is easy to manipulate. This standard is used by Intel coprocessors and most PC-based programs that implement floating-point math.

IEEE 754 specifies that numbers be stored in binary format to reduce storage requirements and allow the built-in binary arithmetic instructions that are available on all microprocessors to process the data in a relatively rapid fashion. However, some numbers that are simple, nonrepeating decimal numbers are converted into repeating binary numbers that cannot be stored with perfect accuracy.

For example, the number 1/10 can be represented in a decimal number system with a simple decimal:
.1
However, the same number in binary format becomes the repeating binary decimal:
.0001100011000111000111 (and so on)
This number cannot be represented in a finite amount of space. Therefore, this number is rounded down by approximately -2.78E-17 when it is stored.

If several arithmetic operations are performed to obtain a given result, these rounding errors may be cumulative.

Share this post


Link to post
AndrewB said:

There is one fundamental reason why this occurs. Can anyone say what it is?

Yes.

Fredrik said:

0.1 can't be represented accurately as a binary fraction no matter how many bits you use

Share this post


Link to post

Syzmanski wins the award.

... However, I believe the binary number for 1/10 is 0.00011001100 ... not whatever it was you said.

Share this post


Link to post

I sorta cheated, I've had it explained before by the guy who writes the drivers for our hardware. But it's easier to copy+paste.

Share this post


Link to post
Captain Red said:

because ling is a big jerk.


WRONG

GooberMan said:

Not in Australia.


Any other wierd ways to spell words you'd like to share with us?

Share this post


Link to post
Fredrik said:

Uhh no, 32-bit x86 processors (and many others) do floating-point math with 80 bits precision in hardware.

Oops. I remembered the FPU did processing with 80 bits, dunno why I thought it did double in software.

Everyone said:

Australian spelling differences

There are plenty. Jail is spelt gaol for example. Civilization is spelt civilisation (in fact, many words with a z similar to above is spelt with an s).

Share this post


Link to post
fraggle said:

0.99999999999.... = 1

ARGH ARGH ARGH ARGH ARGH ARGH ARGH
CLOSE THREAD NOW
For the love of God
before the head explosions begin
ARGH ARGH ARGH ARGH ARGH ARGH ARGH

Share this post


Link to post
fraggle said:

0.99999999999.... = 1

Then I take it 3 = 1 × 3 = 0.99999999... × 3 = 2.9999999....997, mister clever?

Share this post


Link to post

Ling is an arse and should definately definitely go to gaol for his Prejudice ageist Australians. Anyway, I’ve got to put some Petrol in the car (it can go up to 160 kilometres per hour!) and paint the house a different colour. After that, I think I’ll play Medal of Honour, or maybe some Zed-doom.

:P

Paaaaaaaaach the caaaaaaaaaaaaaaaaaant!

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×