Unofficial LSL Reference

[[types:float]]


Unofficial LSL reference

User Tools

Login

You are currently not logged in! Enter your authentication credentials below to log in. You need to have cookies enabled to log in.

Login

Forgotten your password? Get a new one: Set new password

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

types:float [2015-02-04 19:02 SLT]
sei formatting, style
types:float [2015-09-22 11:28 SLT] (current)
sei missed styling a word
Line 2: Line 2:
 ===== Float type ===== ===== Float type =====
  
-The $ty[float] type represents a number with decimals, as opposed to the [[integer]] type which is for whole numbers only.+The $ty[float] type represents a number with decimals, as opposed to the $lty[integer] type which is for whole numbers only.
  
 Floating-point ($ty[float]) constants can be entered in two formats. The first is by just writing the number with its sign, the decimal point (mandatory for the constant to actually be a float and not an integer), and the decimals. For example, ''​3.7''​ or ''​-5.4164''​ are valid float constants. Neither the part to the left of the point nor the part to the right of the point are mandatory (e.g. ''​37.''​ or ''​-.25''​ are valid), but a point without any digits around it is not a valid floating-point constant. Floating-point ($ty[float]) constants can be entered in two formats. The first is by just writing the number with its sign, the decimal point (mandatory for the constant to actually be a float and not an integer), and the decimals. For example, ''​3.7''​ or ''​-5.4164''​ are valid float constants. Neither the part to the left of the point nor the part to the right of the point are mandatory (e.g. ''​37.''​ or ''​-.25''​ are valid), but a point without any digits around it is not a valid floating-point constant.
Line 12: Line 12:
 The range of a float is approximately from -3.4028234e38 to 3.4028234e38. The smallest positive number that it can represent is approx. 1.4e-45. It can also have the value minus zero (-0.0) but that one is usually not distinguished from a regular zero. In Mono, a float can also have three special values: $pinf, $minf, and $nan (Not a Number). In LSO, however, any operation that would result in any of these values will instead cause a math error and stop the script until it's reset. The range of a float is approximately from -3.4028234e38 to 3.4028234e38. The smallest positive number that it can represent is approx. 1.4e-45. It can also have the value minus zero (-0.0) but that one is usually not distinguished from a regular zero. In Mono, a float can also have three special values: $pinf, $minf, and $nan (Not a Number). In LSO, however, any operation that would result in any of these values will instead cause a math error and stop the script until it's reset.
  
-=== Automatic type conversion between float and integer ===+==== Automatic type conversion between float and integer ​====
  
 Most places that accept a $ty[float]-type value also accept an $ty[integer]-type value, which will be automatically converted to $ty[float] if necessary. For example: Most places that accept a $ty[float]-type value also accept an $ty[integer]-type value, which will be automatically converted to $ty[float] if necessary. For example:
Line 41: Line 41:
 </​code>​ </​code>​
  
-=== Precision ===+==== Precision ​====
  
 $ty[float] values are [[http://​en.wikipedia.org/​wiki/​Single-precision_floating-point_format|single-precision]] floats. An implication is that their precision is 6 to 9 significant digits, that is, total digits starting from the leftmost non-zero digit. $ty[float] values are [[http://​en.wikipedia.org/​wiki/​Single-precision_floating-point_format|single-precision]] floats. An implication is that their precision is 6 to 9 significant digits, that is, total digits starting from the leftmost non-zero digit.
Line 48: Line 48:
  
 Due to the way floats work, the closer to zero they are the more precision after the decimal point they have, and vice versa. Numbers above 8,388,608.0 or less than -8,​388,​608.0 don't have any decimal places at all, while numbers between -1.0 and 1.0 have at least 6 decimal places, possibly more depending on how close to zero they are. Due to the way floats work, the closer to zero they are the more precision after the decimal point they have, and vice versa. Numbers above 8,388,608.0 or less than -8,​388,​608.0 don't have any decimal places at all, while numbers between -1.0 and 1.0 have at least 6 decimal places, possibly more depending on how close to zero they are.
 +
 +=== Type casting a float to string ===
  
 When they are typecast to $lty[string],​ floats are always translated with all of their digits to the left of the decimal point, and six decimal places to the right, rounded. However, as an exception, when a $lty[vector] (a collection of three floats) or a $lty[rotation] (a collection of four floats) is typecast to $ty[string],​ its floats are converted using five decimal places to the right of the decimal point. This exception does not apply if they are inside a list and the list is converted to string. When they are typecast to $lty[string],​ floats are always translated with all of their digits to the left of the decimal point, and six decimal places to the right, rounded. However, as an exception, when a $lty[vector] (a collection of three floats) or a $lty[rotation] (a collection of four floats) is typecast to $ty[string],​ its floats are converted using five decimal places to the right of the decimal point. This exception does not apply if they are inside a list and the list is converted to string.
Line 93: Line 95:
 </​code>​ </​code>​
  
-Floats are represented internally in base 2, not in base 10. That leads many people to confusion, because some numbers can't be represented accurately in base 2, and rounding errors occur. It's not a bug; it's a limitation of the type, and a necessary compromise. For example:+The float value $nan is translated to the string ''​NaN'',​ no matter the kind; the float value $pinf is translated to the string ''​Infinity''​ and the float value $minf to the string ''​-Infinity''​. This happens also with other functions that convert floats to string (e.g. $lfn[llList2String],​ $lfn[llDumpList2String]),​ except one: $lfn[llList2CSV] translates the float value $nan to the string ''​nan''​ (if it's regular NaN) or ''​-nan''​ (if it's the indeterminate kind of NaN), and translates $pinf and $minf to ''​inf''​ and ''​-inf''​ respectively. 
 + 
 +=== Precision caveats === 
 + 
 +Floats are represented internally in base 2, not in base 10. That leads many people to confusion, because some decimal ​numbers can't be represented accurately in base 2, and rounding errors occur. It's not a bug; it's a limitation of the type, and a necessary compromise. For example:
  
 <file lsl2 float-equal.lsl>​ <file lsl2 float-equal.lsl>​