Closed Thread Icon

Topic awaiting preservation: Calculation Problem (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8672" title="Pages that link to Topic awaiting preservation: Calculation Problem (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Calculation Problem <span class="small">(Page 1 of 1)</span>\

 
Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 05-26-2003 06:06

Hi, guys. How are you?
Honestly I am sick of bad flu here in New Zealand.
Everybody is coughing a lot. Hope it is not SARS.

Anyway, would you have a time to see my problem?
I thought I coded successfully. But one funny thing annoys me a lot.

My mission:

quote:
Write a program to calculate the interest on a bank acount. The interest is
0% on balances < $1000
1% on balances >=$1000 and < $5000
1.5% on balances >=$5000



And my uploaded scripte is here.

Problem:

quote:
When input some number, I got funny figure. For example, type 659874.
I got 9898.109999999998 in the answer box.
But I think 659874 * 1.5% = 9898.11 isn't it???



Hmm....Do you have any idea????
many thanks for reading this. m(. .)m

Hiroki Kozai

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 05-26-2003 06:38

The problem is that some numbers, when represented within the computer as binary numbers, aren't 100% exact. These tiny innacuracies appear when you output the final number.

The best solution is to decide on how many decimal places you want to display, and then round to that. Since the answer the computer gets is very *close* to the real answer (it's only off by like .00000000001), after rounding it will (almost always) display the correct answer.

How to round to x decimal places:

1. multiply by 10^x
2. use Math.round() to round off
3. divide by 10^x

For example:

Math.round(number*100)/100 // rounds to 2 decimal places

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 05-26-2003 07:07

Many many thanks for that, Slime.

quote:
The problem is that some numbers, when represented within the computer as binary numbers, aren't 100% exact.



Hmm..... why not? I just wonder.....

Hiroki Kozai

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 05-26-2003 07:19

For the same reason that, say, 1/3 can't be represented exactly in decimal form:

0.3333333333

If you have to stop writing digits (which computers must), you can't get *exactly* to 1/3.

Similarly, the number 1/3 in binary form is:

0.010101010101...

(which is equivalent to 1/4 + 1/16 + 1/64 + ...)
but the computer can't use infinite digits, so it has to truncate, and thereby loses a tiny amount of accuracy.

Note that this only happens with fractional numbers.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 05-26-2003 19:48

Here's an article about Javascript precision

I thought that level of precision seemed relatively small but apparently Javascript does have a relatively small precision level (about 16 digits)



.:[ Never resist a perfect moment ]:.

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 05-26-2003 23:07

Hi, guys. Good morning.
How are you today?
Many many thanks for your explanation and nice link, guys.
Have a good day.
I am keep going ahead of studying JavaScript.
Cya.

From JavaScript Lover

Hiroki Kozai

« BackwardsOnwards »

Show Forum Drop Down Menu