Mathematical operators and unset values
The complete source of operators_mathematical.ek9, compiled by the EK9 compiler. Shown on operators.html.
#!ek9 defines module site.operators.mathematical defines program Mathematical() stdout <- Stdout() //Initialise values a <- 21.9 b <- 13.6 c <- Float() d <- Float() //Addition examples e <- a + b //'e' would have the value of 35.5 e := b + a //'e' would again have the value of 35.5 //But in these following cases 'c' is not set and so the result is also not set e := a + c e := c + a //Also in these following cases both 'c' and 'd' are not set and so the result not set e := c + d e := d + c //Subtraction examples e := a - b //'e' = 8.299999999999999 e := b - a //'e' = -8.299999999999999 e := a - c //'e' = not set e := c - a //'e' = not set e := c - d //'e' = not set e := d - c //'e' = not set //Multiplication examples e := a * b //'e' = 297.84 e := b * a //'e' = 297.84 //all other combinations involving 'c' or 'd' result in 'e' being not set //Division examples e := a / b //'e' = 1.6102941176470587 e := b / a //'e' = 0.6210045662100457 //all other combinations involving 'c' or 'd' result in 'e' being not set //Factorial examples alpha <- 5 e := alpha! //'e' = 120.0 //Power examples beta <- 5 e := beta ^ 2 //'e' = 25.0 e := beta ^ 3 //'e' = 125.0 e := beta ^ 4.8 //'e' = 2264.936448992798 //Modulus and remainder examples charlie <- -21 f <- charlie mod 4 //'f' = 3 because -21 + 4 x 6 is 3 f := charlie rem 4 //'f' = -1 because -21 / 4 gives -5 with a remainder of -1 //For positive values the results are the same for modulus and remainder delta <- 21 f := delta mod 4 //'f' = 1 f := delta rem 4 //'f' = 1 //Absolute - abs example f := abs charlie //'f' = 21 f := abs delta //'f' = 21 echo <- -23.88 e := abs echo //'e' = 23.88 //Square root - sqrt example e := sqrt delta //'e' = 4.58257569495584 e := sqrt abs echo //'e' = 4.886716689148247 //Note the combination of sqrt and abs. e := sqrt echo //'e' = not set cannot sqrt a negative number //EK9 does not have imaginary numbers so sqrt of a negative number is not valid stdout.println(`${e?} ${f}`)
Download operators_mathematical.ek9
Output
false 21