Scoop API

scoop:scoop.math:0.1.0

src/scalar/floating_double.scoop

Selected production source · src/scalar/floating_double.scoop

1package scoop.math
3import scoop.math.min as numericMin
4import scoop.math.max as numericMax
6/** Clears the sign bit, retaining every other bit including NaN payloads. */
7public fun abs(x: Double): Double = Double.fromBits(x.toBits() and Long.MAX_VALUE)
9/** Copies only the sign bit from sign into magnitude. */
10public 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. */
14public fun signum(x: Double): Double =
15 if (x.isNaN() || x == 0.0)
16 x
17 else
18 copySign(1.0, x)
20/** Returns the smaller value, propagating NaN and ordering negative zero first. */
21public fun min(a: Double, b: Double): Double {
22 if (a.isNaN())
23 return a
24 if (b.isNaN())
25 return b
26 if (a == 0.0 && b == 0.0)
27 return Double.fromBits(a.toBits() or b.toBits())
28 return if (a <= b)
29 a
30 else
31 b
34/** Returns the larger value, propagating NaN and ordering negative zero first. */
35public fun max(a: Double, b: Double): Double {
36 if (a.isNaN())
37 return a
38 if (b.isNaN())
39 return b
40 if (a == 0.0 && b == 0.0)
41 return Double.fromBits(a.toBits() and b.toBits())
42 return if (a >= b)
43 a
44 else
45 b
48/** Clamps to inclusive numeric bounds; NaN or reversed bounds throw. */
49public 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)
61/** Returns the adjacent representable value toward positive infinity. */
62public fun nextUp(x: Double): Double {
63 if (x.isNaN() || x == Double.POSITIVE_INFINITY)
64 return x
65 if (x == 0.0)
66 return Double.MIN_VALUE
67 return Double.fromBits(
68 x.toBits() +
69 (
70 if (x < 0.0)
71 -1L
72 else
73 1L
74 )
75 )
78/** Returns the adjacent representable value toward negative infinity. */
79public fun nextDown(x: Double): Double {
80 if (x.isNaN() || x == Double.NEGATIVE_INFINITY)
81 return x
82 if (x == 0.0)
83 return -Double.MIN_VALUE
84 return Double.fromBits(
85 x.toBits() +
86 (
87 if (x < 0.0)
88 1L
89 else
90 -1L
91 )
92 )
95/** Returns the neighbor toward direction; equal inputs return direction including its zero sign. */
96public fun nextAfter(x: Double, direction: Double): Double {
97 if (x.isNaN())
98 return x
99 if (direction.isNaN())
100 return direction
101 if (x == direction)
102 return direction
103 return if (x < direction)
104 nextUp(x)
105 else
106 nextDown(x)
109/** Returns the unbiased exponent; zero/subnormal and nonfinite values use boundary sentinels. */
110public fun getExponent(x: Double): Int = ((x.toBits() ushr 52L) and 0x7ffL).toInt() - 1023
112/** Returns the spacing at the magnitude of x, with IEEE zero and nonfinite handling. */
113public 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_VALUE
119 val power = exponent - 52
120 return if (power >= -1022)
121 Double.fromBits((power + 1023).toLong() shl 52L)
122 else
123 Double.fromBits(1L shl (power - (-1074)).toLong())
126/** Rounds to the nearest integer with ties toward positive infinity; overflow saturates and NaN becomes zero. */
127public 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.0
134 else
135 lower
136 ).toLong()
139/** Converts angle units using a coefficient rounded directly to the operand precision. */
140public fun toRadians(x: Double): Double =
141 x * 0.017453292519943295769236907684886127134428718885417254560971914401710091146034494
143/** Converts angle units using a coefficient rounded directly to the operand precision. */
144public fun toDegrees(x: Double): Double =
145 x * 57.295779513082320876798154814105170332405472466564321549160243861202847148321553
147/** Evaluates sin at the operand precision using the target math library. */
148public fun sin(x: Double): Double {
149 @Unsafe
150 {
151 return nativeSin(x)
152 }
155/** Evaluates cos at the operand precision using the target math library. */
156public fun cos(x: Double): Double {
157 @Unsafe
158 {
159 return nativeCos(x)
160 }
163/** Evaluates tan at the operand precision using the target math library. */
164public fun tan(x: Double): Double {
165 @Unsafe
166 {
167 return nativeTan(x)
168 }
171/** Evaluates asin at the operand precision using the target math library. */
172public fun asin(x: Double): Double {
173 @Unsafe
174 {
175 return nativeAsin(x)
176 }
179/** Evaluates acos at the operand precision using the target math library. */
180public fun acos(x: Double): Double {
181 @Unsafe
182 {
183 return nativeAcos(x)
184 }
187/** Evaluates atan at the operand precision using the target math library. */
188public fun atan(x: Double): Double {
189 @Unsafe
190 {
191 return nativeAtan(x)
192 }
195/** Evaluates sinh at the operand precision using the target math library. */
196public fun sinh(x: Double): Double {
197 @Unsafe
198 {
199 return nativeSinh(x)
200 }
203/** Evaluates cosh at the operand precision using the target math library. */
204public fun cosh(x: Double): Double {
205 @Unsafe
206 {
207 return nativeCosh(x)
208 }
211/** Evaluates tanh at the operand precision using the target math library. */
212public fun tanh(x: Double): Double {
213 @Unsafe
214 {
215 return nativeTanh(x)
216 }
219/** Evaluates exp at the operand precision using the target math library. */
220public fun exp(x: Double): Double {
221 @Unsafe
222 {
223 return nativeExp(x)
224 }
227/** Evaluates expm1 at the operand precision using the target math library. */
228public fun expm1(x: Double): Double {
229 @Unsafe
230 {
231 return nativeExpm1(x)
232 }
235/** Evaluates log at the operand precision using the target math library. */
236public fun log(x: Double): Double {
237 @Unsafe
238 {
239 return nativeLog(x)
240 }
243/** Evaluates log10 at the operand precision using the target math library. */
244public fun log10(x: Double): Double {
245 @Unsafe
246 {
247 return nativeLog10(x)
248 }
251/** Evaluates log1p at the operand precision using the target math library. */
252public fun log1p(x: Double): Double {
253 @Unsafe
254 {
255 return nativeLog1p(x)
256 }
259/** Returns the correctly rounded square root, preserving negative zero. */
260public fun sqrt(x: Double): Double {
261 @Unsafe
262 {
263 return nativeSqrt(x)
264 }
267/** Evaluates cbrt at the operand precision using the target math library. */
268public fun cbrt(x: Double): Double {
269 @Unsafe
270 {
271 return nativeCbrt(x)
272 }
275/** Evaluates floor at the operand precision using the target math library. */
276public fun floor(x: Double): Double {
277 @Unsafe
278 {
279 return nativeFloor(x)
280 }
283/** Evaluates ceil at the operand precision using the target math library. */
284public fun ceil(x: Double): Double {
285 @Unsafe
286 {
287 return nativeCeil(x)
288 }
291/** Rounds to an integral floating value with ties to even. */
292public fun rint(x: Double): Double {
293 @Unsafe
294 {
295 return nativeRint(x)
296 }
299/** Evaluates atan2 at the operand precision using the target math library. */
300public fun atan2(y: Double, x: Double): Double {
301 @Unsafe
302 {
303 return nativeAtan2(y, x)
304 }
307/** Evaluates hypot at the operand precision using the target math library. */
308public fun hypot(x: Double, y: Double): Double {
309 @Unsafe
310 {
311 return nativeHypot(x, y)
312 }
315/** Calls the system fma, retaining its rounding behavior. */
316public fun fma(a: Double, b: Double, c: Double): Double {
317 @Unsafe
318 {
319 return nativeFma(a, b, c)
320 }
323/** Returns the IEEE remainder with a nearest-even integer quotient. */
324public fun ieeeRemainder(x: Double, y: Double): Double {
325 @Unsafe
326 {
327 return nativeIeeeRemainder(x, y)
328 }
331/** Scales by an exact power of two with one final rounding. */
332public fun scalb(x: Double, scaleFactor: Int): Double {
333 @Unsafe
334 {
335 return nativeScalb(x, scaleFactor)
336 }
339/** Raises x to y using the target math library's special-value rules. */
340public fun pow(x: Double, y: Double): Double {
341 @Unsafe
342 {
343 return nativePow(x, y)
344 }
347@GCLeaf
348@Extern(name = "sin")
349private fun nativeSin(x: Double): Double
351@GCLeaf
352@Extern(name = "cos")
353private fun nativeCos(x: Double): Double
355@GCLeaf
356@Extern(name = "tan")
357private fun nativeTan(x: Double): Double
359@GCLeaf
360@Extern(name = "asin")
361private fun nativeAsin(x: Double): Double
363@GCLeaf
364@Extern(name = "acos")
365private fun nativeAcos(x: Double): Double
367@GCLeaf
368@Extern(name = "atan")
369private fun nativeAtan(x: Double): Double
371@GCLeaf
372@Extern(name = "sinh")
373private fun nativeSinh(x: Double): Double
375@GCLeaf
376@Extern(name = "cosh")
377private fun nativeCosh(x: Double): Double
379@GCLeaf
380@Extern(name = "tanh")
381private fun nativeTanh(x: Double): Double
383@GCLeaf
384@Extern(name = "exp")
385private fun nativeExp(x: Double): Double
387@GCLeaf
388@Extern(name = "expm1")
389private fun nativeExpm1(x: Double): Double
391@GCLeaf
392@Extern(name = "log")
393private fun nativeLog(x: Double): Double
395@GCLeaf
396@Extern(name = "log10")
397private fun nativeLog10(x: Double): Double
399@GCLeaf
400@Extern(name = "log1p")
401private fun nativeLog1p(x: Double): Double
403@GCLeaf
404@Extern(name = "sqrt")
405private fun nativeSqrt(x: Double): Double
407@GCLeaf
408@Extern(name = "cbrt")
409private fun nativeCbrt(x: Double): Double
411@GCLeaf
412@Extern(name = "floor")
413private fun nativeFloor(x: Double): Double
415@GCLeaf
416@Extern(name = "ceil")
417private fun nativeCeil(x: Double): Double
419@GCLeaf
420@Extern(name = "rint")
421private fun nativeRint(x: Double): Double
423@GCLeaf
424@Extern(name = "atan2")
425private fun nativeAtan2(y: Double, x: Double): Double
427@GCLeaf
428@Extern(name = "hypot")
429private fun nativeHypot(x: Double, y: Double): Double
431@GCLeaf
432@Extern(name = "fma")
433private fun nativeFma(a: Double, b: Double, c: Double): Double
435@GCLeaf
436@Extern(name = "remainder")
437private fun nativeIeeeRemainder(x: Double, y: Double): Double
439@GCLeaf
440@Extern(name = "scalbn")
441private fun nativeScalb(x: Double, scaleFactor: Int): Double
443@GCLeaf
444@Extern(name = "pow")
445private fun nativePow(x: Double, y: Double): Double