Open JobLeonard opened 6 years ago
I haven't had a need for the other functions yet, so I haven't ported them. Maybe I should, and create an NPM module.
Anyway, thanks again for the library!
Ok so I have a question after all! Since all floating point operations in JS use doubles (and I just checked - making the literals fit in a float32 doesn't affect anything), could I gain some extra accuracy by having the constants calculated to the highest precision available to doubles? Since that could reduce rounding errors in intermediate calculations, right?
So I'm building an SPA for biologists that has dynamic plots, and I have a ton of typed arrays that often need log2 projections.
Guess what, your approximation works in JavaScript!
You see, while JavaScript officially only has doubles for numbers, and does not support unions, there is a workaround: using the shared backing buffers of
TypedArray
s. In other words: we create a one elementFloat32Array
, use the same backing buffer for aUint32Array
, and voila, we have our union:Yes, I know this is perverted. But hey, it works! No asm.js or WASM required! And it's fast! Only the desktop of chrome Chrome does not have an immediate disadvantage (and even then only since the last couple of versions - TypedArrays have been given the shaft in terms of optimisations for years). In all other browsers (Firefox, Edge, and all mobile browsers I tried out) seem to benefit from it:
https://run.perf.zone/view/Paul-Mineros-fastlog2-fasterlog2-Float32Array-versions-array-of-100000-elements-v4-1520435680338
https://run.perf.zone/view/Paul-Mineros-fastlog2-fasterlog2-Float32Array-versions-array-of-100-elements-v4-1520436338346