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

Y

yong321

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:

SQL> select 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

Ian Collins

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.
 
J

Jeremy J Starcher

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:

SQL> select 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.
 
M

Mitja Trampus

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:
-1.0658141036401503e-014

I expect this is also the case with other languages you've tried. See others' answers for
possible solutions.
 
Y

yong321

Mitja said:
The printed value is somewhat rounded:

-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:

SQL> select 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
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Sat, 1 Jul 2006 20:01:11 remote, seen in (e-mail address removed) 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'.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sun, 2 Jul 2006
15:17:12 remote, seen in Ian Collins <ian-
(e-mail address removed)> posted :
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 withreasonable 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.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top