Velocity Reviews > very strange operation result

# very strange operation result

maori
Guest
Posts: n/a

 02-08-2008
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.

Henry
Guest
Posts: n/a

 02-08-2008
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.

timothytoe
Guest
Posts: n/a

 02-08-2008
On Feb 8, 8:34 am, Henry <(E-Mail Removed)> 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>

maori
Guest
Posts: n/a

 02-08-2008
And, how can I deal with this in javascript, please?

"Henry" <(E-Mail Removed)> escribió en el mensaje
news:(E-Mail Removed)...
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.

Evertjan.
Guest
Posts: n/a

 02-08-2008
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.
(Please change the x'es to dots in my emailaddress)

Doug Miller
Guest
Posts: n/a

 02-09-2008
In article <Xns9A3EE689DEB6Aeejj99@194.109.133.242>, "Evertjan." <(E-Mail Removed)> 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.

timothytoe
Guest
Posts: n/a

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

John W. Kennedy
Guest
Posts: n/a

 02-09-2008
Doug Miller wrote:
> In article <Xns9A3EE689DEB6Aeejj99@194.109.133.242>, "Evertjan." <(E-Mail Removed)> 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?

VK
Guest
Posts: n/a

 02-09-2008
On Feb 8, 7:46 pm, timothytoe <(E-Mail Removed)> 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
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
> 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>

Dr J R Stockton
Guest
Posts: n/a

 02-10-2008
In comp.lang.javascript message <7cede433-bdb0-4fae-b55d-da5d23d3e971@p6
9g2000hsa.googlegroups.com>, Sat, 9 Feb 2008 11:50:46, VK
<(E-Mail Removed)> 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.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Michael Tan Ruby 32 07-21-2005 03:23 PM shanx__=|;- C Programming 19 10-19-2004 03:55 PM Abhishek Jha C Programming 4 10-17-2004 08:19 AM Thomas Reed Computer Support 7 04-09-2004 08:09 PM Raymond Arthur St. Marie II of III Python 4 07-27-2003 12:09 AM