next Towards
Требования к платформе и версии: Common (1.2), JS (1.2), Wasm-JS (1.8), Wasm-WASI (1.8)
expect fun Double.nextTowards(to: Double): Double
Возвращает значение Double, ближайшее к данному значению в направлении от данного значения к значению to.
Особые случаи:
x.nextTowards(y)равноNaN, если либоx, либоyравноNaNx.nextTowards(x) == x
Смотрите также
Примеры
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// Floating point numbers have discrete representation,
// so depending on the value's magnitude, the step towards
// the previous representable value will be different
println(10000.0.nextTowards(Double.POSITIVE_INFINITY)) // 10000.000000000002
println(100000.0.nextTowards(Double.POSITIVE_INFINITY)) // 100000.00000000001
println(10000.0.nextTowards(0.0)) // 9999.999999999998
println(100000.0.nextTowards(0.0)) // 99999.99999999999
// nextTowards is in fact a more flexible alternative to nextUp/nextDown
println("100.0.nextTowards(1000.0) == 100.0.nextUp() is ${100.0.nextTowards(1000.0) == 100.0.nextUp()}") // true
println("100.0.nextTowards(-1000.0) == 100.0.nextDown() is ${100.0.nextTowards(-1000.0) == 100.0.nextDown()}") // true
// special cases
println(1.0.nextTowards(1.0)) // 1.0
println(Double.POSITIVE_INFINITY.nextTowards(Double.POSITIVE_INFINITY)) // Infinity
println(Double.NEGATIVE_INFINITY.nextTowards(Double.NEGATIVE_INFINITY)) // -Infinity
println(Double.NaN.nextTowards(0.0)) // NaN
println(0.0.nextTowards(Double.NaN)) // NaN
//sampleEnd
}
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// Unlike true real numbers, Double can only represent a fixed set of discrete values.
// According to IEEE-754, Double uses a single bit for a sign, 11 bits for the exponent, and 52 for the fraction.
// The bigger the integer part of the floating point value, the less space remains for "fitting" fractional part.
// As a result, starting from 2⁵² and until 2⁵³-1 Double values are incremented by 1.0
// (meaning that there are no in-between values), values from 2⁵³ until 2⁵⁴-1 are incremented by 2.0,
// but values in between 2⁵¹ and 2⁵²-1 are incremented by 0.5.
//
// The "distance" between a current value and the next one (the "increment" mentioned above) is dynamic,
// it depends on the magnitude of the current value, and it could be checked using Double.ulp.
val v2_52_53 = 4503599627370498.0 // value in range [2⁵², 2⁵³-1]
println(v2_52_53.ulp) // 1.0
val v2_53_54 = 9007199254740998.0 // value in range [2⁵³, 2⁵⁴-1]
println(v2_53_54.ulp) // 2.0
val v2_51_52 = 4503599627370490.0 // value in range [2⁵¹, 2⁵²-1]
println(v2_51_52.ulp) // 0.5
// Instead of using ulp, you can also check the next and previous values representable with a Double
// using Double.nextUp and Double.nextDown correspondingly.
println(9007199254740998.0.nextUp() - 9007199254740998.0) // 2.0
println(9007199254740998.0 - 9007199254740998.0.nextDown()) // 2.0
// As a more flexible alternative to nextUp/nextDown, one may use nextTowards, which accepts a value
// that works as a direction to increase/decrease a given value towards.
println(4503599627370496.0.nextTowards(Double.POSITIVE_INFINITY) - 4503599627370496.0) // 1.0
println(4503599627370496.0 - 4503599627370496.0.nextTowards(Double.NEGATIVE_INFINITY)) // 0.5
//sampleEnd
}
Требования к платформе и версии: JVM (1.2)
inline fun Float.nextTowards(to: Float): Float
Возвращает значение Float, ближайшее к данному значению в направлении от данного значения к значению to.
Особые случаи:
x.nextTowards(y)равноNaN, если либоx, либоyравноNaNx.nextTowards(x) == x
Смотрите также
Примеры
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// Floating point numbers have discrete representation,
// so depending on the value's magnitude, the step towards
// the previous representable value will be different
println(10000.0f.nextTowards(Float.POSITIVE_INFINITY)) // 10000.001
println(100000.0f.nextTowards(Float.POSITIVE_INFINITY)) // 100000.01
println(10000.0f.nextTowards(0.0f)) // 9999.999
println(100000.0f.nextTowards(0.0f)) // 99999.99
// nextTowards is in fact a more flexible alternative to nextUp/nextDown
println("100.0f.nextTowards(1000.0f) == 100.0f.nextUp() is ${100.0f.nextTowards(1000.0f) == 100.0f.nextUp()}") // true
println("100.0f.nextTowards(-1000.0f) == 100.0f.nextDown() is ${100.0f.nextTowards(-1000.0f) == 100.0f.nextDown()}") // true
// special cases
println(1.0f.nextTowards(1.0f)) // 1.0
println(Float.POSITIVE_INFINITY.nextTowards(Float.POSITIVE_INFINITY)) // Infinity
println(Float.NEGATIVE_INFINITY.nextTowards(Float.NEGATIVE_INFINITY)) // -Infinity
println(Float.NaN.nextTowards(0.0f)) // NaN
println(0.0f.nextTowards(Float.NaN)) // NaN
//sampleEnd
}
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// Unlike true real numbers, Float can only represent a fixed set of discrete values.
// According to IEEE-754, Double uses a single bit for a sign, 8 bits for the exponent, and 23 for the fraction.
// The bigger the integer part of the floating point value, the less space remains for "fitting" fractional part.
// As a result, starting from 2²³ and until 2²⁴-1 Float values are incremented by 1.0
// (meaning that there are no in-between values), values from 2²³ until 2²⁵-1 are incremented by 2.0,
// but values in between 2²² and 2²³-1 are incremented by 0.5.
//
// The "distance" between a current value and the next one (the "increment" mentioned above) is dynamic,
// it depends on the magnitude of the current value, and it could be checked using Double.ulp.
val v2_23_24 = 8388618.0f // value in range [2²³, 2²⁴-1]
println(v2_23_24.ulp) // 1.0
val v2_24_25 = 16777226.0f // value in range [2²⁴, 2²⁵-1]
println(v2_24_25.ulp) // 2.0
val v2_22_23 = 4194306.0f // value in range [2²², 2²³-1]
println(v2_22_23.ulp) // 0.5
// Instead of using ulp, you can also check the next and previous values representable with a Double
// using Double.nextUp and Double.nextDown correspondingly.
println(16777226.0f.nextUp() - 16777226.0f) // 2.0
println(16777226.0f - 16777226.0f.nextDown()) // 2.0
// As a more flexible alternative to nextUp/nextDown, one may use nextTowards, which accepts a value
// that works as a direction to increase/decrease a given value towards.
println(8388608.0f.nextTowards(Float.POSITIVE_INFINITY) - 8388608.0f) // 1.0
println(8388608.0f - 8388608.0f.nextTowards(Float.NEGATIVE_INFINITY)) // 0.5
//sampleEnd
}
actual inline fun Double.nextTowards(to: Double): Double
Возвращает значение Double, ближайшее к данному значению в направлении от данного значения к значению to.
Особые случаи:
x.nextTowards(y)равноNaN, если либоx, либоyравноNaNx.nextTowards(x) == x
Смотрите также
Примеры
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// Floating point numbers have discrete representation,
// so depending on the value's magnitude, the step towards
// the previous representable value will be different
println(10000.0.nextTowards(Double.POSITIVE_INFINITY)) // 10000.000000000002
println(100000.0.nextTowards(Double.POSITIVE_INFINITY)) // 100000.00000000001
println(10000.0.nextTowards(0.0)) // 9999.999999999998
println(100000.0.nextTowards(0.0)) // 99999.99999999999
// nextTowards is in fact a more flexible alternative to nextUp/nextDown
println("100.0.nextTowards(1000.0) == 100.0.nextUp() is ${100.0.nextTowards(1000.0) == 100.0.nextUp()}") // true
println("100.0.nextTowards(-1000.0) == 100.0.nextDown() is ${100.0.nextTowards(-1000.0) == 100.0.nextDown()}") // true
// special cases
println(1.0.nextTowards(1.0)) // 1.0
println(Double.POSITIVE_INFINITY.nextTowards(Double.POSITIVE_INFINITY)) // Infinity
println(Double.NEGATIVE_INFINITY.nextTowards(Double.NEGATIVE_INFINITY)) // -Infinity
println(Double.NaN.nextTowards(0.0)) // NaN
println(0.0.nextTowards(Double.NaN)) // NaN
//sampleEnd
}
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// Unlike true real numbers, Double can only represent a fixed set of discrete values.
// According to IEEE-754, Double uses a single bit for a sign, 11 bits for the exponent, and 52 for the fraction.
// The bigger the integer part of the floating point value, the less space remains for "fitting" fractional part.
// As a result, starting from 2⁵² and until 2⁵³-1 Double values are incremented by 1.0
// (meaning that there are no in-between values), values from 2⁵³ until 2⁵⁴-1 are incremented by 2.0,
// but values in between 2⁵¹ and 2⁵²-1 are incremented by 0.5.
//
// The "distance" between a current value and the next one (the "increment" mentioned above) is dynamic,
// it depends on the magnitude of the current value, and it could be checked using Double.ulp.
val v2_52_53 = 4503599627370498.0 // value in range [2⁵², 2⁵³-1]
println(v2_52_53.ulp) // 1.0
val v2_53_54 = 9007199254740998.0 // value in range [2⁵³, 2⁵⁴-1]
println(v2_53_54.ulp) // 2.0
val v2_51_52 = 4503599627370490.0 // value in range [2⁵¹, 2⁵²-1]
println(v2_51_52.ulp) // 0.5
// Instead of using ulp, you can also check the next and previous values representable with a Double
// using Double.nextUp and Double.nextDown correspondingly.
println(9007199254740998.0.nextUp() - 9007199254740998.0) // 2.0
println(9007199254740998.0 - 9007199254740998.0.nextDown()) // 2.0
// As a more flexible alternative to nextUp/nextDown, one may use nextTowards, which accepts a value
// that works as a direction to increase/decrease a given value towards.
println(4503599627370496.0.nextTowards(Double.POSITIVE_INFINITY) - 4503599627370496.0) // 1.0
println(4503599627370496.0 - 4503599627370496.0.nextTowards(Double.NEGATIVE_INFINITY)) // 0.5
//sampleEnd
}
Требования к платформе и версии: Native (1.3)
external fun Float.nextTowards(to: Float): Float
Возвращает значение Float, ближайшее к данному значению в направлении от данного значения к значению to.
Особые случаи:
x.nextTowards(y)равноNaN, если либоx, либоyравноNaNx.nextTowards(x) == x
Смотрите также
Примеры
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// Floating point numbers have discrete representation,
// so depending on the value's magnitude, the step towards
// the previous representable value will be different
println(10000.0f.nextTowards(Float.POSITIVE_INFINITY)) // 10000.001
println(100000.0f.nextTowards(Float.POSITIVE_INFINITY)) // 100000.01
println(10000.0f.nextTowards(0.0f)) // 9999.999
println(100000.0f.nextTowards(0.0f)) // 99999.99
// nextTowards is in fact a more flexible alternative to nextUp/nextDown
println("100.0f.nextTowards(1000.0f) == 100.0f.nextUp() is ${100.0f.nextTowards(1000.0f) == 100.0f.nextUp()}") // true
println("100.0f.nextTowards(-1000.0f) == 100.0f.nextDown() is ${100.0f.nextTowards(-1000.0f) == 100.0f.nextDown()}") // true
// special cases
println(1.0f.nextTowards(1.0f)) // 1.0
println(Float.POSITIVE_INFINITY.nextTowards(Float.POSITIVE_INFINITY)) // Infinity
println(Float.NEGATIVE_INFINITY.nextTowards(Float.NEGATIVE_INFINITY)) // -Infinity
println(Float.NaN.nextTowards(0.0f)) // NaN
println(0.0f.nextTowards(Float.NaN)) // NaN
//sampleEnd
}
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// Unlike true real numbers, Float can only represent a fixed set of discrete values.
// According to IEEE-754, Double uses a single bit for a sign, 8 bits for the exponent, and 23 for the fraction.
// The bigger the integer part of the floating point value, the less space remains for "fitting" fractional part.
// As a result, starting from 2²³ and until 2²⁴-1 Float values are incremented by 1.0
// (meaning that there are no in-between values), values from 2²³ until 2²⁵-1 are incremented by 2.0,
// but values in between 2²² and 2²³-1 are incremented by 0.5.
//
// The "distance" between a current value and the next one (the "increment" mentioned above) is dynamic,
// it depends on the magnitude of the current value, and it could be checked using Double.ulp.
val v2_23_24 = 8388618.0f // value in range [2²³, 2²⁴-1]
println(v2_23_24.ulp) // 1.0
val v2_24_25 = 16777226.0f // value in range [2²⁴, 2²⁵-1]
println(v2_24_25.ulp) // 2.0
val v2_22_23 = 4194306.0f // value in range [2²², 2²³-1]
println(v2_22_23.ulp) // 0.5
// Instead of using ulp, you can also check the next and previous values representable with a Double
// using Double.nextUp and Double.nextDown correspondingly.
println(16777226.0f.nextUp() - 16777226.0f) // 2.0
println(16777226.0f - 16777226.0f.nextDown()) // 2.0
// As a more flexible alternative to nextUp/nextDown, one may use nextTowards, which accepts a value
// that works as a direction to increase/decrease a given value towards.
println(8388608.0f.nextTowards(Float.POSITIVE_INFINITY) - 8388608.0f) // 1.0
println(8388608.0f - 8388608.0f.nextTowards(Float.NEGATIVE_INFINITY)) // 0.5
//sampleEnd
}
actual external fun Double.nextTowards(to: Double): Double
Возвращает значение Double, ближайшее к данному значению в направлении от данного значения к значению to.
Особые случаи:
x.nextTowards(y)равноNaN, если либоx, либоyравноNaNx.nextTowards(x) == x
Смотрите также
Примеры
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// Floating point numbers have discrete representation,
// so depending on the value's magnitude, the step towards
// the previous representable value will be different
println(10000.0.nextTowards(Double.POSITIVE_INFINITY)) // 10000.000000000002
println(100000.0.nextTowards(Double.POSITIVE_INFINITY)) // 100000.00000000001
println(10000.0.nextTowards(0.0)) // 9999.999999999998
println(100000.0.nextTowards(0.0)) // 99999.99999999999
// nextTowards is in fact a more flexible alternative to nextUp/nextDown
println("100.0.nextTowards(1000.0) == 100.0.nextUp() is ${100.0.nextTowards(1000.0) == 100.0.nextUp()}") // true
println("100.0.nextTowards(-1000.0) == 100.0.nextDown() is ${100.0.nextTowards(-1000.0) == 100.0.nextDown()}") // true
// special cases
println(1.0.nextTowards(1.0)) // 1.0
println(Double.POSITIVE_INFINITY.nextTowards(Double.POSITIVE_INFINITY)) // Infinity
println(Double.NEGATIVE_INFINITY.nextTowards(Double.NEGATIVE_INFINITY)) // -Infinity
println(Double.NaN.nextTowards(0.0)) // NaN
println(0.0.nextTowards(Double.NaN)) // NaN
//sampleEnd
}
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// Unlike true real numbers, Double can only represent a fixed set of discrete values.
// According to IEEE-754, Double uses a single bit for a sign, 11 bits for the exponent, and 52 for the fraction.
// The bigger the integer part of the floating point value, the less space remains for "fitting" fractional part.
// As a result, starting from 2⁵² and until 2⁵³-1 Double values are incremented by 1.0
// (meaning that there are no in-between values), values from 2⁵³ until 2⁵⁴-1 are incremented by 2.0,
// but values in between 2⁵¹ and 2⁵²-1 are incremented by 0.5.
//
// The "distance" between a current value and the next one (the "increment" mentioned above) is dynamic,
// it depends on the magnitude of the current value, and it could be checked using Double.ulp.
val v2_52_53 = 4503599627370498.0 // value in range [2⁵², 2⁵³-1]
println(v2_52_53.ulp) // 1.0
val v2_53_54 = 9007199254740998.0 // value in range [2⁵³, 2⁵⁴-1]
println(v2_53_54.ulp) // 2.0
val v2_51_52 = 4503599627370490.0 // value in range [2⁵¹, 2⁵²-1]
println(v2_51_52.ulp) // 0.5
// Instead of using ulp, you can also check the next and previous values representable with a Double
// using Double.nextUp and Double.nextDown correspondingly.
println(9007199254740998.0.nextUp() - 9007199254740998.0) // 2.0
println(9007199254740998.0 - 9007199254740998.0.nextDown()) // 2.0
// As a more flexible alternative to nextUp/nextDown, one may use nextTowards, which accepts a value
// that works as a direction to increase/decrease a given value towards.
println(4503599627370496.0.nextTowards(Double.POSITIVE_INFINITY) - 4503599627370496.0) // 1.0
println(4503599627370496.0 - 4503599627370496.0.nextTowards(Double.NEGATIVE_INFINITY)) // 0.5
//sampleEnd
}
© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.math/next-towards.html