473,325 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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.
Feb 8 '08 #1
10 1290
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.

Feb 8 '08 #2
And, how can I deal with this in javascript, please?
"Henry" <rc*******@raindrop.co.ukescribió en el mensaje
news:b9**********************************@d4g2000p rg.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.
Feb 8 '08 #3
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)
Feb 8 '08 #4
In article <Xn********************@194.109.133.242>, "Evertjan." <ex**************@interxnl.netwrote:
>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.
Feb 9 '08 #5
On Feb 8, 4:24 pm, spamb...@milmac.com (Doug Miller) wrote:
In article <Xns9A3EE689DEB6Aeej...@194.109.133.242>, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
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.
Feb 9 '08 #6
In comp.lang.javascript message <7cede433-bdb0-4fae-b55d-da5d23d3e971@p6
9g2000hsa.googlegroups.com>, Sat, 9 Feb 2008 11:50:46, VK
<sc**********@yahoo.composted:
>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.
Feb 10 '08 #7
mk*****@yahoo.com 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/bestpractices/
Feb 12 '08 #8

Thanks Randy!

On Feb 11, 6:51 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
mk83...@yahoo.com 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 '08 #9
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?
Feb 13 '08 #10
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"?
We
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?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 13 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Allcomp | last post by:
Hello, I have seen something really strange in VB6 If I do a Int ( (5 * 1.2)) , I receive the value 5, but I should receive 6? Is this a bug or something really "normal". I can see that if I...
8
by: Harvey Twyman | last post by:
I have code written under the CCS 'C' Compiler to run on a PIC microcontroller. Code Extract: ------------------------------- char a,b,c; ------------------------------- c = ( a == b );...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
2
by: Chris | last post by:
Hi, a strange behaviour when working with exceptions : when I divide and integer by 0 will an exception be thrown. OK but, when I divide a double by 0 is no exception thrown ??? How come ? ...
11
by: Ben Blank | last post by:
I have a loop which iterates over an array in a particular order: for (j = 0; j < 16; j++) T = ...; The loop proceeds normally for j = 0 through j = 4, but gives an IndexOutOfRangeException at...
6
by: Lost Student | last post by:
Hello guys Any help that you can offer would be greatly appreciated. I am nearing the end of a C++ class and am so lost. The following is my assignment and what I have so far. Please help ...
2
by: peter | last post by:
Hi, I have very strange situation but first description ;) I have: 1) project in VB.NET, in this f.e. 1 function: Public Function Login(ByVal UserName As String, ByVal UserPassword As...
10
by: kyagrd | last post by:
<code> #include <iostream> int main(void) { using namespace std; int p; int* p1 = p; int* p11 = p + 2;
2
by: Spam Catcher | last post by:
Anyone know what this error means? The Undo operation encountered a context that is different from what was applied in the corresponding Set operation. The possible cause is that a context was...
9
by: Boltar | last post by:
On 32 bit linux with gcc 4.2 I get unexpected results with this code: main() { int bits = 32; printf("%d\n",(int)1 << (int)32); printf("%d\n",(int)1 << bits); }
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.