Main Menu

Math: Percent inside a range.

Started by Draco9898, September 06, 2007, 04:19:31 AM

Previous topic - Next topic

Draco9898

Ok, don't make me look stupid and tell me theres a math function for this already.
This finds a value from a range using a percentage.


function CalcPerInsideRng(P#,Bgnrng#,Endrng#)
returnVal# = Bgnrng# + ((Endrng#-Bgnrng#)*P#)
ENDFUNCTION returnVal#
DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin

Rembrandt Q Einstein

Useful function, something I needlessly code again every time I use something like tihs.  Almost every time I use it, I want to bound the P# from 0-1, so it might be helpful to do this.


function CalcPerInsideRng(P#,Bgnrng#,Endrng#)
if p# < 0.0 then p# = 0.0
if p# > 1.0 then p# = 1.0
returnVal# = Bgnrng# + ((Endrng#-Bgnrng#)*P#)
ENDFUNCTION P#,returnVal#


Edit: rearrange.

kevin

#2
 You can use ClipRange#() to clip a value with a pair of markers.  Can be handy to clean up those common bits of clipping code.

PlayBASIC Code: [Select]
psub CalcPerInsideRng(P#,Bgnrng#,Endrng#)
p#=cliprange#(p#,0,1)
returnVal# = Bgnrng# + ((Endrng#-Bgnrng#)*P#)
endPsub returnVal#