473,785 Members | 2,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.00000000000 003
E) alert( 5.9 * 50 ); = 295

Why D) = 245.00000000000 003 ?

Thanks,
Maori.
Feb 8 '08 #1
10 1319
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.00000000000 003
E) alert( 5.9 * 50 ); * *= * 295

Why D) *= 245.00000000000 003 ?
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*******@rain drop.co.ukescri bió en el mensaje
news:b9******** *************** ***********@d4g 2000prg.googleg roups.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.00000000000 003
E) alert( 5.9 * 50 ); = 295

Why D) = 245.00000000000 003 ?
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.javas cript:
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.or g/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.10 9.133.242>, "Evertjan." <ex************ **@interxnl.net wrote:
>timothytoe wrote on 08 feb 2008 in comp.lang.javas cript:
>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 <Xns9A3EE689DEB 6Aeej...@194.10 9.133.242>, "Evertjan." <exjxw.hannivo. ..@interxnl.net wrote:
timothytoe wrote on 08 feb 2008 in comp.lang.javas cript:
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.javas cript message <7cede433-bdb0-4fae-b55d-da5d23d3e971@p6
9g2000hsa.googl egroups.com>, Sat, 9 Feb 2008 11:50:46, VK
<sc**********@y ahoo.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.demo n.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.idiotsdelig ht.net/minitrue/unsupported.
Feb 10 '08 #7
mk*****@yahoo.c om 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.javas cript 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.c om 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.javas cript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptT oolbox.com/bestpractic
Feb 12 '08 #9
What is it about comp.lang.javas cript 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

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

Similar topics

14
6351
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 do ? int ((5 * 1.2 + 0.0000000000000003)) I receive 6. If I add something smaller, I have 5 as a result What is strange is that If I do a ? 5*1.2, I receive 6 so (5 * 1.2)
8
2239
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
6651
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 variable becomes 364 and the other one becomes 365. Does anyone have any insight to what the problem is? Thanks in advance. Bjørn
2
1340
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 ? Try it out.
11
2668
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 j = 5. Playing with the watch window seems to indicate that the modulo is being completely ignored: j = 0x5 (int) T = error: index '4-j%4' out of bound for pointer/array 'T'
6
1678
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 Define a class called Point which contains the coordinates (x, y) of a point in a plane, where both x and y are two integers (positive or negative whole numbers), i.e. the Point class contains two private
2
4114
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 String, Optional ByVal ConnectionParamList As String = Nothing) As String
10
3503
by: kyagrd | last post by:
<code> #include <iostream> int main(void) { using namespace std; int p; int* p1 = p; int* p11 = p + 2;
2
3861
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 Set on the thread and not reverted(undone). It seems to pop up when I use nSoftware's IPDaemon...
9
2257
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
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.