Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   very strange operation result (http://www.velocityreviews.com/forums/t934605-very-strange-operation-result.html)

 maori 02-08-2008 04:20 PM

very strange operation result

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 02-08-2008 04:34 PM

Re: very strange operation result

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 02-08-2008 04:46 PM

Re: very strange operation result

On Feb 8, 8:34 am, Henry <rcornf...@raindrop.co.uk> 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 02-08-2008 04:46 PM

Re: very strange operation result

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

"Henry" <rcornford@raindrop.co.uk> escribió en el mensaje
news:b94412b3-85ca-49d6-b3fb-86387322d6b4@d4g2000prg.googlegroups.com...
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. 02-08-2008 09:39 PM

Re: very strange operation result

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 02-09-2008 12:24 AM

Re: very strange operation result

In article <Xns9A3EE689DEB6Aeejj99@194.109.133.242>, "Evertjan." <exjxw.hannivoort@interxnl.net> 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 02-09-2008 01:14 AM

Re: very strange operation result

On Feb 8, 4:24 pm, spamb...@milmac.com (Doug Miller) wrote:
> In article <Xns9A3EE689DEB6Aeej...@194.109.133.242>, "Evertjan." <exjxw.hannivo...@interxnl.net> 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 02-09-2008 07:15 PM

Re: very strange operation result

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

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

 VK 02-09-2008 07:50 PM

Re: very strange operation result

On Feb 8, 7:46 pm, timothytoe <timothy...@gmail.com> 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
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
> 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 02-10-2008 05:06 PM

Re: very strange operation result

In comp.lang.javascript message <7cede433-bdb0-4fae-b55d-da5d23d3e971@p6
9g2000hsa.googlegroups.com>, Sat, 9 Feb 2008 11:50:46, VK
<schools_ring@yahoo.com> 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 first link.

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

All times are GMT. The time now is 04:32 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.