6.2. Converting Between Number Types

There are two functions which allow you to convert between number types: to_int and to_float


   // simple to_int examples
   to_int(1 + 2)        = 3
   to_int(1.0 + 2.0)    = 3
   to_int(6.0 / 2.0)    = 3

   // complex to_int examples
   to_int(1.0 * 0.5)          = 0
   to_int(5 / 3)              = 1
   to_int(5.0 / 3.0)          = 1
   to_int((5 / 3) * 3)        = 3
   to_int((5.0 / 3.0) * 3.0)  = 5

   // simple to_float examples
   to_float(1 + 2)      = 3.0
   to_float(10 / 5)     = 2.0

   // complex to_float examples
   to_float(5 / 3)              = 1.0
   to_float(5 / 3.0)            = 1.666667
   to_float(5.0 / 3)            = 1.666667
   to_float(5.0 / 3.0)          = 1.666667
   to_float((5 / 3) * 3)        = 3.00000
   to_float((5.0 / 3.0) * 3.0)  = 5.00000

Some of those examples may look confusing until after you've read the section on Operations on Numbers in LPC next.

Casting only converts types--it doesn't convert values. Let's walk through some examples to make this distinction clearer.


   (int) 1            = 1
   (float) 1          = 1
   to_int(1)          = 1
   to_float(1)        = 1.00000

   (int) 1.0          = 1.0
   (float) 1.0        = 1.0
   to_int(1.0)        = 1
   to_float(1.0)      = 1.00000