Difference between revisions of "LlRound"

From Second Life Wiki
Jump to navigation Jump to search
(Not an accuracy issue. N.5 is always exactly representable. It's just the behavior for negatives. Document it.)
(same applies to round as it does for floor for positive values, faster to just +0.5 and truncate to int)
 
(2 intermediate revisions by 2 users not shown)
Line 10: Line 10:
* The returned value is -2147483648 (0x80000000) if the arithmetic result is outside of the range of valid integers (-2147483648 to 2147483647 inclusive).
* The returned value is -2147483648 (0x80000000) if the arithmetic result is outside of the range of valid integers (-2147483648 to 2147483647 inclusive).
|constants
|constants
|examples=<lsl>default
|examples=<syntaxhighlight lang="lsl2">default
{
{
   state_entry()
   state_entry()
Line 32: Line 32:
       //Returns "The rounded value of -4.5 is: -4"
       //Returns "The rounded value of -4.5 is: -4"
     }
     }
}</lsl>
}</syntaxhighlight>
|helpers
|helpers
|also_functions={{LSL DefineRow||[[llCeil]]|Rounds the float to an integer towards positive infinity}}
|also_functions={{LSL DefineRow||[[llCeil]]|Rounds the float to an integer towards positive infinity}}
Line 39: Line 39:
|also_articles
|also_articles
|also_tests
|also_tests
|notes
|notes=*For positive values, it is quicker to add 0.5 to the value and cast to an integer. i=(integer)(f+0.5) produces less bytecode than i=llRound(f) and is about 5 times faster in execution, while giving the same result.
|permission
|permission
|negative_index
|negative_index

Latest revision as of 11:40, 9 October 2023

Summary

Function: integer llRound( float val );
0.0 Forced Delay
10.0 Energy

Returns the integer that val is closest to.

• float val Any valid float value

If the absolute value of the tenths position is 4 or less, val is rounded off; otherwise, val is rounded towards positive infinity.

Caveats

  • The returned value is -2147483648 (0x80000000) if the arithmetic result is outside of the range of valid integers (-2147483648 to 2147483647 inclusive).

Examples

default
{
   state_entry()
   {  
       llSay(0,  "The rounded value of -4.9 is: "+(string)llRound(-4.9) );
       //Returns "The rounded value of -4.9 is: -5"
       
       llSay(0,  "The rounded value of -4.1 is: "+(string)llRound(-4.1) );
       //Returns "The rounded value of -4.1 is: -4"
       
       llSay(0,  "The rounded value of 4.5 is: "+(string)llRound(4.5) );
       //Returns "The rounded value of 4.5 is: 5"
       
       llSay(0,  "The rounded value of 4.9 is: "+(string)llRound(4.9) );
       //Returns "The rounded value of 4.9 is: 5"
       
       llSay(0,  "The rounded value of 4.1 is: "+(string)llRound(4.1) );
       //Returns "The rounded value of 4.1 is: 4"

       llSay(0,  "The rounded value of -4.5 is: "+(string)llRound(-4.5) );
       //Returns "The rounded value of -4.5 is: -4"
    }
}

Notes

  • For positive values, it is quicker to add 0.5 to the value and cast to an integer. i=(integer)(f+0.5) produces less bytecode than i=llRound(f) and is about 5 times faster in execution, while giving the same result.

See Also

Functions

•  llCeil Rounds the float to an integer towards positive infinity
•  llFloor Rounds the float to an integer towards negative infinity

Deep Notes

Signature

function integer llRound( float val );