Math: % between lo .. hi

BlitzMax Forums/BlitzMax Programming/Math: % between lo .. hi

EOF(Posted 2006) [#1]
Do you have a formula for calculating the percentage between a LO and HI value given a value somewhere between the two?

I need to cover negatives too. Examples:
Local LO#=140.0
Local HI#=160.0

Local VAl#=150.0

Print "Percent = "+ ?????????

Local LO#=-2.0
Local HI#=5.0

Local VAl#=0.0

Print "Percent = "+ ?????????


Also, what about finding the value between LO and HI if you know the percentage?
Local LO#=-22.5
Local HI#=200.61

Local P%=75

Print "Value = "+ ?????????
Thanks coders. It's a simple maths thing driving me daft.


Scott Shaver(Posted 2006) [#2]
Local LO#=0.0
Local HI#=100.0

Local VAl#=85.0

Range# = HI#-LO#
Off# = HI#-VAL#
Per# = (1-(Off#/Range#)) *100

Print Per#


Jesse(Posted 2006) [#3]
here are my two cents! a little late.
#= -2.0
hi#= 5.0
val# = 0
percent# = 75
topercent# = Abs((val-lo)/(hi#-lo#))*100.0
frompercent# = (hi-lo)*(percent/100.0)


Pongo(Posted 2006) [#4]
and this one is really late. I don't use BMax yet, but this is a function I made for doing this stuff in B3d,... should carry over.
Function FitValueToRange#( InValue#, RangeIn_Start#, RangeIn_End#, RangeOut_Start#, RangeOut_End# )
	
	OldRange# = RangeIn_End#-RangeIn_Start#
	NewRange# = RangeOut_End# - RangeOut_Start#	
	
	OutValue# = ((InValue#-RangeIn_Start) / OldRange#) * NewRange#	+ RangeOut_Start		

	Return OutValue#

End Function



EOF(Posted 2006) [#5]
Thanks coders.