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

multiplication weirdness

Hello -

I thought I was reasonably versed in JavaScript, buuuuut...

When I do the following:

x = parseFloat(326.655);
y = parseFloat(100);
z = x * y;
alert(z);

When I should get 32665.5 I instead get 32665.499999999996.

Why is that?

Thanks for any help.

Tom

Nov 9 '05 #1
7 1893
harborcoat-design wrote:
Hello -

I thought I was reasonably versed in JavaScript, buuuuut...

When I do the following:

x = parseFloat(326.655);
y = parseFloat(100);
z = x * y;
alert(z);

When I should get 32665.5 I instead get 32665.499999999996.

Why is that?

Thanks for any help.

Tom


Past postings might help:

http://groups.google.com/group/comp....5f3e157c202fd1

http://groups.google.com/group/comp....d36142924ccc21

Nov 9 '05 #2
harborcoat-design wrote on 09 nov 2005 in comp.lang.javascript:
I thought I was reasonably versed in JavaScript, buuuuut...
Seems you are not, sorry.
When I do the following:

x = parseFloat(326.655);
y = parseFloat(100);
z = x * y;
alert(z);
The same goes for:

x = 326.655;
y = 100;
z = x * y;
alert(z);

So why the parseFloat()?
When I should get 32665.5 I instead get 32665.499999999996.
No, you should not.
Why is that?


Javascript stores floating point values in a binary format,
and this is what is the nearest result of your multiplication
in binary floating point outputted in a decimal format.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Nov 9 '05 #3
On 09/11/2005 18:01, harborcoat-design wrote:
I thought I was reasonably versed in JavaScript, buuuuut...
[snip]
x = parseFloat(326.655);
y = parseFloat(100);
Using the parseFloat function here is indeed something to question. The
parseFloat function operates on string values, and should only be
necessary for stripping off non-digit characters; use unary plus (+)
when you know the value is a properly formatted number.

[snip]
When I should get 32665.5 I instead get 32665.499999999996.


Reading the FAQ really can save you, and others, time.

Mike
c.l.javascript FAQ: <http://www.jibbering.com/faq/>

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Nov 9 '05 #4
Seems you are not, sorry.
That's okay - I'm man enough to take my medicine.
Javascript stores floating point values in a binary format,
and this is what is the nearest result of your multiplication
in binary floating point outputted in a decimal format.
Yes - I see - Thanks for the reply.
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)


Nov 9 '05 #5
Michael Winter wrote on 09 nov 2005 in comp.lang.javascript:
use unary plus (+)
when you know the value is a properly formatted number.


Not neccessary in this case with a following multiplication.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Nov 9 '05 #6
On 09/11/2005 18:24, Evertjan. wrote:
Michael Winter wrote on 09 nov 2005 in comp.lang.javascript:
use unary plus (+) when you know the value is a properly formatted
number.


Not neccessary in this case with a following multiplication.


I was referring to type-conversion in general, not the OP's specific
case. No conversion was necessary at all, there.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Nov 9 '05 #7
JRS: In article <11*********************@z14g2000cwz.googlegroups. com>,
dated Wed, 9 Nov 2005 10:01:45, seen in news:comp.lang.javascript,
harborcoat-design <tj******@gmail.com> posted :
I thought I was reasonably versed in JavaScript, buuuuut...
Without reading the newsgroup FAQ, as posted every Mon & Fri ??
When I do the following:

x = parseFloat(326.655);
y = parseFloat(100);
z = x * y;
alert(z);

When I should get 32665.5 I instead get 32665.499999999996.

Why is that?


After the FAQ, read <URL:http://www.merlyn.demon.co.uk/js-maths.htm> and
links.

Note that 100 will be stored exactly, but it is not a power of 2.
326.655 cannot be stored exactly, so multiplication by 100 loses some
bits of the ideal result, giving something that outputs as you see.

But 0.01 cannot be stored exactly, so dividing by 0.01 will not
*necessarily* give the same result, although in this case it does.

AFAICS, parseFloat is worth using only when its parameter can be a
string with other characters after the number.

--
© 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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Nov 9 '05 #8

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

Similar topics

54
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This...
9
by: Ralf Hildebrandt | last post by:
Hi all! First of all: I am a C-newbie. I have noticed a "strange" behavior with the standart integer multiplication. The code is: void main(void)
87
by: Vijay Kumar R Zanvar | last post by:
Hi, Why multiplication of pointers is not allowed? Till now I only know this, but not the reason why! PS: As a rule, I searched the FAQ, but could not find an answer. -- Vijay Kumar R...
17
by: Christopher Dyken | last post by:
Hi group, I'm trying to implement two routines to handle 32x32-bits and 64x64-bits signed integer multiplication on a 32 bits machine in C. It easy to find descriptions of non-signed...
5
by: David Thielen | last post by:
Hi; I am creating png files in my ASP .NET app. When I am running under Windows 2003/IIS 6, the file is not given the security permissions it should have. It does not have any permission for...
5
by: Paul McGuire | last post by:
Does Python's run-time do any optimization of multiplication operations, like it does for boolean short-cutting? That is, for a product a*b, is there any shortcutting of (potentially expensive)...
11
by: mjdeesh_hi | last post by:
How can we perfom multiplication programatically without using + or * operator. Can any one help out in this one. Jagadeesh.
2
by: JYA | last post by:
Hi. I was writing an xmltv parser using python when I faced some weirdness that I couldn't explain. What I'm doing, is read an xml file, create another dom object and copy the element from...
1
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void...
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.