Re: countdown display code
- Posted by SDPringle Aug 24, 2020
- 1895 views
Good job but there is another way you could do it.
You see, the 0.9999 comes from repeating decimals which happens in binary as often as in base-10. You often get non-terminating decimal places after dividing.
You see 1/3 gives you a non-terminating decimal, but the computer will keep a limited set of decimals. Multiply this by 3 and you get 0.99999999. Now 1/86400 is a non-terminating decimal in both binary and decimal. So when you multiply the values you get this just shy of the correct value problem. If you avoid having this fraction in the first place, you avoid the problem.
atom t = floor(diff(d1,d2)) -- total_seconds. A whole number but might be too big for integer type. integer s = remainder(t,60) -- the seconds part of the time 0..59 (always an integer!) t = floor(t/60) -- now total minutes integer m = remainder(t, 60) -- the minute part of the time 0..59 t = floor(t/60) -- now total hours integer h = remainder(t,24) -- hour part of the time. t = floor(t/24) -- now total days -- etc ....

