Module

Puregrain.Internal.Exact

Package
purescript-puregrain
Repository
roman-mkh/puregrain

Fifth and cube roots from exact operations only (+, −, ×, ÷ and comparisons), so that every JavaScript engine gives the same results: ECMA-262 lets Math.pow and Math.cbrt differ between engines, but not these operations. Internal: may change without notice.

Each root scales its input by exact powers of 2 into a small range, [1/32, 1) for the fifth root and [1/8, 1) for the cube root, takes a fixed number of Newton's steps from 1, and scales back. Scaling by powers of 2 loses nothing, so the same number of steps fits every input, from the smallest denormal up, and fifthRoot (32.0 * x) is exactly 2.0 * fifthRoot x.

The results are within two units in the last place of the nearest number to the true root, mostly that number itself, so two neighbouring inputs can get roots up to two units apart in either order. Why, how often, and why it doesn't matter for dithering: docs/exact-arithmetic.md.

#fifthRoot Source

fifthRoot :: Number -> Number

The fifth root of any number. Odd: a negative number's root is the negative of its magnitude's root. 0, the infinities and NaN come back as they are.

#cubeRoot Source

cubeRoot :: Number -> Number

The cube root of any number, as fifthRoot is the fifth.

#fifthRootStep Source

fifthRootStep :: Number -> Number -> Number

One of Newton's steps towards the fifth root of m, from y.

#cubeRootStep Source

cubeRootStep :: Number -> Number -> Number

One of Newton's steps towards the cube root of m, from y.