scoop:scoop.math:0.1.0
src/scalar/floating_double.scoop
Selected production source · src/scalar/floating_double.scoop
1
package scoop.math3
import scoop.math.min as numericMin4
import scoop.math.max as numericMax6
/** Clears the sign bit, retaining every other bit including NaN payloads. */7
public fun abs(x: Double): Double = Double.fromBits(x.toBits() and Long.MAX_VALUE)9
/** Copies only the sign bit from sign into magnitude. */10
public fun copySign(magnitude: Double, sign: Double): Double =11
Double.fromBits((magnitude.toBits() and Long.MAX_VALUE) or (sign.toBits() and Long.MIN_VALUE))13
/** Returns signed one, preserving signed zero and propagating NaN. */14
public fun signum(x: Double): Double =15
if (x.isNaN() || x == 0.0)16
x17
else18
copySign(1.0, x)20
/** Returns the smaller value, propagating NaN and ordering negative zero first. */21
public fun min(a: Double, b: Double): Double {22
if (a.isNaN())23
return a24
if (b.isNaN())25
return b26
if (a == 0.0 && b == 0.0)27
return Double.fromBits(a.toBits() or b.toBits())28
return if (a <= b)29
a30
else31
b32
}34
/** Returns the larger value, propagating NaN and ordering negative zero first. */35
public fun max(a: Double, b: Double): Double {36
if (a.isNaN())37
return a38
if (b.isNaN())39
return b40
if (a == 0.0 && b == 0.0)41
return Double.fromBits(a.toBits() and b.toBits())42
return if (a >= b)43
a44
else45
b46
}48
/** Clamps to inclusive numeric bounds; NaN or reversed bounds throw. */49
public fun clamp(value: Double, min: Double, max: Double): Double {50
if (51
min.isNaN() ||52
max.isNaN() ||53
min >54
max ||55
(min == 0.0 && max == 0.0 && min.toBits() == 0L && max.toBits() < 0L)56
)57
throw IllegalArgumentException(Some("invalid floating bounds"))58
return numericMin(numericMax(value, min), max)59
}61
/** Returns the adjacent representable value toward positive infinity. */62
public fun nextUp(x: Double): Double {63
if (x.isNaN() || x == Double.POSITIVE_INFINITY)64
return x65
if (x == 0.0)66
return Double.MIN_VALUE67
return Double.fromBits(68
x.toBits() +69
(70
if (x < 0.0)71
-1L72
else73
1L74
)75
)76
}78
/** Returns the adjacent representable value toward negative infinity. */79
public fun nextDown(x: Double): Double {80
if (x.isNaN() || x == Double.NEGATIVE_INFINITY)81
return x82
if (x == 0.0)83
return -Double.MIN_VALUE84
return Double.fromBits(85
x.toBits() +86
(87
if (x < 0.0)88
1L89
else90
-1L91
)92
)93
}95
/** Returns the neighbor toward direction; equal inputs return direction including its zero sign. */96
public fun nextAfter(x: Double, direction: Double): Double {97
if (x.isNaN())98
return x99
if (direction.isNaN())100
return direction101
if (x == direction)102
return direction103
return if (x < direction)104
nextUp(x)105
else106
nextDown(x)107
}109
/** Returns the unbiased exponent; zero/subnormal and nonfinite values use boundary sentinels. */110
public fun getExponent(x: Double): Int = ((x.toBits() ushr 52L) and 0x7ffL).toInt() - 1023112
/** Returns the spacing at the magnitude of x, with IEEE zero and nonfinite handling. */113
public fun ulp(x: Double): Double {114
if (x.isNaN() || x.isInfinite())115
return abs(x)116
val exponent = getExponent(x)117
if (exponent < -1022)118
return Double.MIN_VALUE119
val power = exponent - 52120
return if (power >= -1022)121
Double.fromBits((power + 1023).toLong() shl 52L)122
else123
Double.fromBits(1L shl (power - (-1074)).toLong())124
}126
/** Rounds to the nearest integer with ties toward positive infinity; overflow saturates and NaN becomes zero. */127
public fun round(x: Double): Long {128
if (!x.isFinite())129
return x.toLong()130
val lower = floor(x)131
return (132
if (x - lower >= 0.5)133
lower + 1.0134
else135
lower136
).toLong()137
}139
/** Converts angle units using a coefficient rounded directly to the operand precision. */140
public fun toRadians(x: Double): Double =141
x * 0.017453292519943295769236907684886127134428718885417254560971914401710091146034494143
/** Converts angle units using a coefficient rounded directly to the operand precision. */144
public fun toDegrees(x: Double): Double =145
x * 57.295779513082320876798154814105170332405472466564321549160243861202847148321553147
/** Evaluates sin at the operand precision using the target math library. */148
public fun sin(x: Double): Double {149
@Unsafe150
{151
return nativeSin(x)152
}153
}155
/** Evaluates cos at the operand precision using the target math library. */156
public fun cos(x: Double): Double {157
@Unsafe158
{159
return nativeCos(x)160
}161
}163
/** Evaluates tan at the operand precision using the target math library. */164
public fun tan(x: Double): Double {165
@Unsafe166
{167
return nativeTan(x)168
}169
}171
/** Evaluates asin at the operand precision using the target math library. */172
public fun asin(x: Double): Double {173
@Unsafe174
{175
return nativeAsin(x)176
}177
}179
/** Evaluates acos at the operand precision using the target math library. */180
public fun acos(x: Double): Double {181
@Unsafe182
{183
return nativeAcos(x)184
}185
}187
/** Evaluates atan at the operand precision using the target math library. */188
public fun atan(x: Double): Double {189
@Unsafe190
{191
return nativeAtan(x)192
}193
}195
/** Evaluates sinh at the operand precision using the target math library. */196
public fun sinh(x: Double): Double {197
@Unsafe198
{199
return nativeSinh(x)200
}201
}203
/** Evaluates cosh at the operand precision using the target math library. */204
public fun cosh(x: Double): Double {205
@Unsafe206
{207
return nativeCosh(x)208
}209
}211
/** Evaluates tanh at the operand precision using the target math library. */212
public fun tanh(x: Double): Double {213
@Unsafe214
{215
return nativeTanh(x)216
}217
}219
/** Evaluates exp at the operand precision using the target math library. */220
public fun exp(x: Double): Double {221
@Unsafe222
{223
return nativeExp(x)224
}225
}227
/** Evaluates expm1 at the operand precision using the target math library. */228
public fun expm1(x: Double): Double {229
@Unsafe230
{231
return nativeExpm1(x)232
}233
}235
/** Evaluates log at the operand precision using the target math library. */236
public fun log(x: Double): Double {237
@Unsafe238
{239
return nativeLog(x)240
}241
}243
/** Evaluates log10 at the operand precision using the target math library. */244
public fun log10(x: Double): Double {245
@Unsafe246
{247
return nativeLog10(x)248
}249
}251
/** Evaluates log1p at the operand precision using the target math library. */252
public fun log1p(x: Double): Double {253
@Unsafe254
{255
return nativeLog1p(x)256
}257
}259
/** Returns the correctly rounded square root, preserving negative zero. */260
public fun sqrt(x: Double): Double {261
@Unsafe262
{263
return nativeSqrt(x)264
}265
}267
/** Evaluates cbrt at the operand precision using the target math library. */268
public fun cbrt(x: Double): Double {269
@Unsafe270
{271
return nativeCbrt(x)272
}273
}275
/** Evaluates floor at the operand precision using the target math library. */276
public fun floor(x: Double): Double {277
@Unsafe278
{279
return nativeFloor(x)280
}281
}283
/** Evaluates ceil at the operand precision using the target math library. */284
public fun ceil(x: Double): Double {285
@Unsafe286
{287
return nativeCeil(x)288
}289
}291
/** Rounds to an integral floating value with ties to even. */292
public fun rint(x: Double): Double {293
@Unsafe294
{295
return nativeRint(x)296
}297
}299
/** Evaluates atan2 at the operand precision using the target math library. */300
public fun atan2(y: Double, x: Double): Double {301
@Unsafe302
{303
return nativeAtan2(y, x)304
}305
}307
/** Evaluates hypot at the operand precision using the target math library. */308
public fun hypot(x: Double, y: Double): Double {309
@Unsafe310
{311
return nativeHypot(x, y)312
}313
}315
/** Calls the system fma, retaining its rounding behavior. */316
public fun fma(a: Double, b: Double, c: Double): Double {317
@Unsafe318
{319
return nativeFma(a, b, c)320
}321
}323
/** Returns the IEEE remainder with a nearest-even integer quotient. */324
public fun ieeeRemainder(x: Double, y: Double): Double {325
@Unsafe326
{327
return nativeIeeeRemainder(x, y)328
}329
}331
/** Scales by an exact power of two with one final rounding. */332
public fun scalb(x: Double, scaleFactor: Int): Double {333
@Unsafe334
{335
return nativeScalb(x, scaleFactor)336
}337
}339
/** Raises x to y using the target math library's special-value rules. */340
public fun pow(x: Double, y: Double): Double {341
@Unsafe342
{343
return nativePow(x, y)344
}345
}347
@GCLeaf348
@Extern(name = "sin")349
private fun nativeSin(x: Double): Double351
@GCLeaf352
@Extern(name = "cos")353
private fun nativeCos(x: Double): Double355
@GCLeaf356
@Extern(name = "tan")357
private fun nativeTan(x: Double): Double359
@GCLeaf360
@Extern(name = "asin")361
private fun nativeAsin(x: Double): Double363
@GCLeaf364
@Extern(name = "acos")365
private fun nativeAcos(x: Double): Double367
@GCLeaf368
@Extern(name = "atan")369
private fun nativeAtan(x: Double): Double371
@GCLeaf372
@Extern(name = "sinh")373
private fun nativeSinh(x: Double): Double375
@GCLeaf376
@Extern(name = "cosh")377
private fun nativeCosh(x: Double): Double379
@GCLeaf380
@Extern(name = "tanh")381
private fun nativeTanh(x: Double): Double383
@GCLeaf384
@Extern(name = "exp")385
private fun nativeExp(x: Double): Double387
@GCLeaf388
@Extern(name = "expm1")389
private fun nativeExpm1(x: Double): Double391
@GCLeaf392
@Extern(name = "log")393
private fun nativeLog(x: Double): Double395
@GCLeaf396
@Extern(name = "log10")397
private fun nativeLog10(x: Double): Double399
@GCLeaf400
@Extern(name = "log1p")401
private fun nativeLog1p(x: Double): Double403
@GCLeaf404
@Extern(name = "sqrt")405
private fun nativeSqrt(x: Double): Double407
@GCLeaf408
@Extern(name = "cbrt")409
private fun nativeCbrt(x: Double): Double411
@GCLeaf412
@Extern(name = "floor")413
private fun nativeFloor(x: Double): Double415
@GCLeaf416
@Extern(name = "ceil")417
private fun nativeCeil(x: Double): Double419
@GCLeaf420
@Extern(name = "rint")421
private fun nativeRint(x: Double): Double423
@GCLeaf424
@Extern(name = "atan2")425
private fun nativeAtan2(y: Double, x: Double): Double427
@GCLeaf428
@Extern(name = "hypot")429
private fun nativeHypot(x: Double, y: Double): Double431
@GCLeaf432
@Extern(name = "fma")433
private fun nativeFma(a: Double, b: Double, c: Double): Double435
@GCLeaf436
@Extern(name = "remainder")437
private fun nativeIeeeRemainder(x: Double, y: Double): Double439
@GCLeaf440
@Extern(name = "scalbn")441
private fun nativeScalb(x: Double, scaleFactor: Int): Double443
@GCLeaf444
@Extern(name = "pow")445
private fun nativePow(x: Double, y: Double): Double