Element.modular math is wrong (easy fix provided)
Hi and thank you for elm-ui 😄
Element.modular formula is off because of the use of -1.
Current behavior
Each multiplier returns what the previous should:
- E.g:
Element.modular 16 1.25 1yields16instead of20 - E.g:
Element.modular 16 1.25 2yields20instead of25 - E.g:
Element.modular 16 1.25 3yields25instead of31.25 - and so on
Expected behavior
With the above in mind, and with a base, a ratio, and a multiplier:
- Only
0for themultipliershould yield thebasenumber unchanged. (16in this example). -
Element.modular 16 1.25 1should yield20and not16. (try16*1.25*1in a calc)
Versions
- Elm Version: 0.19.1
- Elm UI Version 1.1.8
Solution
The issue is caused by using the -1 in the function definition. The fix is super easy and saves an else if that's not needed because the negative multiplier suffice:
modular : Float -> Float -> Int -> Float
modular normal ratio rescale =
if rescale == 0 then
normal
else
normal * ratio ^ toFloat rescale
I put a demo of this on Ellie: https://ellie-app.com/gm2bfCbKdtZa1
Also refer to these to corroborate my findings:
- https://www.modularscale.com/?16&1.25 (use the Sass mode and Table view)
- https://utopia.fyi/blog/css-modular-scales/
Docs Need Updating too
Contextually, the documentation examples should be fixed/updated as well, to match the corrected formula. Also, to add partial application to the (scaled) examples. Ref. https://github.com/mdgriffith/elm-ui/issues/36
Cheers :)
ps: if needed I would gladly do the PR(s) 💯