You can use abs() in OCL expressions when you need the non-negative magnitude of an Integer or Real value.
Signature
Number::abs() : Number
Result
Returns the absolute value of self.
- A negative number is returned without its minus sign.
- Zero is returned as zero.
- A positive number is returned unchanged.
(-12).abs() -- 12
0.abs() -- 0
4.5.abs() -- 4.5When to use abs()
Use abs() when the direction of a difference does not matter and you need its size.
For example, the following expression returns the distance between two numeric values as a non-negative number:
(self.ActualValue - self.ExpectedValue).abs()If ActualValue is 92 and ExpectedValue is 100, the result is 8. If the values are reversed, the result is also 8.
Notes
In OCL number operators, Number covers both Integer and Real values unless an operation specifies otherwise. abs() returns a Number; it does not round or otherwise convert the value.
Use floor() or round() when you need an integer result. Use min() or max() to select the lower or greater of two values.
