elm-ui icon indicating copy to clipboard operation
elm-ui copied to clipboard

Element.modular math is wrong (easy fix provided)

Open andreacfromtheapp opened this issue 4 years ago • 0 comments

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 1 yields 16 instead of 20
  • E.g: Element.modular 16 1.25 2 yields 20 instead of 25
  • E.g: Element.modular 16 1.25 3 yields 25 instead of 31.25
  • and so on

Expected behavior

With the above in mind, and with a base, a ratio, and a multiplier:

  • Only 0 for the multiplier should yield the base number unchanged. (16 in this example).
  • Element.modular 16 1.25 1 should yield 20 and not 16. (try 16*1.25*1 in 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) 💯

andreacfromtheapp avatar Jan 07 '22 23:01 andreacfromtheapp