Weird problem ( 3 * 31.9 = 95.69999999999999)

B

baong

dear all
i have use this line to time in many my web base appl.
but now i found a weird problem
the javascript line is the same
but i use the calculation 2 time
@ the first time, it is ok, 3 * 31.9 = 95.7
@ the second time, 3 * 31.9 = 95.69999999999999
this is the list number which calculation is NOT correct
6 * 31.9 = 191.39999999999998
7
9
12
14
18
23
24
28
29
This is the line, i use to calculation
Qu = document.form1[it].value ;
Har = document.form1[it+1].value ;
Jum = Qu * Har ;

Thank u b4

Budi
 
M

matty

which browser and computer are you using? I tried IE and Firefox and I
have 95.69999999999 all the time.

You are mixing floating point and integer, so floating point takes
precedence (you should do 3.0 * 31.9 to be syntaxically correct). So
all results that look like .999999 are actually the correct results as
well as the 95.7, it depends on your computer and the browser doing the
calculation. If you want rounding, you should use the Math.round()
function. Check http://www.javascriptkit.com/javatutors/round.shtml for
info on how to round numbers with the decimals you want !
 
R

Randy Webb

matty said the following on 11/13/2005 10:25 PM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

which browser and computer are you using? I tried IE and Firefox and I
have 95.69999999999 all the time.

You are mixing floating point and integer, so floating point takes
precedence (you should do 3.0 * 31.9 to be syntaxically correct). So
all results that look like .999999 are actually the correct results as
well as the 95.7, it depends on your computer and the browser doing the
calculation. If you want rounding, you should use the Math.round()
function. Check http://www.javascriptkit.com/javatutors/round.shtml for
info on how to round numbers with the decimals you want !

Or, just read this groups FAQ, specifically section 4.7
 
R

Randy Webb

kicken said the following on 11/13/2005 10:58 PM:
Floating-point mathmatics isn't perfect in computers due to storage
limits (you can only have 1 bit, not .5 bits and such). There's a nice
long technically correct reason why this happens, but I forget where I
read it at, and don't remember all the details. Basically, just round
the result with Math.round() and be done with it.

Don't do math calculations with decimals and you don't have a problem to
start with.
 
B

baong

thank you very much u all

i think i will do math.round
but i still wondering why NOT every number
but only 3,6,7,9,12,14,18,23,24,28,29

any way thank you again

Budi
 
D

Dag Sunde

baong said:
thank you very much u all

i think i will do math.round
but i still wondering why NOT every number
but only 3,6,7,9,12,14,18,23,24,28,29

Incidentally, those numbers leads to a fraction that
can't be represented as exact x^2.

Remember that there is a finite number of bits set aside
to store the fractional part of a calculation.

If you had 1 byte (8 bits) to store a floating point number
and you set aside 4 bits to hold the integer part and sign,
you'll have 4 bits left for the fraction:

If we store 3.15, we get 100% accuracy

S | I I I . F F F F
0 | 0 1 1 . 1 1 1 1

but if we want to store 3.16, we have to round it, since
we can't store the value 16 in 4 bits. We have to round it
down to .15, or up to .2

0011.1111 or 0011.0010
 
R

Randy Webb

baong said the following on 11/14/2005 2:34 AM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.
thank you very much u all

i think i will do math.round

You are better off not using decimals in multiplication/division
operations in javascript, then you don't have any problems at all.
Remove the decimal, do your multiplication/division, then add the
decimal back using String operations.
but i still wondering why NOT every number
but only 3,6,7,9,12,14,18,23,24,28,29

For the same reason the question in the FAQ gives the results it does.
Did you read it?
 
R

Robert

Randy said:
If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

Do you have some button that automatically inserts this text for you :p
 
L

Lasse Reichstein Nielsen

matty said:
You are mixing floating point and integer, so floating point takes
precedence (you should do 3.0 * 31.9 to be syntaxically correct).

That must be an idea from another language.
In Javascript, all numbers are floating point numbers (more preciely
64 bit floating point numbers as defined in IEEE-754, the kind typically
implemented by the CPU as well). There is no difference between "3" and
"3.0" as a number literal.

The 64 bit floats can represent all integers up to 2^54 precisely, and
do exact integer arithmetic with them.

/L
 
M

matty

Lasse said:
That must be an idea from another language.
In Javascript, all numbers are floating point numbers (more preciely
64 bit floating point numbers as defined in IEEE-754, the kind typically
implemented by the CPU as well). There is no difference between "3" and
"3.0" as a number literal.

The 64 bit floats can represent all integers up to 2^54 precisely, and
do exact integer arithmetic with them.

/L

See, I clicked on "show options" so I won't get slapped :)

So thanks Lasse, I learned something, I didn't know that all numbers
were floating point. I wouldn't have known if the poster had read the
FAQ and didn't post here :)
 
T

Thomas 'PointedEars' Lahn

matty said:
So thanks Lasse, I learned something, I didn't know that all numbers
were floating point. I wouldn't have known if the poster had read the
FAQ and didn't post here :)

Well, you could have read the respective FAQ section before posting as
well ...


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 14 Nov 2005
19:01:43, seen in Lasse Reichstein Nielsen
In Javascript, all numbers are floating point numbers (more preciely
64 bit floating point numbers as defined in IEEE-754, the kind typically
implemented by the CPU as well). There is no difference between "3" and
"3.0" as a number literal.

The 64 bit floats can represent all integers up to 2^54 precisely, and
do exact integer arithmetic with them.

But Math.pow(2, 54) - 1 is even, as is Math.pow(2, 53) + 1 ;
you mean up to (and including) 2^53 (and down to include -2^53).
 
R

Richard Cornford

I thought it was two to the power of fifty three (9007199254740992), but
what is a bit here or there? (and maybe you mean the sign bit to be bit
54.) I have been working on this for the FAQ notes, but I have only got
as far as:-

"Javascript only has one number type and that is a double-precision
64-bit format IEEE 754 floating point number. That type of number is
capable of representing 18437736874454810627 distinct values including
positive and negative infinity and the special numeric value NaN (Not a
Number). The representable range is +/-1.7976931348623157×10^308 (with
numbers as small as +/-5×10^-324). The range of precise integer values
is +/-9007199254740992 (+/-2^53). That leaves 18419722475945328643
values to represent non-integers and integers outside of that range, and
obviously there are considerably more numbers in the IEEE 754 range than
can be represented by the available distinct values.

As a result some numeric values are represented as approximations. For
example, the largest integer value that can be entered in source code
without its IEEE 754 representation being returned in exponential format
when converted to a string is 999999999999999934469, but its IEEE 754
representation is approximated as 999999999999999900000. Most
non-integer numbers are also represented as the closest approximation
possible."
See, I clicked on "show options" so I won't get slapped :)

You might think that but ... you have failed to trim the material you
quoted down to just that part of it that is sufficient to provide
context for your response, as is required by Usenet convention. You have
also quoted a 'signature' which is always incorrect.
So thanks Lasse, I learned something, I didn't know that all
numbers were floating point. I wouldn't have known if the
poster had read the FAQ and didn't post here :)

And it doesn't occurred to you that you would not have needed to wait
until someone asked the question if you have read the FAQ yourself.
Still there seem to be plenty of people who regard the risk of learning
something from it as grounds for not reading the FAQ. Are you one of
them?

Richard.
 
M

matty

I read the FAQ. And there was no mention of "Javascript only has one
number type and that is a double-precision
64-bit format IEEE 754 floating point number." as of 2 days ago in the
FAQ. I search for "64" and it was not there and not in the
http://www.jibbering.com/faq/faq_notes/type_convert.html link either
(which I did read).

"you have failed to trim the material you quoted down to just that part
of it that is sufficient to provide context for your response, as is
required by Usenet convention. You have also quoted a 'signature' which
is always incorrect."

I don't understand. I think that even if newbies make mistakes in the
way they post or reply, they should be rewarded for trying to help, and
given references on how to properly respond rather than getting
arrogant replies that just discourage people from being more active.
Also, I googled "usenet convention" and nothing really clear came up.
If this is refering to the Netiquette of 1998, be aware that we are in
2005 and that new tools, new ways of discussions have made the
newsgroups more accessible and more "affordable" for most of us. I will
try my best to figure that out and will accept all comments and help as
long as they don't violate the "convention" :)
 
R

Randy Webb

Robert said the following on 11/14/2005 8:17 AM:
Do you have some button that automatically inserts this text for you :p

It's automatically added for me, I just move it where I want it or
remove it all together when I don't want it :)
 
R

Richard Cornford

matty said:
I read the FAQ. And there was no mention of "Javascript
only has one number type and that is a double-precision
64-bit format IEEE 754 floating point number." as of 2
days ago in the FAQ. I search for "64" and it was not
there and not in the
http://www.jibbering.com/faq/faq_notes/type_convert.html
link either (which I did read).

The rate at which it is possible to create accurate additional
explanation/detail for the FAQ is related to the number of (suitably
qualified) people willing to do it, and the amount of time they have
available for the task. the task is much nearer its beginning than its
end.
"you have failed to trim the material you quoted down to
just that part of it that is sufficient to provide context
for your response, as is required by Usenet convention. You
have also quoted a 'signature' which is always incorrect."

And this time you have failed to provide a context for your first
response, failed to attribute they quote you have made and failed to use
a conventional quote indicator.
I don't understand.

What don't you understand? That quoting material that is not pertinent
to the responses is undesirable? That signatures are almost always
irrelevant to a response? Or what a signature is?
I think that even if newbies make mistakes in the
way they post or reply,

They do, and those mistakes can have serious consequences for the
outcome of their interactions with the group. Generally everyone gets an
opportunity to demonstrate a willingness to conform with the
conventions.
they should be rewarded for trying to help,

And discouraged from doing harm. Which is what anyone who fails to
follow the post formatting conventions is doing, particularly in
encouraging newcomers to do likewise.
and given references on how to properly respond

And where would you expect to find that information? Maybe in (and/or
referenced from) some sort of resource publicly available on the
Internet, posted to the group at frequent interval and to which posters
are regularly referred?
rather than getting arrogant replies that just
discourage people from being more active.

It is difficult to accurately attribute motivation, attitude or emotion
from the contents of plain text messages. If you re-read the posts that
you believe to be arrogant can you really be certain that they are not
intended as friendly advice, hints in the right direction and justified
warnings (albeit often terse)?
Also, I googled "usenet convention" and nothing really clear came up.

Why didn't you just read the FAQ when you had it avalable?
If this is refering to the Netiquette of 1998, be aware that
we are in 2005 and that new tools, new ways of discussions
have made the newsgroups more accessible and more "affordable"
for most of us.

The Usenet posting conventions evolved in response to diversity in
methods of access. Having even more diversity is, if anything grounds
for more strict adherence to the established conventions. The last thing
that should be allowed to happen to Usenet is to have it fragment into
sub groups using different types of newsreader software.
I will try my best to figure that out and will accept all
comments and help as long as they don't violate the "convention" :)

You don't need to work it out, the details are available in, and
through, the FAQ (that is one of the points of its existence).

Richard.
 
V

VK

Richard said:
"Javascript only has one number type and that is a double-precision
64-bit format IEEE 754 floating point number.

One of urban legends originally raised from the bad math / CS skills of
some ECMA time-share paper wasters (aka tech-writers).

Windows / Mac / Linux and Co are 32-bit systems - 64 bit systems are
still rather new bists on the market.

If the interpreter spots a new number, it will first try reading the
number as an integer. If that's possible, then it stores the number as
a 31 bit integer, not as a 64 bit IEEE 754. (Yes, that's 31 bits, not
32 bits.) If the number cant be read as an integer or it won't fit in
31 bits, then it is stored in 64 bit IEEE 754. Similar logic applies to
all calculations.
 
R

RobG

matty said:
I read the FAQ. And there was no mention of "Javascript only has one
number type and that is a double-precision
64-bit format IEEE 754 floating point number."

It's in the ECMAScript Language Specification - section 4.3.20:

"Number Type
"The type Number is a set of values representing numbers. In
ECMAScript, the set of values represents double-precision 64-bit
format IEEE 754 values including the special "Not-a-Number" (NaN)
values, positive infinity, and negative infinity."


Not to be confused with Number Object, of course...

[...]
Also, I googled "usenet convention" and nothing really clear came up.
If this is refering to the Netiquette of 1998, be aware that we are in
2005 and that new tools, new ways of discussions have made the
newsgroups more accessible and more "affordable" for most of us.

Usenet started in 1979, not everything is 'soooo five minutes ago...'.
It has a long history and well established conventions that are
intended to encourage positive and useful participation.

I will
try my best to figure that out and will accept all comments and help as
long as they don't violate the "convention" :)

See ya round! :)
 
R

rf

VK said:
One of urban legends originally raised from the bad math / CS skills of
some ECMA time-share paper wasters (aka tech-writers).

Windows / Mac / Linux and Co are 32-bit systems - 64 bit systems are
still rather new bists on the market.

64 bit floating point has been around for decades.
If the interpreter spots a new number, it will first try reading the
number as an integer. If that's possible, then it stores the number as
a 31 bit integer, not as a 64 bit IEEE 754. (Yes, that's 31 bits, not
32 bits.) If the number cant be read as an integer or it won't fit in
31 bits, then it is stored in 64 bit IEEE 754. Similar logic applies to
all calculations.

Please provide a referance where these assertions may be verified.

Here is a reference which I believe refutes what you claim:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jscondatatype.asp

Scroll down a bit to the heading "Number data type", wherein it says
<quote>
Internally, JScript represent[sic] all numbers as floating-point values
</quote>

I take this source as being authoritative, since they are the blokes who
wrote JScript, IE's implementation.

--
Cheers, Richard.
If you are reading this using google groups then also read this:
http://www.safalra.com/special/googlegroupsreply/ if you have not done so
already. If you reply to this post without correct quoting and attribution,
as per the above, I, and others, may just totally ignore you.
 
M

matty

RobG said:
It's in the ECMAScript Language Specification - section 4.3.20:

"Number Type
"The type Number is a set of values representing numbers. In
ECMAScript, the set of values represents double-precision 64-bit
format IEEE 754 values including the special "Not-a-Number" (NaN)
values, positive infinity, and negative infinity."
So i've spent quite a while today reading about the javascript history
and ECMA so I wouldn't post stupid replies when trying to help.

What thing that is very confusing to me is the relation between W3C and
ECMA. Is ECMA the standard for Javascript? I see things like W3C
telling me that I should not use "<script language="javascript"> and
rather use <script type="text/javascript"> (which will be depracated
and replaced by <script type="application/javascript"> but ECMA tells
me the opposite, emphasizing on version numbers (<script
language="javascript1.2">) as being important for defining which
version of the language is supported.

One thing people need to understand is that there wouldn't be any group
like this particular group if everyone read and understood the
javascript language. It is not simple, there are numerous books and
references and websites about it and it is still a language in the
works, with multiple versions, conflicting standards, etc. Asking a
question and not knowing the complete history & different
implementations about javascript is not a crime.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top