473,405 Members | 2,373 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,405 software developers and data experts.

floating point numbers question

dear group!

<script language="javascript">
var a = 60381.11;
var b = 1437261.58;
var c = a + b;
alert (c); -> 1497642.6900000002

var a = 60381.11;
var b = 1437261.50;
var c = a + b;
alert (c); -> 1497642.61

</script>

can anybody help me with this? Obviously some kind of precision
problem, but those numbers are not big at all. Any ideas where to find
documentation of internal data representation in js? Any hint is
welcome.
Benedikt
Jul 20 '05 #1
8 5734
> var a = 60381.11;
var b = 1437261.58;
var c = a + b;
alert (c); // 1497642.6900000002 can anybody help me with this? Obviously some kind of precision
problem, but those numbers are not big at all. Any ideas where to find
documentation of internal data representation in js?


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 weakness. 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 do not 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://docs.sun.com/db/doc/800-7895/6hos0aoua?a=view

Jul 20 '05 #2
Lee
Benedikt Wismans said:

dear group!

<script language="javascript">
var a = 60381.11;
var b = 1437261.58;
var c = a + b;
alert (c); -> 1497642.6900000002

var a = 60381.11;
var b = 1437261.50;
var c = a + b;
alert (c); -> 1497642.61

</script>

can anybody help me with this? Obviously some kind of precision
problem, but those numbers are not big at all. Any ideas where to find
documentation of internal data representation in js? Any hint is
welcome.


http://www.jibbering.com/faq/#FAQ4_7

Jul 20 '05 #3
"Benedikt Wismans" <be**************@wiai.uni-bamberg.de> wrote in
message news:f9**************************@posting.google.c om...
<snip>
... . Any ideas where to find documentation of internal
data representation in js? Any hint is welcome.


ANSI/IEEE Std 754-1985: IEEE Standard for Binary
Floating-Point Arithmetic. Institute of Electrical
and Electronic Engineers, New York (1985).

Richard.
Jul 20 '05 #4
VK
Try this:
....
// case 1
var a = 60381.11;
var b = 1437261.58;
var c = a + b;
alert (c); //-> 1497642.6900000002

// case 2
var a = 60381.11;
var b = 1437261.50;
var c = a + b;
alert (c); //-> 1497642.61 !

// case 3
a = 60381.11;
b = 1437261.58;
c = a + b;
alert (c); //-> 1497642.6900000002 again!

// case 4
var d = 60381.11;
var e = 1437261.50;
var f = a + b;
alert (f); //-> 1497642.6900000002 !
....

So it's not so much problem of floating point, but how JavaScript allocates
new variables (case 1 and 4), reinitializes existing variables (case 2, it
evidently makes some kind of auto-rounding or hell knows what), assign new
value to existing variables (case 3)

Never noticed that before... Very interesting...
Any way, if you have to work with floating numbers, make a paper sticker for
yourself:
"(0 == -0) = false"
and place it near of your screen as a reminder that all math on the computer
is approximate and it's never equal to the real value, only up to some
point. This is why for example 0 never equal -0, unless you are using
special tricks and/or libraries.
Benedikt Wismans <be**************@wiai.uni-bamberg.de> wrote in message
news:f9**************************@posting.google.c om...
dear group!

<script language="javascript">
var a = 60381.11;
var b = 1437261.58;
var c = a + b;
alert (c); -> 1497642.6900000002

var a = 60381.11;
var b = 1437261.50;
var c = a + b;
alert (c); -> 1497642.61

</script>

can anybody help me with this? Obviously some kind of precision
problem, but those numbers are not big at all. Any ideas where to find
documentation of internal data representation in js? Any hint is
welcome.
Benedikt

Jul 20 '05 #5
"VK" <sc**********@yahoo.com> writes:
// case 4
var d = 60381.11;
var e = 1437261.50;
var f = a + b;
do you mean "d + e"?
So it's not so much problem of floating point,
Yes it is :).
Never noticed that before... Very interesting...
Any way, if you have to work with floating numbers, make a paper sticker for
yourself:
"(0 == -0) = false"


That would be confuzing, since
alert(0 == -0)
alerts "true". But yes, they have different internal representations.
There are also several million different versions of NaN in IEEE
floating point numbers, but Javascript/ECMAScript makes them all
equal.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6
JRS: In article <f9**************************@posting.google.com >, seen
in news:comp.lang.javascript, Benedikt Wismans
<be**************@wiai.uni-bamberg.de> posted at Mon, 22 Sep 2003
14:55:48 :-
<script language="javascript">
var a = 60381.11;
var b = 1437261.58;
var c = a + b;
alert (c); -> 1497642.6900000002

var a = 60381.11;
var b = 1437261.50;
var c = a + b;
alert (c); -> 1497642.61

</script>

can anybody help me with this? Obviously some kind of precision
problem, but those numbers are not big at all. Any ideas where to find
documentation of internal data representation in js? Any hint is
welcome.


See the Mon/Fri newsgroup FAQ, and js-maths.htm on my site, among
numerous others.

--
© 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 #7
JRS: In article <bk**********@titan.btinternet.com>, seen in
news:comp.lang.javascript, Richard Cornford
<ri*****@litotes.demon.co.uk> posted at Mon, 22 Sep 2003 23:20:39 :-
"Benedikt Wismans" <be**************@wiai.uni-bamberg.de> wrote in
message news:f9**************************@posting.google.c om...
<snip>
... . Any ideas where to find documentation of internal
data representation in js? Any hint is welcome.


ANSI/IEEE Std 754-1985: IEEE Standard for Binary
Floating-Point Arithmetic. Institute of Electrical
and Electronic Engineers, New York (1985).


<URL:http://www.merlyn.demon.co.uk/js-maths.htm#Float>
<URL:http://www.merlyn.demon.co.uk/pas-real.htm>
<URL:http://www.merlyn.demon.co.uk/pas-type.htm>
are on-line, containing information and links.

Present javascript numbers are IEEE Doubles, and so have one sign bit,
51 magnitude bits with an additional implicit 1, and 11 exponent bits.

Any integer from 0 up to 2^53 = 9,007,199,254,740,992 in magnitude
should be stored exactly. The range slightly exceeds +-1.7×10^308.

--
© 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 #8
For a simpler demonstration:

alert(2-1.1);

This is also a problem in Java and databases. Solutions include:
-doing your math using whole numbers and remembering where you want
the decimal point just for formatting
-BigInteger, BigDecimal
-Currency datatype
Jul 20 '05 #9

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

Similar topics

17
by: Gernot Frisch | last post by:
Hi, does repeatingly doing this: float num = GetRandomFloat(); for(;;) { float random = GetRandomFloat(); num*=random; num/=random;
4
by: Dave | last post by:
Hi folks, I am trying to develop a routine that will handle sphere-sphere and sphere-triangle collisions and interactions. My aim is to develop a quake style collision engine where a player can...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
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 have been unable to get it to work perfectly due to...
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
13
by: Chris Stankevitz | last post by:
Hi, I have a very large Visual c++ .net 2003 7.1 native c application (approximately 500,000 lines of code). This application is a simulation that frequently works with floating point numbers....
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; return ( diff <= epsilon ) && ( diff >= -epsilon );...
33
by: dis_is_eagle | last post by:
hi....i have encountered strange problem regarding floating point comparison...the problem is... main() { float a=0.7; if(0.7 a) printf("hi"); else printf("hello");
15
by: arnuld | last post by:
Next month I will start to work on a C++ based Software named CAT++ which is going to provide FORTRAN like arrays in C++ and will be used within Scientific Community and hence will heavily depend...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.