HISE Logo Forum
    • Categories
    • Register
    • Login

    Math.ratio

    Scheduled Pinned Locked Moved Feature Requests
    5 Posts 3 Posters 520 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • LightandSoundL
      LightandSound
      last edited by

      Simple one here, adding a math function that I end up using in most projects.

      Converting one variable in a given range, to the equivalent value in another range via:

      NewValue = (((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)) + NewMin

      Easy to use my own function, but I'm sure this is one many would benefit from and likely already use.

      Final output would look something like:

      Math.rangeRatio(input, oldmin, oldmax, newmin, newmax)

      1 Reply Last reply Reply Quote 1
      • d.healeyD
        d.healey
        last edited by d.healey

        I think the math functions in HISE are just wrapper for the JUCE math library, so it would require a little more work to add a custom function (I might be wrong though).

        I also seem to recall with this function that if oldMax - oldMin == 0 the function returns the wrong result, so you have to handle that separately.

        This is from my KSP library

        //Maps a given value in a given range of values to that of another range of values
        taskfunc units.rescale(value, oldmin, oldmax, newmin, newmax) -> return
            
            declare local old_range
            declare local new_range
        
            old_range := oldmax - oldmin
        
            if (old_range = 0)
                return := newmin
            else
                new_range := newmax - newmin
        
                return := (((value - oldmin) * new_range) / old_range) + newmin        
            end if
        
        end taskfunc
        

        Libre Wave - Freedom respecting instruments and effects
        My Patreon - HISE tutorials
        YouTube Channel - Public HISE tutorials

        LightandSoundL 1 Reply Last reply Reply Quote 2
        • LightandSoundL
          LightandSound @d.healey
          last edited by

          @d-healey

          Yeah you do need to handle it if the old range is 0, which is a very, very fringe case though since you'd need to call the function intentionally, without having a previous range at all!

          ustkU 1 Reply Last reply Reply Quote 1
          • ustkU
            ustk @LightandSound
            last edited by

            @LightandSound @d-healey In what kind of situation would you have the oldRange equal to 0?
            Variable range going from negative to positive value?

            Can't help pressing F5 in the forum...

            LightandSoundL 1 Reply Last reply Reply Quote 0
            • LightandSoundL
              LightandSound @ustk
              last edited by

              @ustk that's why I say it's a fringe case, essentially if you set them as variables or for example use getvalue() without setting the min/max value of the panel then you could run in to problems. But there shouldn't be a 'real world use' for a range of 0 to a larger range, since all that does is spit out the same value. It's just to protect for the inevitable case where human error gets in the way.

              1 Reply Last reply Reply Quote 1
              • First post
                Last post

              50

              Online

              1.7k

              Users

              11.7k

              Topics

              101.9k

              Posts