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

Incorrect math

alert( 100 * 1.15 );

The above *should* show a value of 115. However, what I
am actually getting is:

114.99999999999998

alert( 100 * 1.10 );

is showing as:

110.00000000000001

Why?

thnx,
Christoph
Jul 20 '05 #1
5 2072
> alert( 100 * 1.15 );

The above *should* show a value of 115. However, what I
am actually getting is:

114.99999999999998

alert( 100 * 1.10 );

is showing as:

110.00000000000001

Why?


JavaScript numbers are 64-bit floating point as specified in IEEE 754. It is the
same representation that Java calls Double.

The scheme is intended to preserve as much precision as possible, but it has
some significant weaknesses. For example, it is not able to exactly represent
common fractions such as 1/10 or 1/100. It can at best approximate them. As you
can see, with a single operation you begin accumulating noticeable error. Also,
be aware that the associative law and distributive law no longer apply.

I think floating point is the wrong representation for numbers in most
applications. However, it is extremely popular, and popularity is much more
important these days than suitability or reliability. I recommend that in
applications that require exactness (for example, when working with money) scale
your values by a suitable factor (such as 100) so that all of your arithmetic
will be on whole numbers, which are exact.

See http://www.dcs.ed.ac.uk/home/mhe/plume/node9.html

Jul 20 '05 #2
"Christoph" wrote on 11/11/2003:
alert( 100 * 1.15 );

The above *should* show a value of 115. However, what I
am actually getting is:

114.99999999999998

alert( 100 * 1.10 );

is showing as:

110.00000000000001

Why?


Floating-point values are traditionally represented in a structure
with three parts: a mantissa, an exponent, and the sign. Each field
has a fixed size. Immediately, that suggests limits. Furthermore,
there are certain types of numbers that are difficult to represent
accurately. Basically, there are flaws, and there are plenty of
places to read about them on the Internet. This might not be quite so
true of JavaScript as it is with, say the FPU on a PC, but it's at
least a possible explanation.

Because of this, it's usually not a good idea to do direct
comparisons, and use ranges instead. Alternatively, round floating
point values at certain points in the computation or before
comparisons.

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
Jul 20 '05 #3
Christoph hu kiteb:
alert( 100 * 1.15 );

The above *should* show a value of 115. However, what I
am actually getting is:

114.99999999999998

alert( 100 * 1.10 );

is showing as:

110.00000000000001

Why?


Others have said why. The solutions are:

Use whole numbers wherever possible

Use Math.round() or Math.floor() immediately after any calculation
involving fractions.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #4
Christoph wrote:
alert( 100 * 1.15 );

The above *should* show a value of 115. However, what I
am actually getting is:

114.99999999999998

alert( 100 * 1.10 );

is showing as:

110.00000000000001

Why?

thnx,
Christoph

As subsequent posts noted, fractional arithmetic is problematic in
floating point. Numbers which are finite in one base may be 'repeating'
decimals in another. For the same reason you can't represent 1/3 exactly
in base 10 as a fraction, other numbers which are exact in base 10 may
be repeating in base 2.

Either scaling the numbers in order to use only integer values or
rounding the result will help.

For example, in your example, the most precise value you have is
represented to two decimal places. If you round the answer to two
decimal places, you get the 'correct' answer.

Jul 20 '05 #5
JRS: In article <C7******************@monger.newsread.com>, seen in
news:comp.lang.javascript, Christoph <jc*****@yahoo.com> posted at Tue,
11 Nov 2003 21:02:26 :-
alert( 100 * 1.15 );

The above *should* show a value of 115. However, what I
am actually getting is:

114.99999999999998
...


One should always seek to read a newsgroup's FAQ before posting; it
saves everybody a lot of trouble, the putative poster included. This is
item 4.7 in ours.

But read the whole FAQ, to avoid future problems; it points out,
moreover, that comp.lang.java groups should not be used for javascript.
In fact, AIUI, c.l.j.j has no legitimate existence. FU set.

When questions in the FAQ are asked, the FAQ should be cited in answers
even if it is felt that more explanation should be given in the case in
question.

--
© 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> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #6

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

Similar topics

0
by: Jussi Mononen | last post by:
Hi, I'm having problems to successfully execute the test scripts on a Compaq host ( OSF1 tr51bdev V5.1 2650 alpha ). Almost all tests end up with the following error message "PARI: *** ...
13
by: Sugapablo | last post by:
Here's a wierd one... I have a simple loop that adds values. It seems that after thousands of instances using this loop with no problems whatsoever, when the loop tries the following, it...
17
by: cwdjrxyz | last post by:
Javascript has a very small math function list. However there is no reason that this list can not be extended greatly. Speed is not an issue, unless you nest complicated calculations several levels...
13
by: Yogesh Khanolkar | last post by:
Hi, I am getting incorrect o/p from the code below: #include<stdio.h> #include<float.h> #include<limits.h> main() { double val,val1;
0
by: Rachel Suddeth | last post by:
I am using GetScrollRange to set a "preferred width" for a UserControl that contains a RichTextBox. I think this should return the smallest width such that no horizontal scroll will be needed. Most...
110
by: Gregory Pietsch | last post by:
I'm writing a portable implementation of the C standard library for http://www.clc-wiki.net and I was wondering if someone could check the functions in math.h for sanity/portability/whatever. I'm...
11
by: Sambo | last post by:
I have the following module: ------------------------------- import math def ac_add_a_ph( amp1, ph1, amp2, ph2 ): amp3 = 0.0 ph3 = 0.0 ac1 = ( 0, 0j ) ac2 = ( 0, 0j )
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
0
by: kirby.urner | last post by:
Cyber-curricula have a leveling aspect, as kids nearer Katrina's epicenter tune in and bliss out on 'Warriors of the Net' (why wait for stupid big dummy textbooks to catch up?). They feel more...
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: 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.