# very strange operation result

Discussion in 'Javascript' started by maori, Feb 8, 2008.

1. ### maoriGuest

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.

maori, Feb 8, 2008

2. ### HenryGuest

On Feb 8, 4:20 pm, maori wrote:
> 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.

Henry, Feb 8, 2008

3. ### timothytoeGuest

On Feb 8, 8:34 am, Henry <> wrote:
> On Feb 8, 4:20 pm, maori wrote:
>
> > 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.

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>

timothytoe, Feb 8, 2008
4. ### maoriGuest

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

"Henry" <> escribió en el mensaje
news:...
On Feb 8, 4:20 pm, maori wrote:
> 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.

maori, Feb 8, 2008
5. ### Evertjan.Guest

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.

--
Evertjan.
The Netherlands.

Evertjan., Feb 8, 2008
6. ### Doug MillerGuest

In article <Xns9A3EE689DEB6Aeejj99@194.109.133.242>, "Evertjan." <> wrote:
>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.

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.

Doug Miller, Feb 9, 2008
7. ### timothytoeGuest

On Feb 8, 4:24 pm, (Doug Miller) wrote:
> In article <Xns9A3EE689DEB6Aeej...@194.109.133.242>, "Evertjan." <> wrote:
>
> >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.

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

timothytoe, Feb 9, 2008
8. ### John W. KennedyGuest

Doug Miller wrote:
> In article <Xns9A3EE689DEB6Aeejj99@194.109.133.242>, "Evertjan." <> wrote:
>> 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.

> 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

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.

--
John W. Kennedy
If Bill Gates believes in "intelligent design", why can't he apply it to
Windows?

John W. Kennedy, Feb 9, 2008
9. ### VKGuest

On Feb 8, 7:46 pm, timothytoe <> wrote:
> 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

Secondly, it is not particular Javascript problem: it is true for any
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,
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
> 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>

VK, Feb 9, 2008
10. ### Dr J R StocktonGuest

In comp.lang.javascript message <7cede433-bdb0-4fae-b55d-da5d23d3e971@p6
9g2000hsa.googlegroups.com>, Sat, 9 Feb 2008 11:50:46, VK
<> posted:

>though I would argue with some rounding
>ideas in FAQ4_6

The code there is correct but outdated. For more recent code, go via

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
free, DOS/Win/UNIX, <URL:http://www.idiotsdelight.net/minitrue/> unsupported.

Dr J R Stockton, Feb 10, 2008
11. ### Dr J R StocktonGuest

In comp.lang.javascript message <4712391e-7650-4b89-bb93-b9df18202c7b@j2
0g2000hsi.googlegroups.com>, Sun, 10 Feb 2008 11:03:56, VK
<> posted:
>> >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 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.

--
(c) John Stockton, nr London UK. ??@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
In MS OE, choose Tools, Options, Send; select Plain Text for News and E-mail.
Don't quote more than is needed, and respond after each quoted part.

Dr J R Stockton, Feb 11, 2008
12. ### Guest

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.

On Feb 8, 2:37 pm, Randy Webb <> wrote:
> maori said the following on 2/8/2008 11:46 AM:
>
> Answer:It destroys the order of the conversation
> Question: Why?
> Question: Whats the most annoying thing on Usenet?
>
> > And, how can I deal with this in javascript, please?

>
>
> <URL:http://jibbering.com/faq/index.html#FAQ4_7>
>
> --
> Randy
> Chance Favors The Prepared Mind
> comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
> Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/

, Feb 11, 2008
13. ### Guest

Thanks Randy!

On Feb 11, 6:51 pm, Randy Webb <> wrote:
> 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

, Feb 12, 2008
14. ### Thomas 'PointedEars' LahnGuest

Dr J R Stockton wrote:
> 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
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>

Thomas 'PointedEars' Lahn, Feb 12, 2008
15. ### timothytoeGuest

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?

timothytoe, Feb 13, 2008
16. ### Evertjan.Guest

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?

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

> We

Are you plural, or childish?

> should add a "Why do people act childishly in clj?" to the FAQ.