473,325 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

eval() delivers incorrect results?

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.

Sep 27 '06 #1
4 2088
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>

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.

Sep 27 '06 #2
ni********@gmail.com 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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 27 '06 #3
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.
Sep 27 '06 #4

Dr John Stockton wrote:
<Ri*****@litotes.demon.co.ukposted :
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.
Thanks guys, someone else pointed out this was due to binary fractions.
So if there's someone out there reading this and going "wooot" like I
did, here's Dr. Math explaining it to you:
http://mathforum.org/library/drmath/view/56091.html
It's a good idea to read the newsgroup and its FAQ. See below.
I didn't realize there was a FAQ.. :)

-Niko

Sep 28 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Chris Connett | last post by:
I have an interesting problem with eval(). ---Background Info--- The program I'm working on launches a separate process with a popen to do some highly specialized processing of input, then this...
9
by: HikksNotAtHome | last post by:
This is a very simplified example of an Intranet application. But, is there an easier (or more efficient) way of getting the decimal equivalent of a fraction? The actual function gets the select...
4
by: tozeina | last post by:
Can any one help in this please. I'm using eval function in JavaScript, But when the eval method return a big big number the result will be a number with "E" for example :...
0
by: Tim | last post by:
I am currently running a search via Index Server and allowing users to Search within Results. To search within results, I loop through the original dataset and get the WorkIDs. I then run a new...
13
by: Stephen Kay | last post by:
Is there a way to redirect every single page on an existing web site through a php function? In other words, say I have a whole functional HTML web site, never written to use any php. Now I...
8
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - When should I use eval? ----------------------------------------------------------------------- The ` eval() `...
3
by: Abel Daniel | last post by:
Hi! A python interactive interpreter works by having the user type in some code, compiling and running that code, then printing the results. For printing, the results are turned into strings. ...
5
by: Smiley | last post by:
I'm fooling around with using Eval and trying to manipulate a few things. I ran into a couple of weird results. First of all, in one place I used the following code: $filestring =...
13
by: My Pet Programmer | last post by:
The way I usually set up and work with the XMLHttpRequest to execute server side functions and get results is this: var url = "someurl?params=" + params; var conn = createRequest(); // gets an...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.