# Kotlin | Math.abs() method with Examples

The Math.abs() function returns the absolute value of a given argument. If the argument is non-negative, the argument itself is returned. whereas, if the argument is negative, it’s negation value is returned. Basically, it works as the modulus function in mathematics.

Syntax : fun abs(x : DataType) : DataType

Parameters: It can take values of Data type int, double, long, float.

Returns: It returns absolute value of the argument, without changing the data type.

Exceptions:

• If the argument is NaN, the result is NaN.
• If the argument is Int.MIN_VALUE, the result is that same value, Int.MIN_VALUE, which is negative.
• If the argument is Long.MIN_VALUE, the result if that same value, Long.MIN_VALUE, which is negative.

Code #1: Taking float and double data types as argument.

 `// Kotlin program to illustrate ` `// working of Math.abs() method  ` `import` `kotlin.math.abs ` ` `  `fun main(args : Array){ ` ` `  `    ``val f = -``45``.23f; ` `    ``val d = ``999.32``; ` `   `  `    ``// abs() function taking float as input ` `    ``println(abs(f)); ` ` `  `    ``// abs() function taking double as input ` `    ``println(abs(d)); ` `} `

Output:

```45.23
999.32
```

Code #2: Taking int and long data types as argument.

 `// Kotlin program to illustrate ` `// working of Math.abs() method  ` `import` `kotlin.math.abs ` ` `  `fun main(args : Array){ ` ` `  `    ``val i = -``0``; ` `    ``val l = -``69973688``; ` `   `  `    ``// abs() function taking int as input ` `    ``println(abs(i)); ` `    ``println(abs(Int.MIN_VALUE)); ` ` `  `    ``// abs() function taking long as input ` `    ``println(abs(l)); ` `    ``println(abs(Long.MIN_VALUE)); ` `} `

Output:

```0
-2147483648

69973688
-9223372036854775808
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.