JRS: In article <11**********************@m73g2000cwd.googlegroups .com>,
dated Wed, 27 Sep 2006 07:36:42 remote, seen in
news:comp.lang.javascript, Richard Cornford
<Ri*****@litotes.demon.co.ukposted :
>niko.ny...@gmail.com wrote:
>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>
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.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.