Class Fixed

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.

Accuracy

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".

Overflow and Underflow

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.

CONSTANTS

CONSTRUCTION

COMPARISION

OPERATORS

CONVERSION

OTHER METHODS


(fixed) fixed.minimum value

A constant containing the smallest value a fixed can hold.

Return value

Example

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
                    

(fixed) fixed.maximum value

A constant containing the greatest value a fixed can hold.

Return value

Example

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
                    

(fixed) <fixed source>.copy

Create a copy of the fixed.

Parameters

Return value

Example

main
  fixed f1 = 3.125
  fixed f2 = f1.copy
  print f2                // LOG: 3.125
                    

(fixed) <byte b>

Create a new fixed from the byte.

Parameters

Return value

Example

main
  byte b = 5.truncate
  fixed f = b
  print f                    // LOG: 5.0
                    

(fixed) <int v>

Create a new fixed from the integer.

Parameters

Return value

Example

main
  int v = -3
  fixed f = v
  print f                   // LOG: -3.0				
                    

(fixed) <string s>.as fixed

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.

Parameters

Return value

Example

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
                    

(bool) <fixed f1> == <fixed f2>

Comparision

Parameters

Return value

Example

main
  if (-3.17 == -3.17)
    print "yes"    // LOG: yes
    				

(bool) <fixed f1> != <fixed f2>

Comparision

Parameters

Return value

Example

main
  if (2.16 != 3.17)
    print "yes"    // LOG: yes
    				

(bool) <fixed f1> < <fixed f2>

Comparision

Parameters

Return value

Example

main
  if (2.16 < 3.17)
    print "yes"    // LOG: yes
    				

(bool) <fixed f1> <= <fixed f2>

Comparision

Parameters

Return value

Example

main
  if (2.16 <= 2.16)
    print "yes"    // LOG: yes
    				

(bool) <fixed f1> > <fixed f2>

Comparision

Parameters

Return value

Example

main
  if (3.17 > 2.16)
    print "yes"    // LOG: yes
    				

(bool) <fixed f1> >= <fixed f2>

Comparision

Parameters

Return value

Example

main
  if (3.17 >= 3.17)
    print "yes"    // LOG: yes
    				

(fixed) -<fixed f>

Return negative of 'f'

Parameters

Return value

Example

main
  fixed f

  f = 3.5
  print -f               // LOG: -3.5

  f = -10.75
  print -f               // LOG: 10.75
    				

(fixed) <fixed f1> + <fixed f2>

Return sum of 'f1' and 'f2'.

Parameters

Return value

Example

main
  fixed f = 3.5 + 1.25
  print f               // LOG: 4.75
    				

(fixed) <fixed f1> - <fixed f2>

Return the difference of 'f1' and 'f2'.

Parameters

Return value

Example

main
  fixed f = 3.5 - 1.25
  print f               // LOG: 2.25
    				

(fixed) <fixed f1> * <fixed f2>

Return the product of 'f1' and 'f2'.

Parameters

Return value

Example

main
  fixed f = 3.5 * 1.25
  print f               // LOG: 4.375
    				

(fixed) <fixed f1> / <fixed f2>

Return the quotient of 'f1' and 'f2'.

Parameters

Return value

Example

main
  fixed f = 3.5 / 1.25
  print f               // LOG: 2.799999999813735485076904296875
    				

(fixed) <fixed f1> ** <fixed f2>

Return value 'f1' raised to the power 'f2'. The overflow is handled normally by setting the return value to the minimum or maximum value.

Parameters

Return value

Example

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
                    

<fixed f1> += <fixed f2>

Add 'f2' to 'f1'

Parameters

Example

main
  fixed f = 3.5
  f += 1.25
  print f               // LOG: 4.75
    				

<fixed f1> -= <fixed f2>

Subtract 'f2' from 'f1'

Parameters

Example

main
  fixed f = 3.5
  f -= 1.25
  print f               // LOG: 2.25
    				

<fixed f1> *= <fixed f2>

Multiply 'f1' with 'f2'

Parameters

Example

main
  fixed f = 3.5
  f *= 1.25
  print f               // LOG: 4.375
    				

<fixed f1> /= <fixed f2>

Divide 'f1' by 'f2'

Parameters

Example

main
  fixed f = 3.5
  f /= 1.25
  print f               // LOG: 2.799999999813735485076904296875
    				

<fixed f>++

Add 1.0 to 'f'

Parameters

Example

main
  fixed f = 13.0
  f++
  print f               // LOG: 14.0
    				

<fixed f>--

Subtract 1.0 from 'f'

Parameters

Example

main
  fixed f = 13.0
  f--
  print f               // LOG: 12.0
    				

(string) <fixed f>

Return 'f' as a string.

Parameters

Return value

Example

main
  fixed f = math.pi

  print f               // LOG: 3.14159265370108187198638916015625
    				

(string) as string with <int n> decimals

Return the fixed as a string, where the length of the decimal part is 'n'.

Parameters

Return value

Example

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
    				

(int) round

Return the fixed rounded to the nearest integer.

Return value

Example

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
    			    

(int) round up

Return the fixed rounded upwards to the nearest integer. Also called as ceil.

Return value

Example

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
    			    

(int) round down

Return the fixed rounded downwards to the nearest integer. Also called as floor.

Return value

Example

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
    			    

(fixed) abs

Return the absolute value of the fixed i.e. its unsigned magnitude.

Return value

Example

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