TransformRange – scale value from one range to another

About Monkey 2 Forums Monkey 2 Code Library TransformRange – scale value from one range to another

This topic contains 2 replies, has 2 voices, and was last updated by  DruggedBunny 2 months, 3 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #15127

    DruggedBunny
    Participant

    Had to look this one up! Main intended use for me is to scale joystick input values (-1.0 to 1.0) into other ranges (eg. 0.0 to 1.0)…

     

    Not sure if the correct mathematical term is ‘transform’, but that’s what I’m calling it…

    #15138

    nerobot
    Participant

    Useful function!

    I made this func a bit universal – by using generic for any number types. 🙂

    Note 1: I replaced Assert (from_delta <> 0.0,... with Assert (from_delta <> Null,... to have comparison with _0_ for int-based and 0.0 for float-based types.

    Note 2: Seems that comparison floats with any concrete number is a bad idea, because 0.0 is about 0.0000000001 or so. But it works!

    Note 3: Bad idea to use it for integers because of division operation… so maybe better where-clause would be Where T=Float Or T=Double.

    #15141

    DruggedBunny
    Participant

    Nice, I briefly thought about doing <T> but didn’t really need it.

    Not really familiar with Where, but I think I see how that works, which is cool.

    I’d say it’s a bug, though, if float zero ever evaluates to 0.0000000000001! Although floats can definitely not represent all numbers, and direct value comparisons are generally a bad idea, floating point zero should still always be 0.0[… 0] no matter what — all bits being set to zero.

    Already found mine really useful, great for getting finer control from joystick inputs!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.