scoop:scoop.math:0.1.0
src/scalar/floating_float.scoop
Selected production source · src/scalar/floating_float.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: Float): Float = Float.fromBits(x.toBits() and Int.MAX_VALUE)9
/** Copies only the sign bit from sign into magnitude. */10
public fun copySign(magnitude: Float, sign: Float): Float =11
Float.fromBits((magnitude.toBits() and Int.MAX_VALUE) or (sign.toBits() and Int.MIN_VALUE))13
/** Returns signed one, preserving signed zero and propagating NaN. */14
public fun signum(x: Float): Float =15
if (x.isNaN() || x == 0f)16
x17
else18
copySign(1f, x)20
/** Returns the smaller value, propagating NaN and ordering negative zero first. */21
public fun min(a: Float, b: Float): Float {22
if (a.isNaN())23
return a24
if (b.isNaN())25
return b26
if (a == 0f && b == 0f)27
return Float.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: Float, b: Float): Float {36
if (a.isNaN())37
return a38
if (b.isNaN())39
return b40
if (a == 0f && b == 0f)41
return Float.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: Float, min: Float, max: Float): Float {50
if (51
min.isNaN() ||52
max.isNaN() ||53
min >54
max ||55
(min == 0f && max == 0f && min.toBits() == 0 && max.toBits() < 0)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: Float): Float {63
if (x.isNaN() || x == Float.POSITIVE_INFINITY)64
return x65
if (x == 0f)66
return Float.MIN_VALUE67
return Float.fromBits(68
x.toBits() +69
(70
if (x < 0f)71
-172
else73
174
)75
)76
}78
/** Returns the adjacent representable value toward negative infinity. */79
public fun nextDown(x: Float): Float {80
if (x.isNaN() || x == Float.NEGATIVE_INFINITY)81
return x82
if (x == 0f)83
return -Float.MIN_VALUE84
return Float.fromBits(85
x.toBits() +86
(87
if (x < 0f)88
189
else90
-191
)92
)93
}95
/** Returns the neighbor toward direction; equal inputs return direction including its zero sign. */96
public fun nextAfter(x: Float, direction: Float): Float {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: Float): Int = ((x.toBits() ushr 23L) and 0xff).toInt() - 127112
/** Returns the spacing at the magnitude of x, with IEEE zero and nonfinite handling. */113
public fun ulp(x: Float): Float {114
if (x.isNaN() || x.isInfinite())115
return abs(x)116
val exponent = getExponent(x)117
if (exponent < -126)118
return Float.MIN_VALUE119
val power = exponent - 23120
return if (power >= -126)121
Float.fromBits((power + 127).toInt() shl 23L)122
else123
Float.fromBits(1 shl (power - (-149)).toLong())124
}126
/** Rounds to the nearest integer with ties toward positive infinity; overflow saturates and NaN becomes zero. */127
public fun round(x: Float): Int {128
if (!x.isFinite())129
return x.toInt()130
val lower = floor(x)131
return (132
if (x - lower >= 0.5f)133
lower + 1f134
else135
lower136
).toInt()137
}139
/** Converts angle units using a coefficient rounded directly to the operand precision. */140
public fun toRadians(x: Float): Float =141
x * 0.017453292519943295769236907684886127134428718885417254560971914401710091146034494f143
/** Converts angle units using a coefficient rounded directly to the operand precision. */144
public fun toDegrees(x: Float): Float =145
x * 57.295779513082320876798154814105170332405472466564321549160243861202847148321553f147
/** Evaluates sin at the operand precision using the target math library. */148
public fun sin(x: Float): Float {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: Float): Float {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: Float): Float {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: Float): Float {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: Float): Float {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: Float): Float {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: Float): Float {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: Float): Float {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: Float): Float {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: Float): Float {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: Float): Float {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: Float): Float {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: Float): Float {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: Float): Float {253
@Unsafe254
{255
return nativeLog1p(x)256
}257
}259
/** Returns the correctly rounded square root, preserving negative zero. */260
public fun sqrt(x: Float): Float {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: Float): Float {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: Float): Float {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: Float): Float {285
@Unsafe286
{287
return nativeCeil(x)288
}289
}291
/** Rounds to an integral floating value with ties to even. */292
public fun rint(x: Float): Float {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: Float, x: Float): Float {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: Float, y: Float): Float {309
@Unsafe310
{311
return nativeHypot(x, y)312
}313
}315
/** Calls the system fmaf, retaining its rounding behavior. */316
public fun fma(a: Float, b: Float, c: Float): Float {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: Float, y: Float): Float {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: Float, scaleFactor: Int): Float {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: Float, y: Float): Float {341
@Unsafe342
{343
return nativePow(x, y)344
}345
}347
@GCLeaf348
@Extern(name = "sinf")349
private fun nativeSin(x: Float): Float351
@GCLeaf352
@Extern(name = "cosf")353
private fun nativeCos(x: Float): Float355
@GCLeaf356
@Extern(name = "tanf")357
private fun nativeTan(x: Float): Float359
@GCLeaf360
@Extern(name = "asinf")361
private fun nativeAsin(x: Float): Float363
@GCLeaf364
@Extern(name = "acosf")365
private fun nativeAcos(x: Float): Float367
@GCLeaf368
@Extern(name = "atanf")369
private fun nativeAtan(x: Float): Float371
@GCLeaf372
@Extern(name = "sinhf")373
private fun nativeSinh(x: Float): Float375
@GCLeaf376
@Extern(name = "coshf")377
private fun nativeCosh(x: Float): Float379
@GCLeaf380
@Extern(name = "tanhf")381
private fun nativeTanh(x: Float): Float383
@GCLeaf384
@Extern(name = "expf")385
private fun nativeExp(x: Float): Float387
@GCLeaf388
@Extern(name = "expm1f")389
private fun nativeExpm1(x: Float): Float391
@GCLeaf392
@Extern(name = "logf")393
private fun nativeLog(x: Float): Float395
@GCLeaf396
@Extern(name = "log10f")397
private fun nativeLog10(x: Float): Float399
@GCLeaf400
@Extern(name = "log1pf")401
private fun nativeLog1p(x: Float): Float403
@GCLeaf404
@Extern(name = "sqrtf")405
private fun nativeSqrt(x: Float): Float407
@GCLeaf408
@Extern(name = "cbrtf")409
private fun nativeCbrt(x: Float): Float411
@GCLeaf412
@Extern(name = "floorf")413
private fun nativeFloor(x: Float): Float415
@GCLeaf416
@Extern(name = "ceilf")417
private fun nativeCeil(x: Float): Float419
@GCLeaf420
@Extern(name = "rintf")421
private fun nativeRint(x: Float): Float423
@GCLeaf424
@Extern(name = "atan2f")425
private fun nativeAtan2(y: Float, x: Float): Float427
@GCLeaf428
@Extern(name = "scoop_math_hypot_float")429
private fun nativeHypot(x: Float, y: Float): Float431
@GCLeaf432
@Extern(name = "fmaf")433
private fun nativeFma(a: Float, b: Float, c: Float): Float435
@GCLeaf436
@Extern(name = "remainderf")437
private fun nativeIeeeRemainder(x: Float, y: Float): Float439
@GCLeaf440
@Extern(name = "scalbnf")441
private fun nativeScalb(x: Float, scaleFactor: Int): Float443
@GCLeaf444
@Extern(name = "powf")445
private fun nativePow(x: Float, y: Float): Float