473,396 Members | 1,996 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,396 software developers and data experts.

parseFloat function seems to have a bug when summing? help!

hello!
i think i have a bug when i use parseFoat function:

i have the following operation :

parseFloat("10.664062") + parseFloat("10.664062")
+parseFloat("10.664062");

the result of following statement must be 31.992186. Does somebody
explain to me why the result returned is 31.992185999999997.

strange no?

thanks a lot for your help.
Jul 23 '05 #1
5 3930
No, it is not a bug. The reason for this result is the fact that the
internal representation of a float (which follows IEEE 754 standard) is
limited to 7 correct digits, because of the limitation to 32 bits. See e.g.
http://research.microsoft.com/~holla...ieeefloat.html (that
looks correct to me, i haven't read it all)

If you want a higher precision, you'll have to use a double precision
floating point variable, but i don't know whether this exists in javascript.

Tim
Jul 23 '05 #2
Tim Fooy wrote:
No, it is not a bug. The reason for this result is the fact that the
internal representation of a float (which follows IEEE 754 standard) is
limited to 7 correct digits, because of the limitation to 32 bits. See e.g.
http://research.microsoft.com/~holla...ieeefloat.html (that
looks correct to me, i haven't read it all)

If you want a higher precision, you'll have to use a double precision
floating point variable, but i don't know whether this exists in javascript.


You can use the toFixed() method to convert a Number object to a fixed
number of decimal places.

e.g.

(1.159).toFixed(2)

would return 1.16

ref: http://tinyurl.com/3ys5x
drew.
Jul 23 '05 #3
"Tim Fooy" <th*****@invalid.com> writes:
No, it is not a bug. The reason for this result is the fact that the
internal representation of a float (which follows IEEE 754 standard) is
limited to 7 correct digits, because of the limitation to 32 bits.
Actually, Javascript numbers are 64 bit (double precission) IEEE 754
floating point numbers, so it has 53 significant bits. The problem is
not the number of significant digits itself, but merely the fact that it
is finite. There are some numbers that look simple in base ten that
cannot be represented exactly in base two. The small error on the
53rd bit accumulates when you do arithmetic, until it becomse visible.

Try something as simple as
alert(0.1 + 0.1 + 0.1);
which alerts 0.30000000000000004 in my browser.
If you want a higher precision, you'll have to use a double precision
floating point variable, but i don't know whether this exists in javascript.


It does, it's all there is.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #4
JRS: In article <40***********************@news.skynet.be>, seen in
news:comp.lang.javascript, Tim Fooy <th*****@invalid.com> posted at Wed,
28 Apr 2004 13:25:13 :
No, it is not a bug. The reason for this result is the fact that the
internal representation of a float (which follows IEEE 754 standard) is
limited to 7 correct digits, because of the limitation to 32 bits. See e.g.
http://research.microsoft.com/~holla...ieeefloat.html (that
looks correct to me, i haven't read it all)

If you want a higher precision, you'll have to use a double precision
floating point variable, but i don't know whether this exists in javascript.


What you write would be true if Numbers in javascript were represented
by IEEE Singles. They are represented by IEEE Doubles.

Although the OP only gets 7 digits as desired, the error is actually of
the order of 1 in 1E16.
I cannot explain why the OP gets 31.992185999999997 ; ISTM that one gets
31.992185999999996.

The OP could have tested +"10.664062" + +"10.664062" + +"10.664062" or
10.664062 + 10.664062 + 10.664062 or 3*10.664062 all of which
give me 31.992185999999996 - which shows that the matter has nothing
to do with parseFloat.

The OP should have read the FAQ, specifically Sec 4.7, and what it links
to and/or my site and/or many others.

Try 0.2 + 0.4 and 0.1 + 0.7.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #5
DrewM wrote:
You can use the toFixed() method to convert a Number object to a fixed
number of decimal places.

e.g.

(1.159).toFixed(2)

would return 1.16


And then wonder why you get errors for .007 ? Its well documented in
the c.l.j FAQ, http://www.jibbering.com/faq/#FAQ4_6
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/
Jul 23 '05 #6

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

Similar topics

2
by: SunMan | last post by:
Hello! I am trying to create a program that will ask for a user to enter a series of letters (codes) and then print out a table that shows the codes in decending frequency. Only letters will be...
1
by: Dalan | last post by:
I have tried both methods of using DSum and creating a Function to address summing some number columns, but to no avail. Since this has been a popular topic over the years, I'm sure I'll receive...
7
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
4
by: Lori2836 via AccessMonster.com | last post by:
I have a query where I am grouping by Fiscal Month, Summing # of Quotes, Summing # of Days.......and created an expression.........Avg Days: /. And I get the following error...
13
by: dpbsmith.janissary.2006 | last post by:
I know C++ mostly from "learning by doing." My main reference is Stoustrup's book. I was puzzled by something in a colleague's code that looked like this: abc=BOOL(def) I asked him what that...
3
by: kbeana | last post by:
I can't figure this out for the life of me. I have several fields that are set up as currency, and all have a decimal place of 2. I am summing some of them in the report footer, and it will be a...
2
by: Will | last post by:
Hi, I need to handle blank values in a query calculation. I have 636 records at the moment but when I sum over these records the blank fields are not returned. I have looked around here and on...
7
by: kpfunf | last post by:
Getting the following error opening a report, cannot figure out the cause: "You tried to execute a query that does not include the specified expression 'RQ_FuelQuoteReportHistory.Vendor' as part...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.