Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

.07*100 results 7.00000001 and .08*100 results 8. Why?

530 views
Skip to first unread message

Hem

unread,
Jan 29, 2003, 8:11:11 AM1/29/03
to
Hi,
Can anybody please explain the above behaviour to me.
To see this behaviour, just add the following code to teh
BODY Tag -
onload = "alert(.07*100)"
Numbers 7, 14, 28, 29, 55,
56,57,58,109,110,111,,112,113,114,115,116.... donot return
as whole numbers while others do. I couldnot reason this
behaviour. Could anyone please help me in this.

Thanx & Regards,
Hem

Evertjan.

unread,
Jan 29, 2003, 10:25:34 AM1/29/03
to

Numbers like 0.07 cannot be represented exactly in floating point binary,
the internal javascript conversion, so they are aproximated.

It is normal behavour. If you do not like it you should round.

<script>
a=.07*100
document.write(a+" = normal behavour<br>")
a=Math.floor(a+0.5)
document.write(a+" = rounded<br>")
</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Lee Peedin

unread,
Jan 29, 2003, 10:38:21 AM1/29/03
to
And for more reading:
http://www2.hursley.ibm.com/decimal/

A decimal class for Java
http://www2.hursley.ibm.com/decimalj/

I know Mike personally and for seveal years he has been working with
both hardware & software developers to try to get decimal arithmetic
as the standard. Several languages (Rexx included) already use
decimal arithmetic.

Lee

Evertjan.

unread,
Jan 29, 2003, 4:58:14 PM1/29/03
to
Lee Peedin wrote on 29 jan 2003 in microsoft.public.scripting.jscript:
> I know Mike personally and for seveal years he has been working with
> both hardware & software developers to try to get decimal arithmetic
> as the standard. Several languages (Rexx included) already use
> decimal arithmetic.

Long, long ago (1982?), Bill Gates (himself?) wrote "Central Data Basic"
for the Signetics/Philips 2650 microprocessor.

This BASIC's arithmetic was compleatly decimal.

It was a joy for financial programming. I made a special version changing
the decimal point with a decimal comma by changing one bite in the
executable.

Dr John Stockton

unread,
Jan 29, 2003, 6:05:22 PM1/29/03
to
JRS: In article <ec5501c2c797$e4ecaba0$8bf82ecf@TK2MSFTNGXA05>, seen in
news:microsoft.public.scripting.jscript, Hem <hemk...@pspl.co.in>
posted at Wed, 29 Jan 2003 05:11:11 :-

>onload = "alert(.07*100)"
>Numbers 7, 14, 28, 29, 55,
>56,57,58,109,110,111,,112,113,114,115,116.... donot return
>as whole numbers while others do. I couldnot reason this
>behaviour. Could anyone please help me in this.

Read the c.l.j FAQ, and the links therefrom; and my site.

Numbers are held as IEEE Doubles, in binary; and so most decimal
fractions are inexact.

--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MSIE 4 ©
<URL:http://www.jibbering.com/faq/> FAQ for comp.lang.javascript by Jim Ley.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

Hemkiran Daryanani

unread,
Jan 30, 2003, 3:01:17 AM1/30/03
to
Thank you very much for all the help. This surely enlightened me and
reminded me of basics we studied in college but forgot over years. The
links for extra reading surely helped.

Regards,
Hemkiran


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

0 new messages