$5 calculator > javascript


#1

for(a = 20;a>-1;a -= 1/60){
document.write(a);
}

I am not sure what the problem is here. I have deduced this to a problem with my computer or notepad++ (the application I like to use to write up code).

This outputs, 20 -= 1/60. what I want to do is output 20 -= (1/60)--all the way till zero. [20 - (1200/60)] ~ in math terms. I want to see each subtraction, and so I am using document.write to output each time 1/60 is subtracted.But when I reach [20 - (1199/60] -- the last subtraction before it should hit 0. A major problem occurs, and instead of the next value being 0, I get this: -1.4627882238826828e-13

Something else to note is that the subtraction before This, which should be [20 - (1199/60], is actually 0.016666666666520388. This is because javascript is rounding each time and this is leading me to get the wacky number instead of 0. I checked it out on a good old calculator and it is true 0.016666666666520388 - 1/60, is -1.4627882238826828e-13. So how do I stop javascript from doing this idiotic rounding, and messing up my code? (I'm trying to stop my code when it hits 0)

Also, I checkout my code using a good old calculator, and it does not do this silly rounding error, and it ACTUALLY hits 0! What a surprise!

Any ideas? Also I don't want to calculate what this number will be each time, and making it my cut-off (instead of 0), simply because that is not fixing the problem--but avoiding it. And, I want to be as economic as possible with my precious code!

Also, I have read up a little on binary representations of decimals. How do I fix it for my specific example? I don't want to download anything special off the internet.


#2

There is an infinite number of numbers, it is not possible to represent them all exactly with a finite amount of memory.

If the numbers you want to represent exactly are all rational, then keep track of a numerator and denominator, the very same as is taught in school.

1200 / 60
1199 / 60
1198 / 60
1197 / 60
...

There are third party libraries for dealing with rational numbers, there's nothing special about using those. I dare claim almost all websites you use download scripts from various locations (unless you block javascript in your browser)

You can also use a denominator that is a power of 2 (like 64) which can be represented exactly with a binary number system


#3

Im doing a count down timer, so i need to subtract (1/60) each time. That is actually a great idea, to count down once from the numerator from a rational expression. YOU my may have just solved my problem. Thank you for that! really appreciate it!

Also I have read a bit about binary floating point representations, I am a novice computer programmer so I didn't quite understand that part, care to enlighten me?


#4

That's far too open-ended, there's no way I can guess what you're missing without you telling me, and there's nothing I can say that isn't already accessible on that topic. You already know this.


#5

Why do computers have problems doing things like 2/10 + 1/10? This is 3/10, but the output is not that, and notice this is isn't even a repeating decimal. I searched this up, but the websites I found had huge complex theorems and proofs on floating point numbers, and either I didn't understand the terminology or they did not answer the questions. Why can't computers store 2/10 and 1/10 correctly, so that the sum in 3/10?


#6

1/10 repeats infinitely in base2


#7

O ok wow, that makes sense now, I need to go and read up on base 10 to base 2 convertions. Thank you for the awsome help!