472,353 Members | 1,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

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
6 2321
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pekka Henttonen | last post by:
Forgive me this stupid question, but what is the best way to store the length of an object in a database? What datatype would you use? Would it be...
10
by: John | last post by:
Hi all... Either I'm doing really something really stupid - or maybe there is some bug somewhere (optimizing?). In a function I have: int x1,...
24
by: j0mbolar | last post by:
C supports single precision floating point and double precision floating point but does it support fixed floating point? i've read that fixed...
10
by: Shawn | last post by:
Hello all, I apologize as I am sure this has probably been dealth with before... but I am doing an exercise from "Practical C Programming" and I...
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. ...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. ...
32
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b;...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at:...
12
by: EvilOldGit | last post by:
In Stroustrup he talks of a MathErr class that you can use to catch floating point exceptions. It doesn't exist, at least not in Visual C++ I...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

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.