Get the absolute value of -42 and the square root of 16.
← Operators and Expressions · Ref: Q1104
Prefix unary operators:
positive <- abs negVal side <- sqrt area
Keyword operators, not method calls -- no parentheses needed. abs works on Integer and Float; sqrt on Float. See Q240 for arithmetic operators.
Example
defines module qa.operators.unaryabssqrt defines program UnaryAbsSqrtDemo() stdout <- Stdout() //Absolute value of a negative integer negVal <- -42 positive <- abs negVal stdout.println(`abs -42: ${positive}`) //Square root of a float area <- 16.0 side <- sqrt area stdout.println(`sqrt 16.0: ${side}`) //Absolute value of a negative float temperature <- -5.7 magnitude <- abs temperature stdout.println(`abs -5.7: ${magnitude}`)
Common mistakes
E50001 — EK9 has no Math class; `Math.abs(negVal)` leaves 'Math' unresolved and triggers E50001 - use the 'abs' prefix operator: abs negVal. See ek9 -h E50001 for details.
Incorrect:
positive <- Math.abs(negVal)
Correct:
positive <- abs negVal
Other ways to ask this
- I need to remove the sign from a negative number and compute a square root
- In Java I'd use Math.abs() and Math.sqrt(). Write the EK9 equivalents
- Given a negative integer and a float, apply abs and sqrt prefix operators
- Compute absolute value and square root using EK9's unary prefix operators
Coming from another language?
Java: Math.abs(x), Math.sqrt(x). Python: abs(x), math.sqrt(x). Rust: x.abs(), x.sqrt(). EK9: abs x, sqrt x — prefix operators, not functions.
Keywords: absolute, sqrt, math, square root, prefix, abs, unary