2

Possible Duplicates:
Why 81.66 * 15 = 1224.8999999 in Javascript (or Perl) and not 1224.9 ?
Is JavaScript’s math broken?

var t1 = 5000;
var t2 = 0.07;
alert(t1 * t2);

Here is very simple code that return incorrect result; I predict the result is 350

but the result is 350.00000000000005

How can I get correct result?

and what's wrong in this code?

Community
  • 1
  • 1
Sungguk Lim
  • 6,109
  • 7
  • 43
  • 63
  • 1
    This is a duplicate of any one of the following: http://stackoverflow.com/questions/2387675/why-81-66-15-1224-8999999-in-javascript-or-perl-and-not-1224-9 http://stackoverflow.com/questions/2284873/float-inaccuracies-in-python http://stackoverflow.com/questions/2379934/unexpected-result-using-posix-ceil-in-perl http://stackoverflow.com/questions/872544/precision-of-floating-point – Sinan Ünür Mar 10 '10 at 00:52
  • 1
    Indeed... A well-worn classic: http://stackoverflow.com/questions/588004/is-javascripts-math-broken – Shog9 Mar 10 '10 at 00:54
  • [Read](http://en.wikipedia.org/wiki/Floating_point). This is a duplicate of a million other questions. – Stefan Kendall Mar 10 '10 at 00:50

3 Answers3

3

Floating point arithmetic in computers is an approximation of the value.

see http://en.wikipedia.org/wiki/Floating_point#Problems_with_floating-point

Jonathan S.
  • 541
  • 1
  • 3
  • 13
  • 2
    I disagree - it's no more a problem with floating point than 1/3 or π are for the decimal number system. You simply can't represent 0.1 exactly in binary. – duffymo Mar 10 '10 at 00:53
  • 1
    @duffmo Floating point variables have a given number of bits to encode a numeric value. There are uncountably infinitely many numbers yet only a finite number of bits. Therefore, there will always be some values that cannot be represented whether you use binary or BCD or whatever using the finite number of bits available to you. Now, if you go the arbitrary precision route, you are still limited by the machine's memory which is finite. Now, keep in mind that `1/3` is the most accurate representation of `1/3`. – Sinan Ünür Mar 10 '10 at 00:56
  • No, I understand it quite well, thanks. I know all about floating point numbers. – duffymo Mar 10 '10 at 01:00
  • @duffymo Are you saying that 0.1 when represented as a float is an approximation of the value 0.1? Sure sounds like you agree with Jonathan if you ignore the "I disagree" part of your comment. – lillq Mar 10 '10 at 01:00
  • 1
    What duffymo is trying to say is that every number system, regardless of base, will have trouble representing some values using a finite number of digits. This becomes a problem with floating-point because of the *finite* number of bits available for storage according to the spec, as others have pointed out. – Peter Mar 10 '10 at 01:04
  • I'm saying that if you try to write the value 0.1 in binary that you won't be able to do it exactly: "Some numbers (e.g., 1/3 and 0.1) cannot be represented exactly in binary floating-point no matter what the precision." – duffymo Mar 10 '10 at 01:21
  • There's no such thing as an exact decimal representation of 1/3, regardless. They're called irrational numbers. Same for pi and e. – duffymo Mar 10 '10 at 01:22
1

Math.round() will help you :-)

Phil Rykoff
  • 11,999
  • 3
  • 39
  • 63
0

The definitive article to read is

What Every Computer Scientist Should Know About Floating-Point Arithmetic

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339