very strange operation result

M

maori

You can try this in javascript:

A) alert( 1.9 * 50 ); = 95
B) alert( 2.9 * 50 ); = 145
C) alert( 3.9 * 50 ); = 195
D) alert( 4.9 * 50 ); = 245.00000000000003
E) alert( 5.9 * 50 ); = 295

Why D) = 245.00000000000003 ?

Thanks,
Maori.
 
H

Henry

You can try this in javascript:

A) alert( 1.9 * 50 );    =   95
B) alert( 2.9 * 50 );    =   145
C) alert( 3.9 * 50 );    =   195
D) alert( 4.9 * 50 );    =   245.00000000000003
E) alert( 5.9 * 50 );    =   295

Why D)  = 245.00000000000003 ?

Javascript's only numeric data type is an IEEE double precision
floating point number. That type of number can represent many numbers
precisely, and others it can only approximate. You have encountered
one of the numbers that can only be approximated.

This should not be an unexpected feature as it is common in systems
for representing numbers. For example, a decimal representation of 1
divided by 3 is unlikely to be precise (because it would be infinitely
long), while the same number can be precisely represented in base 3 as
0.1.
 
T

timothytoe

Javascript's only numeric data type is an IEEE double precision
floating point number. That type of number can represent many numbers
precisely, and others it can only approximate. You have encountered
one of the numbers that can only be approximated.

This should not be an unexpected feature as it is common in systems
for representing numbers. For example, a decimal representation of 1
divided by 3 is unlikely to be precise (because it would be infinitely
long), while the same number can be precisely represented in base 3 as
0.1.

What you found did not surprise me. I've seen that kind of things for
decades. The only system I can remember that didn't have that problem
was the Atari 8-bit, which turned on the binary coded decimal bit in
the 6502 for Atari BASIC.

However, I've come across the problem in JavaScript even when
multiplying and dividing integers. To me, that seems REALLY weird. For
example, look where I had to use Math.round. I assume it's the
division that screws me up. Without the Math.round, I get fractional
turds...

<pre>
//------------------------------------------------------------------------------------------------------------
// combinations(n,k)
// returns the combinations of k objects from a set of n objects
combinations: function(n,k) {
function factorial(a) {
return (a<2) ? 1 : a*factorial(a-1);
}
return Math.round(factorial(n)/(factorial(k)*factorial(n-k)));
},
</pre>
 
M

maori

And, how can I deal with this in javascript, please?


"Henry" <[email protected]> escribió en el mensaje
You can try this in javascript:

A) alert( 1.9 * 50 ); = 95
B) alert( 2.9 * 50 ); = 145
C) alert( 3.9 * 50 ); = 195
D) alert( 4.9 * 50 ); = 245.00000000000003
E) alert( 5.9 * 50 ); = 295

Why D) = 245.00000000000003 ?

Javascript's only numeric data type is an IEEE double precision
floating point number. That type of number can represent many numbers
precisely, and others it can only approximate. You have encountered
one of the numbers that can only be approximated.

This should not be an unexpected feature as it is common in systems
for representing numbers. For example, a decimal representation of 1
divided by 3 is unlikely to be precise (because it would be infinitely
long), while the same number can be precisely represented in base 3 as
0.1.
 
E

Evertjan.

timothytoe wrote on 08 feb 2008 in comp.lang.javascript:
What you found did not surprise me. I've seen that kind of things for
decades. The only system I can remember that didn't have that problem
was the Atari 8-bit, which turned on the binary coded decimal bit in
the 6502 for Atari BASIC.


Central Data Basic for the Signetics/Philips 2650 microprosessor,
rumoured to have been written by a youth called William Gates,
usded BCD as it's standard Math engine.

And there were more:

<http://en.wikipedia.org/wiki/Binary-coded_decimal>

It is stil used in financial programming, in the code most radio clocks in
Europe receive from the Frankfurt long wave transmitter, and many other
applocations.
 
D

Doug Miller

timothytoe wrote on 08 feb 2008 in comp.lang.javascript:

Pfui. IBM mainframes -- System/360 and its descendants (S/370, 309x, 40xx,
etc.) -- have had true decimal arithmetic at the hardware level for more than
forty years.
 
T

timothytoe

Pfui. IBM mainframes -- System/360 and its descendants (S/370, 309x, 40xx,
etc.) -- have had true decimal arithmetic at the hardware level for more than
forty years.

--
Regards,
Doug Miller (alphageek at milmac dot com)

It's time to throw all their damned tea in the harbor again.

I didn't say the Atari was the only one. I said it was the only one I
remembered.
 
J

John W. Kennedy

Pfui. IBM mainframes -- System/360 and its descendants (S/370, 309x, 40xx,
etc.) -- have had true decimal arithmetic at the hardware level for more than
forty years.

And the latest round of the z/Architecture (the current descendant), has
added the new IEEE-754r decimal floating point, which Intel,
unfortunately, seems to be resisting, though languages are working to
add it.

Before the S/360, most of IBM's product line -- the 650/707x series, the
702/705/7080 series, the 1401/1440/1460 series, the 1410/7010 series,
the 350, and the 1620/1710 -- used decimal arithmetic /exclusively/,
even for addressing. Only the 701/704/709/704x/709x series and the 7030
supercomputer used binary. Even the one-off NORC supercomputer used
decimal.
 
V

VK

However, I've come across the problem in JavaScript even when
multiplying and dividing integers. To me, that seems REALLY weird.

First of all, you've come across the problem in dividing integers, not
in multiplying them. Multiplication doesn't change the dyadicity of
decadic integers.

Secondly, it is not particular Javascript problem: it is true for any
machine using internal dyadic numbers while getting/outputting decadic
numbers, that means for any up to moment existing or ever existed
discrete computer. This is why it is always amusing to read some
"sweet memory" references of the kind "and model X did not have
it" :)
It is just always the programming choice what to implement: to make
the output "as expected by an average user" suffering for some
precision lost, or to output "as it really is but may be unexpected
for an average user". Javascript uses the second approach, some
environment might/may offer the first one.

Basically decadic rationals is a rather small subset of real numbers,
and decadic rationals being representable as dyadic rationals as well
are forming even much more smaller subset of the first set (dyadic
solenoid). Vulgarly speaking, only some decadic numbers can be exactly
represented as a finite sequence in decimal system, and out from them
only some can be represented as a finite sequence in binary system.

The overall math defining all relations may be rather involving but I
can post it here. From the practical point of view
http://www.jibbering.com/faq/index.html#FAQ4_6
and
http://www.jibbering.com/faq/index.html#FAQ4_7
do provide enough of info though I would argue with some rounding
ideas in FAQ4_6



For
 
D

Dr J R Stockton

In comp.lang.javascript message <7cede433-bdb0-4fae-b55d-da5d23d3e971@p6
9g2000hsa.googlegroups.com>, Sat, 9 Feb 2008 11:50:46, VK
though I would argue with some rounding
ideas in FAQ4_6

The code there is correct but outdated. For more recent code, go via
the first link.
 
D

Dr J R Stockton

In comp.lang.javascript message <4712391e-7650-4b89-bb93-b9df18202c7b@j2
0g2000hsi.googlegroups.com>, Sun, 10 Feb 2008 11:03:56, VK
The code is just fine as a program entity.

In the FAQ, it is not. It is slower than necessary, for example.
I do not agree - as I said a year or so ago - that it prepostulates
exactly one rounding algorithm out of possible and respectively call
any other possible rounding algorithm as a bug.

There must be a meaning to that. Have you considered learning to write
unambiguous English? I believe India is still a good place to learn it.
For the absolute
majority of users the differences are irrelevant yet it should be
stated in the FAQ the chosen rounding algorithm thus the outcome.
Simply stating "according to ECMAScript 3rd ed." doesn't tell too
much, neither holds some big importance in the domain of question:
Ecmascript International is not an IEEE authoritative member.

The IEEE, of course, is merely a national body, though it has members
from various countries. Its authority is only local, though its
abilities can be more widely respected. Its preferences can be
incompatible with the agreed international ones.

One has told the FAQ maintainer about the superior authority of ISO/IEC
16262.


Bankers' Rounding of IEEE Doubles is probably too awkward for the FAQ.

A search at ISO for "Language independent arithmetic" reveals standards,
but costing too many CHF.

@hotpop failed.
 
M

mk834tt

Randy seems to have a low threshold... or he's bored.
Is there anyone out there that has escaped him?
Said on top for Randy. :)
 
M

mk834tt

Thanks Randy!

(e-mail address removed) said the following on 2/11/2008 6:16 PM:
Randy seems to have a low threshold... or he's bored.
Neither.

Is there anyone out there that has escaped him?

Yes. The many that use a little intelligence to solve a problem rather
than wanting someone to spoon feed them.
Said on top for Randy. :)

That's nice[1].

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractic
 
T

Thomas 'PointedEars' Lahn

Dr said:
One has told the FAQ maintainer about the superior authority of ISO/IEC
16262.

And one has been disproved already. It is only that one's assumed
superiourity prevents one from recognizing that.


PointedEars
 
T

timothytoe

What is it about comp.lang.javascript that makes people behave as if
they were in 2nd grade? We should add a "Why do people act childishly
in clj?" to the FAQ.

How old are you people?
 
E

Evertjan.

timothytoe wrote on 13 feb 2008 in comp.lang.javascript:
What is it about comp.lang.javascript that makes people behave as if
they were in 2nd grade?

You must be first grade, we are only second grade.

Or are you referring to some local school system, unknown to many of "us"?

Are you plural, or childish?
should add a "Why do people act childishly in clj?" to the FAQ.

Perhaps you can give us some first grade advice about Javascript?
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top