jlongster / absurd-sql

sqlite3 in ur indexeddb (hopefully a better backend soon)
MIT License
4.15k stars 101 forks source link

Is Math.round correct here? #24

Open quolpr opened 3 years ago

quolpr commented 3 years ago

I am not sure, but usually, Math.ceil is used in such cases

https://github.com/jlongster/absurd-sql/blob/ee7fa617e48fd6b4a51cf58893ef514b0da272fb/src/indexeddb/file-ops-fallback.js#L1 https://github.com/jlongster/absurd-sql/blob/ee7fa617e48fd6b4a51cf58893ef514b0da272fb/src/indexeddb/file-ops.js#L3

Is it wrong, or could you explain why Math.round was used?

jlongster commented 2 years ago

That was a defensive measure to make sure we were never bit by floating point errors. You don't want ceil because if the error was 20.00000000001 when it should be 20, it would round up to 21. Floating point errors are always tiny amounts off, and round makes sure that whether or not they are below or above the target number it will round to the right one.

(I'm not familiar enough to know when these errors will happen with integers. But for example try .1 * .2. If numbers are always divisible, there may never be rounding errors. We could verify this with property tests)