# Difference between revisions of "Category:LSL Float"

(→Useful Snippets: added a less efficient method) |
(→Useful Snippets: Gave Strife's function precedence since it's the more robust, clarified the caveats of mine (having investigated them a bit more fully).) |
||

Line 29: | Line 29: | ||

== Useful Snippets == | == Useful Snippets == | ||

<div style="padding: 0.5em"> | <div style="padding: 0.5em"> | ||

− | + | If you need to validate an arbitrary float without limitations then the following function is ideal: | |

− | + | ||

− | + | ||

− | If you need to validate an arbitrary float without | + | |

<lsl>integer isValidFloat(string s) { return (string)((float)s) != (string)((float)("-" + llStringTrim(s, STRING_TRIM_HEAD))); }</lsl> | <lsl>integer isValidFloat(string s) { return (string)((float)s) != (string)((float)("-" + llStringTrim(s, STRING_TRIM_HEAD))); }</lsl> | ||

+ | |||

+ | However, the following is more efficient, but comes with the noted caveats. If these are not an issue to you then it is the recommended option, particularly under Mono: | ||

+ | <lsl>integer isValidFloat(string s) { return (float)(s + "1") != 0.0; }</lsl> | ||

+ | '''Caveats''': | ||

+ | * Under LSO-LSL scientific notation with an exponent greater than 3 will fail (throw a Math Error). Mono is unaffected as it supports <code>infinity</code>. | ||

+ | * Under both Mono and LSO-LSL you may find strange results if dealing with strings containing more than 9 decimal places. Remember that string casting in LSL only gives up to 6 so is safe, and human input is rarely going to be that accurate, plus values that small are not usually all that useful. | ||

</div></div> | </div></div> | ||

## Revision as of 15:15, 22 October 2010

LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |

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

If you need to validate an arbitrary float without limitations then the following function is ideal: <lsl>integer isValidFloat(string s) { return (string)((float)s) != (string)((float)("-" + llStringTrim(s, STRING_TRIM_HEAD))); }</lsl>

However, the following is more efficient, but comes with the noted caveats. If these are not an issue to you then it is the recommended option, particularly under Mono:
<lsl>integer isValidFloat(string s) { return (float)(s + "1") != 0.0; }</lsl>
**Caveats**:

- Under LSO-LSL scientific notation with an exponent greater than 3 will fail (throw a Math Error). Mono is unaffected as it supports
`infinity`

. - Under both Mono and LSO-LSL you may find strange results if dealing with strings containing more than 9 decimal places. Remember that string casting in LSL only gives up to 6 so is safe, and human input is rarely going to be that accurate, plus values that small are not usually all that useful.

## See Also

**Articles**

- LSL Examples: Format Decimal
- LSL Examples: Float2String

## Pages in category "LSL Float"

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

## D## F |
## P## R |
## S## T |