eval() delivers incorrect results?

N

niko.nyman

I noticed weird results when using eval() to do some simple
calculations. Here are a few eval() statements and the corresponding
results from JavaScript:

eval("1.2+0.97") => 2.17
eval("1.2+0.98") => 2.1799999999999997
eval("1.2+0.99") => 2.19
eval("1.2+0.981") => 2.181

This can happen with other numbers as well, but certainly not with any
combination.

I have confirmed this on Firefox and Safari, two different versions of
OS X and both Intel and PowerPC Macs.
 
R

Richard Cornford

I noticed weird results when using eval() to do some simple
calculations. Here are a few eval() statements and the corresponding
results from JavaScript:

eval("1.2+0.97") => 2.17
eval("1.2+0.98") => 2.1799999999999997
eval("1.2+0.99") => 2.19
eval("1.2+0.981") => 2.181
<snip>

Those are correct result. The number type in javascript is an IEEE
double precision floating point number. It cannot represent all number
precisely so when it cannot represent a number it uses the nearest
approximation. Mathematics using the nearest approximation produce
approximate results. You will notice that the difference between the
results you expect and the results you have is tiny.

Also remember that this phenomenon is part of all representations of
non-integer numbers. For example, what is the decimal fraction
representation of one divided by three? Is it precise or an
approximation?

Richard.
 
R

Randy Webb

(e-mail address removed) said the following on 9/27/2006 10:28 AM:
I noticed weird results when using eval() to do some simple
calculations. Here are a few eval() statements and the corresponding
results from JavaScript:

That's not eval giving incorrect results, it is floating point errors
you see. But the biggest problem in your code was using eval to do
simple calculations.
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Wed, 27 Sep 2006 07:36:42 remote, seen in
Richard Cornford

Eval is not needed for such (FAQ 4.40) : 1.2 + 0.98 gives the same.

Those are correct result. The number type in javascript is an IEEE
double precision floating point number. It cannot represent all number
precisely so when it cannot represent a number it uses the nearest
approximation. Mathematics using the nearest approximation produce
approximate results. You will notice that the difference between the
results you expect and the results you have is tiny.

Also remember that this phenomenon is part of all representations of
non-integer numbers. For example, what is the decimal fraction
representation of one divided by three? Is it precise or an
approximation?

Have you so completely lost interest in the newsgroup FAQ that you no
longer want to cite its relevant articles, 4.7 and 4.6 ?


It's a good idea to read the newsgroup and its FAQ. See below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top