floating point numbers question

B

Benedikt Wismans

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
 
L

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
 
D

Douglas Crockford

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
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in Benedikt Wismans
<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.
 
R

Richard Cornford

message
... . 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.
 
V

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.
 
L

Lasse Reichstein Nielsen

VK said:
// 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,099
Messages
2,570,626
Members
47,237
Latest member
David123

Latest Threads

Top