ulp
Returns the ulp (unit in the last place) of this value.
An ulp is a positive distance between this value and the next nearest Double value larger in magnitude.
Special cases:
NaN.ulpisNaNx.ulpis+Infwhenxis+Infor-Inf0.0.ulpisDouble.MIN_VALUE
Since Kotlin
1.2See also
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// the "distance" between two Double values 2ⁿ and 2ⁿ+1 is 2ⁿ⁻⁵²
// 2⁴⁹ = 562949953421312, 2⁴⁹⁻⁵² = 1/8 = 0.125
println(562949953421312.0.ulp) // 0.125
// 2⁴⁵ = 35184372088832, 2⁴⁹⁻⁵² = 1/128 = 0.0078125
println(35184372088832.0.ulp) // 0.0078125
// special cases
println(Double.NaN.ulp) // NaN
println(Double.POSITIVE_INFINITY.ulp) // Infinity
println(Double.NEGATIVE_INFINITY.ulp) // Infinity
println("0.0.ulp == Double.MIN_VALUE is ${0.0.ulp == Double.MIN_VALUE}") // true
//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
}Returns the ulp (unit in the last place) of this value.
An ulp is a positive distance between this value and the next nearest Double value larger in magnitude.
Special cases:
NaN.ulpisNaNx.ulpis+Infwhenxis+Infor-Inf0.0.ulpisDouble.MIN_VALUE
Since Kotlin
1.2See also
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// the "distance" between two Double values 2ⁿ and 2ⁿ+1 is 2ⁿ⁻⁵²
// 2⁴⁹ = 562949953421312, 2⁴⁹⁻⁵² = 1/8 = 0.125
println(562949953421312.0.ulp) // 0.125
// 2⁴⁵ = 35184372088832, 2⁴⁹⁻⁵² = 1/128 = 0.0078125
println(35184372088832.0.ulp) // 0.0078125
// special cases
println(Double.NaN.ulp) // NaN
println(Double.POSITIVE_INFINITY.ulp) // Infinity
println(Double.NEGATIVE_INFINITY.ulp) // Infinity
println("0.0.ulp == Double.MIN_VALUE is ${0.0.ulp == Double.MIN_VALUE}") // true
//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
}Returns the ulp of this value.
An ulp is a positive distance between this value and the next nearest Float value larger in magnitude.
Special cases:
NaN.ulpisNaNx.ulpis+Infwhenxis+Infor-Inf0.0.ulpisFloat.MIN_VALUE
Since Kotlin
1.2See also
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// the "distance" between two Float values 2ⁿ and 2ⁿ+1 is 2ⁿ⁻²⁴
// 2²⁰ = 1048576, 2²⁰⁻²⁴ = 1/8 = 0.125
println(1048576.0f.ulp) // 0.125
// 2¹⁶ = 65536, 2¹⁶⁻²⁴ = 1/128 = 0.0078125
println(65536.0f.ulp) // 0.0078125
// special cases
println(Float.NaN.ulp) // NaN
println(Float.POSITIVE_INFINITY.ulp) // Infinity
println(Float.NEGATIVE_INFINITY.ulp) // Infinity
println("0.0f.ulp == Float.MIN_VALUE is ${0.0f.ulp == Float.MIN_VALUE}") // true
//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
}Returns the ulp (unit in the last place) of this value.
An ulp is a positive distance between this value and the next nearest Double value larger in magnitude.
Special Cases:
NaN.ulpisNaNx.ulpis+Infwhenxis+Infor-Inf0.0.ulpisDouble.MIN_VALUE
Since Kotlin
1.2Returns the ulp of this value.
An ulp is a positive distance between this value and the next nearest Float value larger in magnitude.
Special cases:
NaN.ulpisNaNx.ulpis+Infwhenxis+Infor-Inf0.0.ulpisFloat.MIN_VALUE
Since Kotlin
1.3See also
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// the "distance" between two Float values 2ⁿ and 2ⁿ+1 is 2ⁿ⁻²⁴
// 2²⁰ = 1048576, 2²⁰⁻²⁴ = 1/8 = 0.125
println(1048576.0f.ulp) // 0.125
// 2¹⁶ = 65536, 2¹⁶⁻²⁴ = 1/128 = 0.0078125
println(65536.0f.ulp) // 0.0078125
// special cases
println(Float.NaN.ulp) // NaN
println(Float.POSITIVE_INFINITY.ulp) // Infinity
println(Float.NEGATIVE_INFINITY.ulp) // Infinity
println("0.0f.ulp == Float.MIN_VALUE is ${0.0f.ulp == Float.MIN_VALUE}") // true
//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
}Returns the ulp (unit in the last place) of this value.
An ulp is a positive distance between this value and the next nearest Double value larger in magnitude.
Special cases:
NaN.ulpisNaNx.ulpis+Infwhenxis+Infor-Inf0.0.ulpisDouble.MIN_VALUE
Since Kotlin
1.3Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// the "distance" between two Double values 2ⁿ and 2ⁿ+1 is 2ⁿ⁻⁵²
// 2⁴⁹ = 562949953421312, 2⁴⁹⁻⁵² = 1/8 = 0.125
println(562949953421312.0.ulp) // 0.125
// 2⁴⁵ = 35184372088832, 2⁴⁹⁻⁵² = 1/128 = 0.0078125
println(35184372088832.0.ulp) // 0.0078125
// special cases
println(Double.NaN.ulp) // NaN
println(Double.POSITIVE_INFINITY.ulp) // Infinity
println(Double.NEGATIVE_INFINITY.ulp) // Infinity
println("0.0.ulp == Double.MIN_VALUE is ${0.0.ulp == Double.MIN_VALUE}") // true
//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
}Returns the ulp (unit in the last place) of this value.
An ulp is a positive distance between this value and the next nearest Double value larger in magnitude.
Special cases:
NaN.ulpisNaNx.ulpis+Infwhenxis+Infor-Inf0.0.ulpisDouble.MIN_VALUE
Since Kotlin
1.8See also
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// the "distance" between two Double values 2ⁿ and 2ⁿ+1 is 2ⁿ⁻⁵²
// 2⁴⁹ = 562949953421312, 2⁴⁹⁻⁵² = 1/8 = 0.125
println(562949953421312.0.ulp) // 0.125
// 2⁴⁵ = 35184372088832, 2⁴⁹⁻⁵² = 1/128 = 0.0078125
println(35184372088832.0.ulp) // 0.0078125
// special cases
println(Double.NaN.ulp) // NaN
println(Double.POSITIVE_INFINITY.ulp) // Infinity
println(Double.NEGATIVE_INFINITY.ulp) // Infinity
println("0.0.ulp == Double.MIN_VALUE is ${0.0.ulp == Double.MIN_VALUE}") // true
//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
}Returns the ulp (unit in the last place) of this value.
An ulp is a positive distance between this value and the next nearest Double value larger in magnitude.
Special cases:
NaN.ulpisNaNx.ulpis+Infwhenxis+Infor-Inf0.0.ulpisDouble.MIN_VALUE
Since Kotlin
1.8See also
Samples
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
// the "distance" between two Double values 2ⁿ and 2ⁿ+1 is 2ⁿ⁻⁵²
// 2⁴⁹ = 562949953421312, 2⁴⁹⁻⁵² = 1/8 = 0.125
println(562949953421312.0.ulp) // 0.125
// 2⁴⁵ = 35184372088832, 2⁴⁹⁻⁵² = 1/128 = 0.0078125
println(35184372088832.0.ulp) // 0.0078125
// special cases
println(Double.NaN.ulp) // NaN
println(Double.POSITIVE_INFINITY.ulp) // Infinity
println(Double.NEGATIVE_INFINITY.ulp) // Infinity
println("0.0.ulp == Double.MIN_VALUE is ${0.0.ulp == Double.MIN_VALUE}") // true
//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
}