views:

327

answers:

6

Possible Duplicates:
Why does 99.99 / 100 = 0.9998999999999999
Dealing with accuracy problems in floating-point numbers

I've seen this issue in php and javascript. I have this number: float 0.699

if I do this: 0.699 x 100 = 69.89999999999999

why?

edit

round(0.699 x 10, 2): float 69.90000000000001

+10  A: 

Floating point arithmetic is not exact.

See Floating point on Wikipedia for a deeper discussion of the problem.

Emil Vikström
1.0 + 1.0 = 3 for very large numbers of 1
Matt S
1 + 1 = 3. Putting the decimal 0 makes it so the 1 can't be a large number.
Samuel
I'm not getting it :(
Makram Saleh
the wikipedia article explains it well, thank you!
Sirber
+1  A: 

Javascript numbers are floating point.

Take a look at The complete javascript number reference. Excerpt:

All numbers in Javascript are 64bit (8 bytes) floating point numbers which yields an effective range of 5e-324 (negative) to 1.7976931348623157e+308 (positive) at the time this article was written (this may eventually change to 128 bits in the future as 64 bit processors become commonplace and the ECMA standards evolve).

Integers are considered reliable (numbers without a period or exponent notation) to 15 digits (9e15) 1. Floating point numbers are considered only as reliable as possible and no more! This is an especially important concept to understand for currency manipulation as 0.06 + 0.01 resolves to 0.06999999999999999 instead of 0.07.

marapet
+2  A: 

This will happen in any language. Floats, like everything else on a computer, are stored as binary. The number 0.699, while representable exactly in decimal, is probably a repeating decimal in binary, so it can't be stored to exact precision.

Check out the wikipedia entry for how floats are stored, and why this happens.

Tesserex
+5  A: 

This is what has helped me in the past. It has a lot to do with how things are represented in binary. Basically long story short in binary there isn't an exact number for all real numbers of large numbers.

The link below will describe that in more detail for you.

What Every Computer Scientist Should Know About Floating-Point Arithmetic

chenger
FTA: Squeezing infinitely many real numbers into a finite number of bits requires an approximate representation.
Sirber
Yes but as they explain later trying to show 0.1 in binary is not easy especially when you are constrained to a certain amount of space..1^10 is represented .00011001100110011001100110011001^2 which is actually .09...... some other stuff.
chenger
A: 

Take a look at Floating Point, specifically the section on IEEE 754 and representable numbers.

siz
A: 

This behavior can be reproduced in many programming languages, including C++ and Assembly. The reason is floating point format using by FPU. You can read details here:

http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/Chapter_14/CH14-1.html#HEADING1-19

General rule: never expect exact result of floating-point operations. Never compare two floating point numbers, use interval, for example: instead of testing f1 == f2, use f1 > (f2 -e) and f1 < ( f2 + e ), e is some small value.

Alex Farber