I have a min and max position of an object and I want to represent an arbitrary point between them as a float between 0.0 and 1.0. This feels relatively basic math, but I can’t quite figure out what I need to do with this. Is there a special name for this sort of thing? Also, are there any built-in methods that would be useful for this?

  • o11c
    link
    fedilink
    arrow-up
    4
    arrow-down
    2
    ·
    1 year ago

    Related, note that division is much slower than multiplication.

    Instead of:

    n / d
    

    see if you can refactor it to:

    n * (1.0/d)
    

    where that inverse can then be hoisted out of loops.

    • shotgun_crab@lemmy.world
      link
      fedilink
      English
      arrow-up
      3
      ·
      edit-2
      1 year ago

      Just to clarify, the value in parentheses must be precalculated.

      For example, instead of:

      n / 2
      

      Do:

      n * 0.5
      
      • o11c
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

        Compilers are generally forbidden from optimizing floating-point arithmetic, since it can change precision.

    • Murderturd@lemmy.world
      link
      fedilink
      arrow-up
      2
      arrow-down
      1
      ·
      1 year ago

      If multiplication vs division is causing perf issues you fucked up somewhere or shouldn’t be asking on Lemmy for help because your performance critical system is of the safety and health type.

      I’ve never had division actually be a real issue.