main
fixed f
f = fixed.minimum value
print f // LOG: -2147483648.0
// Note the underflow handling
f = fixed.minimum value - 1000.0
print f // LOG: -2147483648.0
Type fixed is for handling real numbers like 2.3 and 0.0001.
The greatest value it can hold is 2147483647.99999999978... and the smallest one is -2147483648.0, so the range includes the range of int.
Fixed type implements several methods. All the basic arithmetic operators exist: +, -, *, / and more specific power operator: **. There are special methods like negation (unary minus), absolute value (abs), rounding up and down, type conversions, converting a real number in a string to fixed, and vice versa. See also class math, which has more numeric methods for fixeds like trigonometry, logarithm, and random fixed generator.
Fixed is implemented using fixed-point arithmetic. A fixed is 64-bit wide, such that it has 32-bits for the integer part and 32-bits for the fractional part. Fixed-point number is not as accurate as the floating-point numbers (for instance float or double in the C language), but unfortunately many embedded microprocessors does not have a floating-point calculation unit. Luckily, thank's to our fixed's 64-bits, we have a good accuracy and a very good performance also. An example of inaccuracy is a real value 0.33. Converting it to a fixed variable results 0.32999999984167516231536865234375, so the difference is smaller than 1e-9. Tip: use the method 'as string with <int> decimals' to avoid printing long fractional part like here. For instance, 'print (0.33).as string with 2 decimals' prints nicely "0.33".
Fixed automatically handles the overflow and underflow situations for you. In case of overflow/underflow the resulting value is set to the corresponding maximum or minimum value. For instance, 2144478.0 * 2144478.0 results fixed.maximum value and fixed.minimum value * 2.0 results fixed.minimum value. The same applies with the math methods also.
A constant containing the smallest value a fixed can hold.
main
fixed f
f = fixed.minimum value
print f // LOG: -2147483648.0
// Note the underflow handling
f = fixed.minimum value - 1000.0
print f // LOG: -2147483648.0
A constant containing the greatest value a fixed can hold.
main
fixed f
f = fixed.maximum value
print f // LOG: 2147483647.99999999976716935634613037109375
// Note the overflow handling
f = fixed.maximum value + 1000.0
print f // LOG: 2147483647.99999999976716935634613037109375
Create a copy of the fixed.
main
fixed f1 = 3.125
fixed f2 = f1.copy
print f2 // LOG: 3.125
Create a new fixed from the byte.
main
byte b = 5.truncate
fixed f = b
print f // LOG: 5.0
Create a new fixed from the integer.
main
int v = -3
fixed f = v
print f // LOG: -3.0
Create a new fixed from the real number in string 's'. All prefixing white-spaces are ignored, but then one or more digits are expected. The decimal part separated by comma '.' is optional. Rest of the string is ignored.
main
string s
fixed f
s = " -2.125"
f = s.as fixed
print f // LOG: -2.125
// Note that the method extracts the first value only
s = " -2.125#3.1786"
f = s.as fixed
print f // LOG: -2.125
// Decimal part is optional
s = " -2 "
f = s.as fixed
print f // LOG: -2.0
Comparision
main
if (-3.17 == -3.17)
print "yes" // LOG: yes
Comparision
main
if (2.16 != 3.17)
print "yes" // LOG: yes
Comparision
main
if (2.16 < 3.17)
print "yes" // LOG: yes
Comparision
main
if (2.16 <= 2.16)
print "yes" // LOG: yes
Comparision
main
if (3.17 > 2.16)
print "yes" // LOG: yes
Comparision
main
if (3.17 >= 3.17)
print "yes" // LOG: yes
Return negative of 'f'
main
fixed f
f = 3.5
print -f // LOG: -3.5
f = -10.75
print -f // LOG: 10.75
Return sum of 'f1' and 'f2'.
main
fixed f = 3.5 + 1.25
print f // LOG: 4.75
Return the difference of 'f1' and 'f2'.
main
fixed f = 3.5 - 1.25
print f // LOG: 2.25
Return the product of 'f1' and 'f2'.
main
fixed f = 3.5 * 1.25
print f // LOG: 4.375
Return the quotient of 'f1' and 'f2'.
main
fixed f = 3.5 / 1.25
print f // LOG: 2.799999999813735485076904296875
Return value 'f1' raised to the power 'f2'. The overflow is handled normally by setting the return value to the minimum or maximum value.
main
fixed f
f = 0.0 ** 1.0
print f // LOG: 0.0
f = 1.0 ** 1000.0
print f // LOG: 1.0
f = 2.0 ** 16.0
print f // LOG: 65536.0
f = 2.0 ** -4.0
print f // LOG: 0.0625
f = (-2.0) ** 3.0
print f // LOG: -8.0
f = 5.0 ** 2.0
print f // LOG: 25.0
f = fixed.minimum value ** fixed.maximum value
print f // LOG: 2147483647.99999999976716935634613037109375
f = fixed.maximum value ** fixed.minimum value
print f // LOG: 0.0
Add 'f2' to 'f1'
main
fixed f = 3.5
f += 1.25
print f // LOG: 4.75
Subtract 'f2' from 'f1'
main
fixed f = 3.5
f -= 1.25
print f // LOG: 2.25
Multiply 'f1' with 'f2'
main
fixed f = 3.5
f *= 1.25
print f // LOG: 4.375
Divide 'f1' by 'f2'
main
fixed f = 3.5
f /= 1.25
print f // LOG: 2.799999999813735485076904296875
Add 1.0 to 'f'
main
fixed f = 13.0
f++
print f // LOG: 14.0
Subtract 1.0 from 'f'
main
fixed f = 13.0
f--
print f // LOG: 12.0
Return 'f' as a string.
main
fixed f = math.pi
print f // LOG: 3.14159265370108187198638916015625
Return the fixed as a string, where the length of the decimal part is 'n'.
main
fixed f = math.pi
print f.as string with 0 decimals // LOG: 3
print f.as string with 1 decimals // LOG: 3.1
print f.as string with 2 decimals // LOG: 3.14
print f.as string with 3 decimals // LOG: 3.142
print f.as string with 4 decimals // LOG: 3.1416
print f.as string with 5 decimals // LOG: 3.14159
f = -5.0
print f.as string with 10 decimals // LOG: -5.0000000000
Return the fixed rounded to the nearest integer.
main
int v
v = (0.001).round
print v // LOG: 0
v = (0.4).round
print v // LOG: 0
v = (1.6).round
print v // LOG: 2
v = (5.0).round
print v // LOG: 5
v = (7.2).round
print v // LOG: 7
v = (-5.2).round
print v // LOG: -5
v = (-5.99).round
print v // LOG: -6
Return the fixed rounded upwards to the nearest integer. Also called as ceil.
main
int v
v = (0.001).round up
print v // LOG: 1
v = (0.4).round up
print v // LOG: 1
v = (1.6).round up
print v // LOG: 2
v = (5.0).round up
print v // LOG: 5
v = (7.2).round up
print v // LOG: 8
v = (-5.2).round up
print v // LOG: -5
v = (-5.99).round up
print v // LOG: -5
Return the fixed rounded downwards to the nearest integer. Also called as floor.
main
int v
v = (0.001).round down
print v // LOG: 0
v = (0.4).round down
print v // LOG: 0
v = (1.6).round down
print v // LOG: 1
v = (5.0).round down
print v // LOG: 5
v = (7.2).round down
print v // LOG: 7
v = (-5.2).round down
print v // LOG: -6
v = (-5.99).round down
print v // LOG: -6
Return the absolute value of the fixed i.e. its unsigned magnitude.
main
fixed f
f = -4.25
print f.abs // LOG: 4.25
// Note the slight inaccuracy with a fixed-point
f = 13.33333
print f.abs // LOG: 13.33332999981939792633056640625