Scala abs - Syntax
mixed abs(mixed n);
The abs() function in Scala is used to return the absolute value or modulus |x| of a real number x is the non-negative value of x without regard to its sign.
mixed abs(mixed n);
mixed n |
Specifies the real number (int, long, float, double) / infinity (*Required) |
---|---|
Return mixed |
Returns a number / infinity respective to an input argument. |
Convert the positive and negative integer to an absolute value.
object MathExample {
def main(args: Array[String]) = {
// Positive integer value
val n = (9).abs;
println("The absolute value of 9 is " + n);
// Negative integer value
val x = (-6).abs;
println("The absolute value of -6 is " + x);
}
}
Convert the positive and negative float to an absolute value.
object MathExample {
def main(args: Array[String]) = {
// Positive floating point value
val n = (9.3).abs;
println("The absolute value of 9.3 is " + n);
// Negative floating point value
val x = (-6.3).abs;
println("The absolute value of -6.3 is " + x);
}
}
Convert the positive and negative Infinity to an absolute value.
object MathExample {
def main(args: Array[String]) = {
// Positive ∞ infinite value
val n = (Double.PositiveInfinity).abs;
println("The absolute value of Infinity is " + n);
// Negative ∞ infinite value
val x = (Double.NegativeInfinity).abs;
println("The absolute value of -Infinity is " + x);
}
}