Difference between revisions of "Min"

From Second Life Wiki
Jump to navigation Jump to search
(New page: {{LSL_Function |mode=user |func=min |p1_type=float|p1_name=x |p2_type=float|p2_name=y |return_type=float |return_value=returns the lesser of x and y |func_desc=Returns the lesser of two ar...)
 
m (<lsl> tag to <source>)
 
Line 12: Line 12:
See also: [[Max]]
See also: [[Max]]
|examples=
|examples=
<lsl>float value = min(10,20); //value == 10</lsl>
<source lang="lsl2">float value = min(10,20); //value == 10</source>
|spec=<lsl>float min(float x, float y)
|spec=<source lang="lsl2">float min(float x, float y)
{
{
     return ( ( llAbs( x >= y ) ) * y ) + ( ( llAbs( x<y ) ) * x );
     return ( ( llAbs( x >= y ) ) * y ) + ( ( llAbs( x<y ) ) * x );
}</lsl>
}</source>
|helpers
|helpers
|also_functions
|also_functions
|also_events
|also_events
|also_tests=
|also_tests=
<lsl>// Max and Min Functions and Unit Tests
<source lang="lsl2">// Max and Min Functions and Unit Tests
// Emilie (Hermit Barber in World)
// Emilie (Hermit Barber in World)
// Creative Commons: Attribution, Share-alike, Non Commecial
// Creative Commons: Attribution, Share-alike, Non Commecial
Line 72: Line 72:
         ix=-0.001;iy=-0.00001;say(test,ix,iy,Min(ix,iy));
         ix=-0.001;iy=-0.00001;say(test,ix,iy,Min(ix,iy));
     }
     }
}</lsl>
}</source>
|also_articles
|also_articles
|location
|location

Latest revision as of 16:21, 24 January 2015

Summary

Function: float min( float x, float y );

Returns the lesser of two arbitrary values
Returns a float

• float x
• float y

If values x and y are equal, returns y

See also: Max

Specification

float min(float x, float y)
{
    return ( ( llAbs( x >= y ) ) * y ) + ( ( llAbs( x<y ) ) * x );
}

Examples

float value = min(10,20); //value == 10

Deep Notes

Tests

// Max and Min Functions and Unit Tests
// Emilie (Hermit Barber in World)
// Creative Commons: Attribution, Share-alike, Non Commecial

float Max(float x,float y)
{
    return ( ( llAbs( x >= y ) ) * x ) + ( ( llAbs( x<y ) ) * y );
}

float Min(float x,float y)
{
    return ( ( llAbs( x >= y ) ) * y ) + ( ( llAbs( x<y ) ) * x );
}

// Copy the above functions to your program - the balance is mereley a set of unit tests
    
say(string f, float x,float y, float z)
{
    llOwnerSay("Testing " + f + " with input X: " + (string) x +  " Input Y: " + (string) y + "Resulting in: " + (string) z);
}

default
{
    state_entry()
    {
        string test = "MAX";
        float ix = 0;
        float iy = 0;
        float iz = 0;
        say(test,ix,iy,Max(ix,iy));
        ix=100;iy=100;say(test,ix,iy,Max(ix,iy));
        ix=-100;iy=100;say(test,ix,iy,Max(ix,iy));
        ix=100;iy=-100;say(test,ix,iy,Max(ix,iy));
        ix=-100;iy=-100;say(test,ix,iy,Max(ix,iy));
        ix=-0.001;iy=0.00001;say(test,ix,iy,Max(ix,iy));
        ix=0.001;iy=0.00001;say(test,ix,iy,Max(ix,iy));
        ix=-0.001;iy=0.00001;say(test,ix,iy,Max(ix,iy));
        ix=0.001;iy=-0.00001;say(test,ix,iy,Max(ix,iy));
        ix=-0.001;iy=-0.00001;say(test,ix,iy,Max(ix,iy));
        
        test = "MIN";
        say(test,ix,iy,Min(ix,iy));
        ix=100;iy=100;say(test,ix,iy,Min(ix,iy));
        ix=-100;iy=100;say(test,ix,iy,Min(ix,iy));
        ix=100;iy=-100;say(test,ix,iy,Min(ix,iy));
        ix=-100;iy=-100;say(test,ix,iy,Min(ix,iy));
        ix=0.001;iy=0.00001;say(test,ix,iy,Min(ix,iy));
        ix=-0.001;iy=0.00001;say(test,ix,iy,Min(ix,iy));
        ix=0.001;iy=-0.00001;say(test,ix,iy,Min(ix,iy));
        ix=-0.001;iy=-0.00001;say(test,ix,iy,Min(ix,iy));
    }
}

Signature