By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,029 Members | 1,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,029 IT Pros & Developers. It's quick & easy.

What's the best way to handle this floating point problem?

P: n/a
With this script

<script>
alert(8/(3-8/3))
</script>

I hope to get 24, but I get 23.99999999999999 in IE 6.0.2800 and
Firefox 1.5.0.4. alert(6/(1-3/4)) returns 24 as expected.

I see a number of threads in this newsgroup about the floating point
issue. It may be hard to get it right. But other languages I tried
don't have this problem with this calculation.

Perl:

C:\>perl -e "print 8/(3-8/3)"
24

Python:

$ python -c 'print 8/(3-8/3.0)' #have to say 3.0, not 3
24.0

Oracle:

SQLselect 8/(3-8/3) from dual;

8/(3-8/3)
----------
24

and awk:

$ awk "BEGIN {print 8/(3-8/3)}"
24

I want to compare the calculation result with 24. What's the best
solution in Javascript? Any advice is appreciated.

Yong Huang

Jul 2 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
yo*****@yahoo.com wrote:
>
I want to compare the calculation result with 24. What's the best
solution in Javascript? Any advice is appreciated.
You shouldn't compare floating point numbers in any language (unless it
makes some guarantees regarding their accuracy).

The solution is the same as in any other language, compare against a range.

--
Ian Collins.
Jul 2 '06 #2

P: n/a
On Sat, 01 Jul 2006 20:01:11 -0700, yong321 wrote:
With this script

<script>
alert(8/(3-8/3))
</script>

I hope to get 24, but I get 23.99999999999999 in IE 6.0.2800 and
Firefox 1.5.0.4. alert(6/(1-3/4)) returns 24 as expected.

I see a number of threads in this newsgroup about the floating point
issue. It may be hard to get it right. But other languages I tried
don't have this problem with this calculation.

Perl:

C:\>perl -e "print 8/(3-8/3)"
24

Python:

$ python -c 'print 8/(3-8/3.0)' #have to say 3.0, not 3
24.0

Oracle:

SQLselect 8/(3-8/3) from dual;

8/(3-8/3)
----------
24

and awk:

$ awk "BEGIN {print 8/(3-8/3)}"
24

I want to compare the calculation result with 24. What's the best
solution in Javascript? Any advice is appreciated.

Yong Huang
I would guess that in most of those examples the answer
is silently changed to an integer for you. I'm sure that
I won't be the only one to say "Don't do an exact compare
of floating point numbers" and refer you to the wonderful
work of David Goldberg: (Google for it.)
What Every Computer Scientist Should Know About
Floating-Point Arithmetic

Your options are:
a) Convert it to an integer
b) Compare to a range (if (a-b) < 0.001 then close enough)
c) Rely on how one particular browser happens to handle
floating point numbers and then curse and swear when the
next version of that browser comes out.
Jul 2 '06 #3

P: n/a
yo*****@yahoo.com wrote:
With this script
alert(8/(3-8/3))

I hope to get 24, but I get 23.99999999999999 in IE 6.0.2800 and
Firefox 1.5.0.4. alert(6/(1-3/4)) returns 24 as expected.

I see a number of threads in this newsgroup about the floating point
issue. It may be hard to get it right. But other languages I tried
don't have this problem with this calculation.
$ python -c 'print 8/(3-8/3.0)' #have to say 3.0, not 3
24.0
The printed value is somewhat rounded:
>>a = 8/(3-8/3.0)
print a
24.0
>>a == 24
False
>>a-24
-1.0658141036401503e-014

I expect this is also the case with other languages you've tried. See others' answers for
possible solutions.
Jul 2 '06 #4

P: n/a
Mitja Trampus wrote:
yo*****@yahoo.com wrote:
With this script
alert(8/(3-8/3))

I hope to get 24, but I get 23.99999999999999 in IE 6.0.2800 and
Firefox 1.5.0.4. alert(6/(1-3/4)) returns 24 as expected.

I see a number of threads in this newsgroup about the floating point
issue. It may be hard to get it right. But other languages I tried
don't have this problem with this calculation.
$ python -c 'print 8/(3-8/3.0)' #have to say 3.0, not 3
24.0

The printed value is somewhat rounded:
>>a = 8/(3-8/3.0)
>>print a
24.0
>>a == 24
False
>>a-24
-1.0658141036401503e-014

I expect this is also the case with other languages you've tried. See others' answers for
possible solutions.
Thanks, Mitja. You're right. See this.

Oracle SQL:

SQLselect decode(8/(3-8/3), 24, 'equals', 'not equals') from dual;

DECODE(8/(
----------
not equals

awk:

C:\>gawk "BEGIN {if (8/(3-8/3)!=24) {print \"not equals\"}}"
not equals

C:\>gawk "BEGIN {print 24 - 8/(3-8/3)}"
1.06581e-014

I think that's enough. I'll take the suggestion to compare with a very
small range. Thanks, everyone.

Yong Huang

Jul 2 '06 #5

P: n/a
JRS: In article <11**********************@m73g2000cwd.googlegroups .com>,
dated Sat, 1 Jul 2006 20:01:11 remote, seen in news:comp.lang.javascript,
yo*****@yahoo.com posted :
><script>
alert(8/(3-8/3))
</script>

I hope to get 24, but I get 23.99999999999999 in IE 6.0.2800 and
Firefox 1.5.0.4. alert(6/(1-3/4)) returns 24 as expected.

Where possible, calculations should be adapted so that all values are
integers multiplied by a not-too-big +- power of 2. You will find no
trouble with alert(8*3/(3*3-8)) ; your second calculation uses only
exact numbers. Please do not start a sentence with a lower-case letter,
since it can lead to confusion; there's always some alternative, such as
inserting the word "And".

Read the newsgroup FAQ (which you should have done before posting), and
my js-round.htm /ff./; via sig below.

If Firefox shows "false" for alert( (8/(3-8/3)) == 24 ) - and that is
IMHO what it should show - then it is deceitfully misrepresenting the
number 23.99999999999999 as a displayed string '24'.
--
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.
Jul 2 '06 #6

P: n/a
JRS: In article <4g*************@individual.net>, dated Sun, 2 Jul 2006
15:17:12 remote, seen in news:comp.lang.javascript, Ian Collins <ian-
ne**@hotmail.composted :
>yo*****@yahoo.com wrote:
>>
I want to compare the calculation result with 24. What's the best
solution in Javascript? Any advice is appreciated.
You shouldn't compare floating point numbers in any language (unless it
makes some guarantees regarding their accuracy).
All javascript numbers are floating-point, except maybe in future
versions. So you are actually saying that one should never use
relational operators between javascript Numbers.
In any language, it is reasonable to compare floating-point numbers with
= < <= but not with == or != (in javascript notation). It is also
reasonable to compare for equality floats that are certain to have
integer values (more strictly - that are certain to have exact values).
It is not appropriate to compare for equality a javascript float which
has been calculated by inexact arithmetic, and that means where any
number involved is not of the form of a 53-bit binary integer times two-
to-the-N for N less than about 1024.
--
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.
Jul 2 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.