Difference between revisions of "LlAbs"

From Second Life Wiki
Jump to navigation Jump to search
(this function is not terribly necessary, even if convenient)
 
(17 intermediate revisions by 11 users not shown)
Line 1: Line 1:
{{LSL_Function
{{LSL Function
|func=llAbs
|func=llAbs
|func_id=6|func_sleep=0.0|func_energy=10.0
|func_id=6|func_sleep=0.0|func_energy=10.0
Line 7: Line 7:
|p1_desc=Any integer value
|p1_desc=Any integer value
|return_type=integer
|return_type=integer
|return_text=that is the positive version of '''val'''.
|return_text=that is the positive version of {{LSLP|val}}.
|other_languages={{LSL OL|C++|[http://www.cplusplus.com/reference/cstdlib/abs/ abs]}}
|spec
|spec
|caveats=*The llAbs of -2147483648 is -2147483648, this is because 2147483648 is not a valid [[integer]] value.
|caveats=*The llAbs of -2147483648 is -2147483648. This is because the positive integer 2147483648 is outside the range of allowed LSL {{LSLGC|Integer|integer values}}.
|examples=<lsl>default
|examples=
<source lang="lsl2">
default
{
{
     state_entry()
     state_entry()
     {
     {
         llSay(0,"The absolute value of -4 is: "+(string)llAbs(-4) );
//      returns: "The absolute value of -4 is: 4"
     }   //Returns "The absolute value of -4 is: 4"
         llSay(PUBLIC_CHANNEL, "The absolute value of -4 is: "+(string)llAbs(-4) );
}</lsl><lsl>// Here's a more elaborate example.
     }
ShowAbsolute(integer X)
}
</source>
<source lang="lsl2">
// Here's a more elaborate example.
 
ShowAbsolute(integer inputInteger)
{
{
     string Message = "llAbs("
     string output = "llAbs(" + (string)inputInteger + ") --> " + (string)llAbs(inputInteger);
        + (string)X
 
        + ") --> "
     llSay(PUBLIC_CHANNEL, output);
        + (string)llAbs(X);
     llSay(PUBLIC_CHANNEL, Message);
}
}
default
default
{
{
     state_entry()
     state_entry()
     {
     {
         ShowAbsolute(-3);
         ShowAbsolute(-3);   //  llAbs(-3)  --> 3
         ShowAbsolute(5);
         ShowAbsolute(5);   //  llAbs(5)  --> 5
         ShowAbsolute(-20);
         ShowAbsolute(-20); //  llAbs(-20) --> 20
         ShowAbsolute(0);
         ShowAbsolute(0);   //  llAbs(0)  --> 0
     }
     }
}</lsl>
}
 
</source>
<pre>
// Here's the output produced by the more elaborate example:
Test Object: llAbs(-3) --> 3
Test Object: llAbs(5) --> 5
Test Object: llAbs(-20) --> 20
Test Object: llAbs(0) --> 0
</pre>
|helpers
|helpers
|also_header
|also_header
Line 50: Line 50:
|also_functions={{LSL DefineRow||[[llFabs]]|[[float]] version of llAbs}}
|also_functions={{LSL DefineRow||[[llFabs]]|[[float]] version of llAbs}}
|also_events
|also_events
|also_articles={{LSL DefineRow||{{Wikipedia|Absolute_value}}|}}
|also_articles={{LSL DefineRow||{{Wikipedia|Absolute value}}|}}
|notes
|notes=*Using <code>{{LSLP|val}}-({{LSLP|val}}<<1)*({{LSLP|val}}<0)</code> is roughly two times faster than llAbs, as it avoids a function call and produces identical results.
|cat1=Math
|cat1=Math
|cat2=Integer
|cat2=Integer
|cat3
|cat3
|cat4
|cat4
|haiku={{Haiku|Whether good or bad,|we are not here to judge you.|Just to ask... How much?}}
}}
}}

Latest revision as of 14:49, 28 October 2023

Summary

Function: integer llAbs( integer val );

Returns an integer that is the positive version of val.

• integer val Any integer value
This function is similar to functions (e.g. abs) found in many other languages: 
C++
abs

Caveats

  • The llAbs of -2147483648 is -2147483648. This is because the positive integer 2147483648 is outside the range of allowed LSL integer values.
All Issues ~ Search JIRA for related Bugs

Examples

default
{
    state_entry()
    {
//      returns: "The absolute value of -4 is: 4"
        llSay(PUBLIC_CHANNEL, "The absolute value of -4 is: "+(string)llAbs(-4) );
    }
}
// Here's a more elaborate example.

ShowAbsolute(integer inputInteger)
{
    string output = "llAbs(" + (string)inputInteger + ") --> " + (string)llAbs(inputInteger);

    llSay(PUBLIC_CHANNEL, output);
}

default
{
    state_entry()
    {
        ShowAbsolute(-3);   //  llAbs(-3)  --> 3
        ShowAbsolute(5);    //  llAbs(5)   --> 5
        ShowAbsolute(-20);  //  llAbs(-20) --> 20
        ShowAbsolute(0);    //  llAbs(0)   --> 0
    }
}

Notes

  • Using val-(val<<1)*(val<0) is roughly two times faster than llAbs, as it avoids a function call and produces identical results.

See Also

Functions

•  llFabs float version of llAbs

Articles

•  "Wikipedia logo"Absolute value

Deep Notes

Search JIRA for related Issues

Signature

function integer llAbs( integer val );

Haiku

Whether good or bad,
we are not here to judge you.
Just to ask... How much?