abs

expect fun abs(x: Double): Double(source)

Returns the absolute value of the given value x.

Special cases:

  • abs(NaN) is NaN

Since Kotlin

1.2

See also

extension property for Double

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(3.14)) // 3.14
println(abs(-3.14)) // 3.14
println(abs(-0.0)) // 0.0
println(abs(Double.NEGATIVE_INFINITY)) // Infinity
println(abs(Double.NaN)) // NaN 
   //sampleEnd
}

expect fun abs(x: Float): Float(source)

Returns the absolute value of the given value x.

Special cases:

  • abs(NaN) is NaN

Since Kotlin

1.2

See also

extension property for Float

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(3.14f.absoluteValue) // 3.14
println((-3.14f).absoluteValue) // 3.14
println((-0.0f).absoluteValue) // 0.0
println(Float.NEGATIVE_INFINITY.absoluteValue) // Infinity
println(Float.NaN.absoluteValue) // NaN 
   //sampleEnd
}

expect fun abs(n: Int): Int(source)

Returns the absolute value of the given value n.

Special cases:

  • abs(Int.MIN_VALUE) is Int.MIN_VALUE due to an overflow

Since Kotlin

1.2

See also

extension property for Int

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(42)) // 42
println(abs(-42)) // 42
// Special case: can't get the absolute value due to an overflow
println("abs(Int.MIN_VALUE) == Int.MIN_VALUE is ${abs(Int.MIN_VALUE) == Int.MIN_VALUE}") // true 
   //sampleEnd
}

expect fun abs(n: Long): Long(source)

Returns the absolute value of the given value n.

Special cases:

  • abs(Long.MIN_VALUE) is Long.MIN_VALUE due to an overflow

Since Kotlin

1.2

See also

extension property for Long

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(42L)) // 42
println(abs(-42L)) // 42
// Special case: can't get the absolute value due to an overflow
println("abs(Long.MIN_VALUE) == Long.MIN_VALUE is ${abs(Long.MIN_VALUE) == Long.MIN_VALUE}") // true 
   //sampleEnd
}
actual inline fun abs(x: Double): Double(source)

Returns the absolute value of the given value x.

Special cases:

  • abs(NaN) is NaN

Since Kotlin

1.2

See also

extension property for Double

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(3.14)) // 3.14
println(abs(-3.14)) // 3.14
println(abs(-0.0)) // 0.0
println(abs(Double.NEGATIVE_INFINITY)) // Infinity
println(abs(Double.NaN)) // NaN 
   //sampleEnd
}

actual inline fun abs(x: Float): Float(source)

Returns the absolute value of the given value x.

Special cases:

  • abs(NaN) is NaN

Since Kotlin

1.2

See also

extension property for Float

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(3.14f)) // 3.14
println(abs(-3.14f)) // 3.14
println(abs(-0.0f)) // 0.0
println(abs(Float.NEGATIVE_INFINITY)) // Infinity
println(abs(Float.NaN)) // NaN 
   //sampleEnd
}

actual fun abs(n: Int): Int(source)

Returns the absolute value of the given value n.

Special cases:

  • abs(Int.MIN_VALUE) is Int.MIN_VALUE due to an overflow

Since Kotlin

1.2

See also

extension property for Int

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(42)) // 42
println(abs(-42)) // 42
// Special case: can't get the absolute value due to an overflow
println("abs(Int.MIN_VALUE) == Int.MIN_VALUE is ${abs(Int.MIN_VALUE) == Int.MIN_VALUE}") // true 
   //sampleEnd
}

actual fun abs(n: Long): Long(source)

Returns the absolute value of the given value n.

Special cases:

  • abs(Long.MIN_VALUE) is Long.MIN_VALUE due to an overflow

Since Kotlin

1.2

See also

extension property for Long

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(42L)) // 42
println(abs(-42L)) // 42
// Special case: can't get the absolute value due to an overflow
println("abs(Long.MIN_VALUE) == Long.MIN_VALUE is ${abs(Long.MIN_VALUE) == Long.MIN_VALUE}") // true 
   //sampleEnd
}
actual inline fun abs(x: Double): Double(source)

Returns the absolute value of the given value x.

Special cases:

  • abs(NaN) is NaN

Since Kotlin

1.2

See also

extension property for Double

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(3.14)) // 3.14
println(abs(-3.14)) // 3.14
println(abs(-0.0)) // 0.0
println(abs(Double.NEGATIVE_INFINITY)) // Infinity
println(abs(Double.NaN)) // NaN 
   //sampleEnd
}

actual inline fun abs(x: Float): Float(source)

Returns the absolute value of the given value x.

Special cases:

  • abs(NaN) is NaN

Since Kotlin

1.2

See also

extension property for Float

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(3.14f)) // 3.14
println(abs(-3.14f)) // 3.14
println(abs(-0.0f)) // 0.0
println(abs(Float.NEGATIVE_INFINITY)) // Infinity
println(abs(Float.NaN)) // NaN 
   //sampleEnd
}

actual inline fun abs(n: Int): Int(source)

Returns the absolute value of the given value n.

Special cases:

  • abs(Int.MIN_VALUE) is Int.MIN_VALUE due to an overflow

Since Kotlin

1.2

See also

extension property for Int

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(42)) // 42
println(abs(-42)) // 42
// Special case: can't get the absolute value due to an overflow
println("abs(Int.MIN_VALUE) == Int.MIN_VALUE is ${abs(Int.MIN_VALUE) == Int.MIN_VALUE}") // true 
   //sampleEnd
}

actual inline fun abs(n: Long): Long(source)

Returns the absolute value of the given value n.

Special cases:

  • abs(Long.MIN_VALUE) is Long.MIN_VALUE due to an overflow

Since Kotlin

1.2

See also

extension property for Long

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(42L)) // 42
println(abs(-42L)) // 42
// Special case: can't get the absolute value due to an overflow
println("abs(Long.MIN_VALUE) == Long.MIN_VALUE is ${abs(Long.MIN_VALUE) == Long.MIN_VALUE}") // true 
   //sampleEnd
}
actual external fun abs(x: Double): Double(source)

Returns the absolute value of the given value x.

Special cases:

  • abs(NaN) is NaN

Since Kotlin

1.3

See also

extension property for Double

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(3.14)) // 3.14
println(abs(-3.14)) // 3.14
println(abs(-0.0)) // 0.0
println(abs(Double.NEGATIVE_INFINITY)) // Infinity
println(abs(Double.NaN)) // NaN 
   //sampleEnd
}

actual external fun abs(x: Float): Float(source)

Returns the absolute value of the given value x.

Special cases:

  • abs(NaN) is NaN

Since Kotlin

1.3

See also

extension property for Float

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(3.14f)) // 3.14
println(abs(-3.14f)) // 3.14
println(abs(-0.0f)) // 0.0
println(abs(Float.NEGATIVE_INFINITY)) // Infinity
println(abs(Float.NaN)) // NaN 
   //sampleEnd
}

actual external fun abs(n: Int): Int(source)

Returns the absolute value of the given value n.

Special cases:

  • abs(Int.MIN_VALUE) is Int.MIN_VALUE due to an overflow

Since Kotlin

1.3

See also

extension property for Int

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(42)) // 42
println(abs(-42)) // 42
// Special case: can't get the absolute value due to an overflow
println("abs(Int.MIN_VALUE) == Int.MIN_VALUE is ${abs(Int.MIN_VALUE) == Int.MIN_VALUE}") // true 
   //sampleEnd
}

actual external fun abs(n: Long): Long(source)

Returns the absolute value of the given value n.

Special cases:

  • abs(Long.MIN_VALUE) is Long.MIN_VALUE due to an overflow

Since Kotlin

1.3

See also

extension property for Long

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(42L)) // 42
println(abs(-42L)) // 42
// Special case: can't get the absolute value due to an overflow
println("abs(Long.MIN_VALUE) == Long.MIN_VALUE is ${abs(Long.MIN_VALUE) == Long.MIN_VALUE}") // true 
   //sampleEnd
}
actual fun abs(x: Double): Double(source)

Returns the absolute value of the given value x.

Special cases:

  • abs(NaN) is NaN

Since Kotlin

1.8

See also

extension property for Double

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(3.14)) // 3.14
println(abs(-3.14)) // 3.14
println(abs(-0.0)) // 0.0
println(abs(Double.NEGATIVE_INFINITY)) // Infinity
println(abs(Double.NaN)) // NaN 
   //sampleEnd
}

actual fun abs(x: Float): Float(source)

Returns the absolute value of the given value x.

Special cases:

  • abs(NaN) is NaN

Since Kotlin

1.8

See also

extension property for Float

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(3.14f)) // 3.14
println(abs(-3.14f)) // 3.14
println(abs(-0.0f)) // 0.0
println(abs(Float.NEGATIVE_INFINITY)) // Infinity
println(abs(Float.NaN)) // NaN 
   //sampleEnd
}

actual fun abs(n: Int): Int(source)

Returns the absolute value of the given value n.

Special cases:

  • abs(Int.MIN_VALUE) is Int.MIN_VALUE due to an overflow

Since Kotlin

1.8

See also

extension property for Int

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(42)) // 42
println(abs(-42)) // 42
// Special case: can't get the absolute value due to an overflow
println("abs(Int.MIN_VALUE) == Int.MIN_VALUE is ${abs(Int.MIN_VALUE) == Int.MIN_VALUE}") // true 
   //sampleEnd
}

actual fun abs(n: Long): Long(source)

Returns the absolute value of the given value n.

Special cases:

  • abs(Long.MIN_VALUE) is Long.MIN_VALUE due to an overflow

Since Kotlin

1.8

See also

extension property for Long

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(42L)) // 42
println(abs(-42L)) // 42
// Special case: can't get the absolute value due to an overflow
println("abs(Long.MIN_VALUE) == Long.MIN_VALUE is ${abs(Long.MIN_VALUE) == Long.MIN_VALUE}") // true 
   //sampleEnd
}
actual fun abs(x: Double): Double(source)

Returns the absolute value of the given value x.

Special cases:

  • abs(NaN) is NaN

Since Kotlin

1.8

See also

extension property for Double

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(3.14)) // 3.14
println(abs(-3.14)) // 3.14
println(abs(-0.0)) // 0.0
println(abs(Double.NEGATIVE_INFINITY)) // Infinity
println(abs(Double.NaN)) // NaN 
   //sampleEnd
}

actual fun abs(x: Float): Float(source)

Returns the absolute value of the given value x.

Special cases:

  • abs(NaN) is NaN

Since Kotlin

1.8

See also

extension property for Float

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(3.14f)) // 3.14
println(abs(-3.14f)) // 3.14
println(abs(-0.0f)) // 0.0
println(abs(Float.NEGATIVE_INFINITY)) // Infinity
println(abs(Float.NaN)) // NaN 
   //sampleEnd
}

actual fun abs(n: Int): Int(source)

Returns the absolute value of the given value n.

Special cases:

  • abs(Int.MIN_VALUE) is Int.MIN_VALUE due to an overflow

Since Kotlin

1.8

See also

extension property for Int

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(42)) // 42
println(abs(-42)) // 42
// Special case: can't get the absolute value due to an overflow
println("abs(Int.MIN_VALUE) == Int.MIN_VALUE is ${abs(Int.MIN_VALUE) == Int.MIN_VALUE}") // true 
   //sampleEnd
}

actual fun abs(n: Long): Long(source)

Returns the absolute value of the given value n.

Special cases:

  • abs(Long.MIN_VALUE) is Long.MIN_VALUE due to an overflow

Since Kotlin

1.8

See also

extension property for Long

Samples

import kotlin.math.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   println(abs(42L)) // 42
println(abs(-42L)) // 42
// Special case: can't get the absolute value due to an overflow
println("abs(Long.MIN_VALUE) == Long.MIN_VALUE is ${abs(Long.MIN_VALUE) == Long.MIN_VALUE}") // true 
   //sampleEnd
}