max

inline fun max(a: UInt, b: UInt): UInt(source)
inline fun max(a: ULong, b: ULong): ULong(source)

Returns the greater of two values.

Since Kotlin

1.5

expect fun max(a: Double, b: Double): Double(source)

Returns the greater of two values.

If either value is NaN, then the result is NaN.

Since Kotlin

1.2

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1.0, 1.0)) // 1.0
println(max(1.0, -1.0)) // 1.0
println(max(-3.0, -1.0)) // -1.0
println(max(-0.0, 0.0)) // 0.0

// special cases
println(max(Double.NaN, 0.0)) // NaN
println(max(-9000.0, Double.NaN)) // NaN
// Note that MIN_VALUE has a different meaning compared to Long.MIN_VALUE or Int.MIN_VALUE
println("max(Double.MIN_VALUE, 0.0) == Double.MIN_VALUE is ${max(Double.MIN_VALUE, 0.0) == Double.MIN_VALUE}") // true
println(max(Double.POSITIVE_INFINITY, Double.MAX_VALUE)) // Infinity 
   //sampleEnd
}

expect fun max(a: Float, b: Float): Float(source)

Returns the greater of two values.

If either value is NaN, then the result is NaN.

Since Kotlin

1.2

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1.0f, 1.0f)) // 1.0
println(max(1.0f, -1.0f)) // 1.0
println(max(-3.0f, -1.0f)) // -1.0
println(max(-0.0f, 0.0f)) // 0.0

// special cases
println(max(Float.NaN, 0.0f)) // NaN
println(max(-9000.0f, Float.NaN)) // NaN
// Note that MIN_VALUE has a different meaning compared to Long.MIN_VALUE or Int.MIN_VALUE
println("max(Float.MIN_VALUE, 0.0f) == Float.MIN_VALUE is ${max(Float.MIN_VALUE, 0.0f) == Float.MIN_VALUE}") // true
println(max(Float.POSITIVE_INFINITY, Float.MAX_VALUE)) // Infinity 
   //sampleEnd
}

expect fun max(a: Int, b: Int): Int(source)

Returns the greater of two values.

Since Kotlin

1.2

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1, 1)) // 1
println(max(1, -1)) // 1
println(max(-3, -1)) // -1 
   //sampleEnd
}

expect fun max(a: Long, b: Long): Long(source)

Returns the greater of two values.

Since Kotlin

1.2

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1L, 1L)) // 1
println(max(1L, -1L)) // 1
println(max(-3L, -1L)) // -1 
   //sampleEnd
}
actual inline fun max(a: Double, b: Double): Double(source)

Returns the greater of two values.

If either value is NaN, then the result is NaN.

Since Kotlin

1.2

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1.0, 1.0)) // 1.0
println(max(1.0, -1.0)) // 1.0
println(max(-3.0, -1.0)) // -1.0
println(max(-0.0, 0.0)) // 0.0

// special cases
println(max(Double.NaN, 0.0)) // NaN
println(max(-9000.0, Double.NaN)) // NaN
// Note that MIN_VALUE has a different meaning compared to Long.MIN_VALUE or Int.MIN_VALUE
println("max(Double.MIN_VALUE, 0.0) == Double.MIN_VALUE is ${max(Double.MIN_VALUE, 0.0) == Double.MIN_VALUE}") // true
println(max(Double.POSITIVE_INFINITY, Double.MAX_VALUE)) // Infinity 
   //sampleEnd
}

actual inline fun max(a: Float, b: Float): Float(source)

Returns the greater of two values.

If either value is NaN, then the result is NaN.

Since Kotlin

1.2

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1.0f, 1.0f)) // 1.0
println(max(1.0f, -1.0f)) // 1.0
println(max(-3.0f, -1.0f)) // -1.0
println(max(-0.0f, 0.0f)) // 0.0

// special cases
println(max(Float.NaN, 0.0f)) // NaN
println(max(-9000.0f, Float.NaN)) // NaN
// Note that MIN_VALUE has a different meaning compared to Long.MIN_VALUE or Int.MIN_VALUE
println("max(Float.MIN_VALUE, 0.0f) == Float.MIN_VALUE is ${max(Float.MIN_VALUE, 0.0f) == Float.MIN_VALUE}") // true
println(max(Float.POSITIVE_INFINITY, Float.MAX_VALUE)) // Infinity 
   //sampleEnd
}

actual inline fun max(a: Int, b: Int): Int(source)

Returns the greater of two values.

Since Kotlin

1.2

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1, 1)) // 1
println(max(1, -1)) // 1
println(max(-3, -1)) // -1 
   //sampleEnd
}

actual inline fun max(a: Long, b: Long): Long(source)

Returns the greater of two values.

Since Kotlin

1.2

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1L, 1L)) // 1
println(max(1L, -1L)) // 1
println(max(-3L, -1L)) // -1 
   //sampleEnd
}
actual inline fun max(a: Double, b: Double): Double(source)

Returns the greater of two values.

If either value is NaN, then the result is NaN.

Since Kotlin

1.2

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1.0, 1.0)) // 1.0
println(max(1.0, -1.0)) // 1.0
println(max(-3.0, -1.0)) // -1.0
println(max(-0.0, 0.0)) // 0.0

// special cases
println(max(Double.NaN, 0.0)) // NaN
println(max(-9000.0, Double.NaN)) // NaN
// Note that MIN_VALUE has a different meaning compared to Long.MIN_VALUE or Int.MIN_VALUE
println("max(Double.MIN_VALUE, 0.0) == Double.MIN_VALUE is ${max(Double.MIN_VALUE, 0.0) == Double.MIN_VALUE}") // true
println(max(Double.POSITIVE_INFINITY, Double.MAX_VALUE)) // Infinity 
   //sampleEnd
}

actual inline fun max(a: Float, b: Float): Float(source)

Returns the greater of two values.

If either value is NaN, then the result is NaN.

Since Kotlin

1.2

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1.0f, 1.0f)) // 1.0
println(max(1.0f, -1.0f)) // 1.0
println(max(-3.0f, -1.0f)) // -1.0
println(max(-0.0f, 0.0f)) // 0.0

// special cases
println(max(Float.NaN, 0.0f)) // NaN
println(max(-9000.0f, Float.NaN)) // NaN
// Note that MIN_VALUE has a different meaning compared to Long.MIN_VALUE or Int.MIN_VALUE
println("max(Float.MIN_VALUE, 0.0f) == Float.MIN_VALUE is ${max(Float.MIN_VALUE, 0.0f) == Float.MIN_VALUE}") // true
println(max(Float.POSITIVE_INFINITY, Float.MAX_VALUE)) // Infinity 
   //sampleEnd
}

actual inline fun max(a: Int, b: Int): Int(source)

Returns the greater of two values.

Since Kotlin

1.2

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1, 1)) // 1
println(max(1, -1)) // 1
println(max(-3, -1)) // -1 
   //sampleEnd
}

actual inline fun max(a: Long, b: Long): Long(source)

Returns the greater of two values.

Since Kotlin

1.2

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1L, 1L)) // 1
println(max(1L, -1L)) // 1
println(max(-3L, -1L)) // -1 
   //sampleEnd
}
actual fun max(a: Double, b: Double): Double(source)

Returns the greater of two values.

If either value is NaN, then the result is NaN.

Since Kotlin

1.3

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1.0, 1.0)) // 1.0
println(max(1.0, -1.0)) // 1.0
println(max(-3.0, -1.0)) // -1.0
println(max(-0.0, 0.0)) // 0.0

// special cases
println(max(Double.NaN, 0.0)) // NaN
println(max(-9000.0, Double.NaN)) // NaN
// Note that MIN_VALUE has a different meaning compared to Long.MIN_VALUE or Int.MIN_VALUE
println("max(Double.MIN_VALUE, 0.0) == Double.MIN_VALUE is ${max(Double.MIN_VALUE, 0.0) == Double.MIN_VALUE}") // true
println(max(Double.POSITIVE_INFINITY, Double.MAX_VALUE)) // Infinity 
   //sampleEnd
}

actual fun max(a: Float, b: Float): Float(source)

Returns the greater of two values.

If either value is NaN, then the result is NaN.

Since Kotlin

1.3

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1.0f, 1.0f)) // 1.0
println(max(1.0f, -1.0f)) // 1.0
println(max(-3.0f, -1.0f)) // -1.0
println(max(-0.0f, 0.0f)) // 0.0

// special cases
println(max(Float.NaN, 0.0f)) // NaN
println(max(-9000.0f, Float.NaN)) // NaN
// Note that MIN_VALUE has a different meaning compared to Long.MIN_VALUE or Int.MIN_VALUE
println("max(Float.MIN_VALUE, 0.0f) == Float.MIN_VALUE is ${max(Float.MIN_VALUE, 0.0f) == Float.MIN_VALUE}") // true
println(max(Float.POSITIVE_INFINITY, Float.MAX_VALUE)) // Infinity 
   //sampleEnd
}

actual fun max(a: Int, b: Int): Int(source)

Returns the greater of two values.

Since Kotlin

1.3

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1, 1)) // 1
println(max(1, -1)) // 1
println(max(-3, -1)) // -1 
   //sampleEnd
}

actual fun max(a: Long, b: Long): Long(source)

Returns the greater of two values.

Since Kotlin

1.3

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1L, 1L)) // 1
println(max(1L, -1L)) // 1
println(max(-3L, -1L)) // -1 
   //sampleEnd
}
actual fun max(a: Double, b: Double): Double(source)

Returns the greater of two values.

If either value is NaN, then the result is NaN.

Since Kotlin

1.8

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1.0, 1.0)) // 1.0
println(max(1.0, -1.0)) // 1.0
println(max(-3.0, -1.0)) // -1.0
println(max(-0.0, 0.0)) // 0.0

// special cases
println(max(Double.NaN, 0.0)) // NaN
println(max(-9000.0, Double.NaN)) // NaN
// Note that MIN_VALUE has a different meaning compared to Long.MIN_VALUE or Int.MIN_VALUE
println("max(Double.MIN_VALUE, 0.0) == Double.MIN_VALUE is ${max(Double.MIN_VALUE, 0.0) == Double.MIN_VALUE}") // true
println(max(Double.POSITIVE_INFINITY, Double.MAX_VALUE)) // Infinity 
   //sampleEnd
}

actual fun max(a: Float, b: Float): Float(source)

Returns the greater of two values.

If either value is NaN, then the result is NaN.

Since Kotlin

1.8

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1.0f, 1.0f)) // 1.0
println(max(1.0f, -1.0f)) // 1.0
println(max(-3.0f, -1.0f)) // -1.0
println(max(-0.0f, 0.0f)) // 0.0

// special cases
println(max(Float.NaN, 0.0f)) // NaN
println(max(-9000.0f, Float.NaN)) // NaN
// Note that MIN_VALUE has a different meaning compared to Long.MIN_VALUE or Int.MIN_VALUE
println("max(Float.MIN_VALUE, 0.0f) == Float.MIN_VALUE is ${max(Float.MIN_VALUE, 0.0f) == Float.MIN_VALUE}") // true
println(max(Float.POSITIVE_INFINITY, Float.MAX_VALUE)) // Infinity 
   //sampleEnd
}

actual fun max(a: Int, b: Int): Int(source)

Returns the greater of two values.

Since Kotlin

1.8

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1, 1)) // 1
println(max(1, -1)) // 1
println(max(-3, -1)) // -1 
   //sampleEnd
}

actual fun max(a: Long, b: Long): Long(source)

Returns the greater of two values.

Since Kotlin

1.8

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1L, 1L)) // 1
println(max(1L, -1L)) // 1
println(max(-3L, -1L)) // -1 
   //sampleEnd
}
actual fun max(a: Double, b: Double): Double(source)

Returns the greater of two values.

If either value is NaN, then the result is NaN.

Since Kotlin

1.8

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1.0, 1.0)) // 1.0
println(max(1.0, -1.0)) // 1.0
println(max(-3.0, -1.0)) // -1.0
println(max(-0.0, 0.0)) // 0.0

// special cases
println(max(Double.NaN, 0.0)) // NaN
println(max(-9000.0, Double.NaN)) // NaN
// Note that MIN_VALUE has a different meaning compared to Long.MIN_VALUE or Int.MIN_VALUE
println("max(Double.MIN_VALUE, 0.0) == Double.MIN_VALUE is ${max(Double.MIN_VALUE, 0.0) == Double.MIN_VALUE}") // true
println(max(Double.POSITIVE_INFINITY, Double.MAX_VALUE)) // Infinity 
   //sampleEnd
}

actual fun max(a: Float, b: Float): Float(source)

Returns the greater of two values.

If either value is NaN, then the result is NaN.

Since Kotlin

1.8

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1.0f, 1.0f)) // 1.0
println(max(1.0f, -1.0f)) // 1.0
println(max(-3.0f, -1.0f)) // -1.0
println(max(-0.0f, 0.0f)) // 0.0

// special cases
println(max(Float.NaN, 0.0f)) // NaN
println(max(-9000.0f, Float.NaN)) // NaN
// Note that MIN_VALUE has a different meaning compared to Long.MIN_VALUE or Int.MIN_VALUE
println("max(Float.MIN_VALUE, 0.0f) == Float.MIN_VALUE is ${max(Float.MIN_VALUE, 0.0f) == Float.MIN_VALUE}") // true
println(max(Float.POSITIVE_INFINITY, Float.MAX_VALUE)) // Infinity 
   //sampleEnd
}

actual fun max(a: Int, b: Int): Int(source)

Returns the greater of two values.

Since Kotlin

1.8

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1, 1)) // 1
println(max(1, -1)) // 1
println(max(-3, -1)) // -1 
   //sampleEnd
}

actual fun max(a: Long, b: Long): Long(source)

Returns the greater of two values.

Since Kotlin

1.8

Samples

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

fun main() { 
   //sampleStart 
   println(max(-1L, 1L)) // 1
println(max(1L, -1L)) // 1
println(max(-3L, -1L)) // -1 
   //sampleEnd
}