# Limit a var value

#### magic

##### Active member
3rd Party Module Dev
3rd Party Tool Dev
Say I want to multiply x variable by y but I want to make sure the value of x is less than 1.0

x=x*y
if x>1.0 then x=1.0

Is there any better or simple way to doit?

#### Wingnut

##### Well-known member
3rd Party Module Dev
Tutorial Author
If you want to just limit the value after multiplying it, without rescaling / normalizing the values you could do this

Code:
``x = Max(1.0,x)``

EDIT
Sorry that should be Min not Max of course, so it outputs the smallest number of the two!

Last edited:

#### magic

##### Active member
3rd Party Module Dev
3rd Party Tool Dev
Thanks. that what i need.

about rescaling / normalizing. im intrested to know what is that and maybe some example usage.

#### Wingnut

##### Well-known member
3rd Party Module Dev
Tutorial Author
Great. Sure I can show a few examples, two simple useful ones I can think off would be these:
Code:
``````Import mojo2

Function Main()
New MyApp
End

Class MyApp Extends App

Field canvas:Canvas

Method OnCreate()
canvas= New Canvas()

' Min-Max Normalization
Local min :Float = 0.0
Local max:Float = 50.0
Local value:Float = 25 ' Value here
Local a:Float, b:Float
Local newmin:Float = -20.0
Local newmax:Float = 20.0

' #1 Convert a range of values (min - max) into the range 0.0 - 1.0
Local output:float = (value - min) / (max - min)
Print output

' #2 Convert a value from ANY range to ANY other range, linearly
a = (newmax - newmin) / (max - min) ;  b = newmax - a * max ; output = a * value + b
Print output
End

Method OnUpdate()

End

Method OnRender()
canvas.Clear
canvas.Flush
End

End``````

Last edited: