Difference between revisions of "Category:LSL Float"

From Second Life Wiki
Jump to navigation Jump to search
(→‎Useful Snippets: I put the "1" at the wrong end! Also added a note)
(→‎Useful Snippets: added a less efficient method)
Line 31: Line 31:
The following is a useful way to validate a string that may be a float; it isn't 100% accurate (to do so is considerably more complex), however it will match all but the most extreme float values; so as long as you are using what are typical values in LSL (don't really go beyond 4 or 5 decimal places, or beyond the billions in values) then it is by far the most efficient method.
The following is a useful way to validate a string that may be a float; it isn't 100% accurate (to do so is considerably more complex), however it will match all but the most extreme float values; so as long as you are using what are typical values in LSL (don't really go beyond 4 or 5 decimal places, or beyond the billions in values) then it is by far the most efficient method.
<lsl>integer isValidFloat(string s) { return (float)(s + "1") != 0.0; }</lsl>
<lsl>integer isValidFloat(string s) { return (float)(s + "1") != 0.0; }</lsl>
If you need to validate an arbitrary float without the above limitations the following is more robust. However it is less efficient.
<lsl>integer isValidFloat(string s) { return (string)((float)s) != (string)((float)("-" + llStringTrim(s, STRING_TRIM_HEAD))); }</lsl>
</div></div>
</div></div>



Revision as of 08:17, 22 October 2010

Floating point data types are 32 bit numbers in IEEE-754 form. If you want a decimal point in your number, then it is a float.

The Range is 1.175494351E-38 to 3.402823466E+38

They can be specified in scientific notation like 2.6E-5.

If a function requires a float as a parameter, and the number is an integer (e.g. 5), be sure to add a .0 so it is created as a float (e.g. 5.0)

If you are dividing 2 constants, be sure to define them as floats or your result may get rounded. Better yet, do the math on your calculator and save the server some cycles.

Examples

<lsl>float min = 1.175494351E-38; float max = 3.402823466E+38; float sci = 2.6E-5; float sci_a = 2.6E+3; float sci_b = 2.6E3; float sci_c = 26000.E-1; float f = 2600;//implicitly typecast to a float float E = 85.34859; </lsl>

Useful Snippets

The following is a useful way to validate a string that may be a float; it isn't 100% accurate (to do so is considerably more complex), however it will match all but the most extreme float values; so as long as you are using what are typical values in LSL (don't really go beyond 4 or 5 decimal places, or beyond the billions in values) then it is by far the most efficient method. <lsl>integer isValidFloat(string s) { return (float)(s + "1") != 0.0; }</lsl>

If you need to validate an arbitrary float without the above limitations the following is more robust. However it is less efficient. <lsl>integer isValidFloat(string s) { return (string)((float)s) != (string)((float)("-" + llStringTrim(s, STRING_TRIM_HEAD))); }</lsl>

See Also

Articles

Subcategories

This category has only the following subcategory.

Pages in category "LSL Float"

The following 8 pages are in this category, out of 8 total.