Scoop API

scoop:scoop.core:0.1.0

src/floating.scoop

Selected production source · src/floating.scoop

1/**
2 * IEEE 754 binary32 floating-point values, including infinities, signed zero, and NaNs. Default
3 * equality treats all NaNs as equal and both zeros as equal; [ieeeCompare] provides unordered IEEE
4 * comparison. Arithmetic uses round-to-nearest, ties-to-even.
5 */
6@Intrinsic("core_float")
7public struct Float :
8 Add<Float, Float>,
9 Sub<Float, Float>,
10 Mul<Float, Float>,
11 Div<Float, Float>,
12 Rem<Float, Float>,
13 UnaryPlus<Float>,
14 UnaryMinus<Float>,
15 Increment<Float>,
16 Decrement<Float>,
17 Equals<Float>, Compare<Float>, ToString {
18 /**
19 * Representation constants, raw-bit construction, and scalar codec operations.
20 */
21 public companion object : ValueEncoder<Float>, ValueDecoder<Float>, ValueInspector<Float> {
22 /**
23 * Exposes the floating-point value through scalar inspection.
24 */
25 public override fun inspect(value: Float): ValueView =
26 ValueObservation(typeInfo<Float>().type, None, { ValueShape.Scalar(ScalarValue.Float(value)) })
28 /**
29 * Writes the value through the encoder's matching floating-point scalar operation.
30 */
31 public override fun encode(value: Float, encoder: Encoder): Unit =
32 encoder.singleValue().writeFloat(value)
34 /**
35 * Reads the value through the decoder's matching floating-point scalar operation.
36 */
37 public override fun decode(decoder: Decoder): Float =
38 decoder.singleValue().readFloat()
40 /**
41 * A not-a-number value. Use isNaN to classify NaNs.
42 */
43 public const val NaN: Float = 0f / 0f
44 /**
45 * The positive unbounded floating-point value.
46 */
47 public const val POSITIVE_INFINITY: Float = 1f / 0f
48 /**
49 * The negative unbounded floating-point value.
50 */
51 public const val NEGATIVE_INFINITY: Float = -POSITIVE_INFINITY
52 /**
53 * The largest finite positive value.
54 */
55 public const val MAX_VALUE: Float = 3.4028234663852886e38f
56 /**
57 * The smallest positive subnormal value, not the most negative value.
58 */
59 public const val MIN_VALUE: Float = 1.401298464324817e-45f
60 /**
61 * The smallest positive normal value.
62 */
63 public const val MIN_NORMAL: Float = 1.1754943508222875e-38f
65 /**
66 * Constructs a value from its exact representation bits, including any NaN payload or
67 * signed zero.
68 */
69 public fun fromBits(bits: Int): Float {
70 @Unsafe { return addressOf(bits).cast<Float>().load() }
71 }
72 }
74 /**
75 * Returns the raw representation bits, preserving signed zeros and NaN payloads.
76 */
77 @NoGC
78 public fun toBits(): Int {
79 @Unsafe { return addressOf(this).cast<Int>().load() }
80 }
82 /**
83 * Returns the same floating-point representation, preserving signed zero and NaN bits.
84 */
85 @NoGC @Intrinsic("float_unary_plus")
86 public override operator fun unaryPlus(): Float
88 /**
89 * Flips the sign bit, including for zero and NaN.
90 */
91 @NoGC @Intrinsic("float_unary_minus")
92 public override operator fun unaryMinus(): Float
94 /**
95 * Adds one at the receiver's floating-point precision.
96 */
97 @NoGC @Intrinsic("float_inc")
98 public override operator fun inc(): Float
100 /**
101 * Subtracts one at the receiver's floating-point precision.
102 */
103 @NoGC @Intrinsic("float_dec")
104 public override operator fun dec(): Float
106 /**
107 * Adds at the receiver's precision using IEEE rounding and non-finite results.
108 */
109 @NoGC @Intrinsic("float_plus")
110 public override operator fun plus(other: Float): Float
112 /**
113 * Subtracts at the receiver's precision using IEEE rounding and non-finite results.
114 */
115 @NoGC @Intrinsic("float_minus")
116 public override operator fun minus(other: Float): Float
118 /**
119 * Multiplies at the receiver's precision using IEEE rounding and non-finite results.
120 */
121 @NoGC @Intrinsic("float_times")
122 public override operator fun times(other: Float): Float
124 /**
125 * Performs IEEE division; zero divisors produce infinity or NaN rather than an integer
126 * arithmetic exception.
127 */
128 @NoGC @Intrinsic("float_div")
129 public override operator fun div(other: Float): Float
131 /**
132 * Computes the remainder using a quotient truncated toward zero. Invalid operations produce
133 * NaN.
134 */
135 @NoGC @Intrinsic("float_rem")
136 public override operator fun rem(other: Float): Float
138 /**
139 * Compares numerical values, treating all NaNs as one equal class and both signed zeros as
140 * equal.
141 */
142 @NoGC @Intrinsic("float_equals")
143 public override operator fun equals(other: Float): Boolean
145 /**
146 * Tests whether the value has any NaN representation. Default self-inequality is not a NaN
147 * test.
148 */
149 @NoGC @Intrinsic("float_is_nan")
150 public fun isNaN(): Boolean
152 /**
153 * Returns true for positive or negative infinity.
154 */
155 @NoGC @Intrinsic("float_is_infinite")
156 public fun isInfinite(): Boolean
158 /**
159 * Returns true for finite values, including subnormal values and both zeros.
160 */
161 @NoGC @Intrinsic("float_is_finite")
162 public fun isFinite(): Boolean
164 /**
165 * Returns -1, 0, or 1 in numerical order, with all NaNs equal and after positive infinity;
166 * signed zeros compare equally.
167 */
168 @NoGC @Intrinsic("float_compare_to")
169 public override operator fun compareTo(other: Float): Long
171 /**
172 * Returns [IeeeComparison.Unordered] if either operand is NaN; otherwise compares numerical
173 * values with both zeros equal.
174 */
175 @NoGC
176 public fun ieeeCompare(other: Float): IeeeComparison {
177 if (isNaN() || other.isNaN()) return IeeeComparison.Unordered
178 val order = compareTo(other)
179 if (order < 0L) return IeeeComparison.Less
180 if (order > 0L) return IeeeComparison.Greater
181 return IeeeComparison.Equal
182 }
184 /**
185 * Truncates toward zero and saturates to the 8-bit signed range. NaN becomes zero.
186 */
187 @NoGC @Intrinsic("float_to_int8")
188 public fun toByte(): Byte
190 /**
191 * Truncates toward zero and saturates to the 16-bit signed range. NaN becomes zero.
192 */
193 @NoGC @Intrinsic("float_to_int16")
194 public fun toShort(): Short
196 /**
197 * Truncates toward zero and saturates to the 32-bit signed range. NaN becomes zero.
198 */
199 @NoGC @Intrinsic("float_to_int")
200 public fun toInt(): Int
202 /**
203 * Truncates toward zero and saturates to the 64-bit signed range. NaN becomes zero.
204 */
205 @NoGC @Intrinsic("float_to_long")
206 public fun toLong(): Long
208 /**
209 * Truncates toward zero and saturates to the 8-bit unsigned range. NaN becomes zero.
210 */
211 @NoGC @Intrinsic("float_to_uint8")
212 public fun toUByte(): UByte
214 /**
215 * Truncates toward zero and saturates to the 16-bit unsigned range. NaN becomes zero.
216 */
217 @NoGC @Intrinsic("float_to_uint16")
218 public fun toUShort(): UShort
220 /**
221 * Truncates toward zero and saturates to the 32-bit unsigned range. NaN becomes zero.
222 */
223 @NoGC @Intrinsic("float_to_uint")
224 public fun toUInt(): UInt
226 /**
227 * Truncates toward zero and saturates to the 64-bit unsigned range. NaN becomes zero.
228 */
229 @NoGC @Intrinsic("float_to_ulong")
230 public fun toULong(): ULong
232 /**
233 * Converts to binary32 with nearest-even rounding. Overflow may produce infinity; a zero keeps
234 * its sign.
235 */
236 @NoGC @Intrinsic("float_to_float")
237 public fun toFloat(): Float
239 /**
240 * Converts to binary64. Every finite Float value is represented exactly; a zero keeps its
241 * sign.
242 */
243 @NoGC @Intrinsic("float_to_double")
244 public fun toDouble(): Double
246 /**
247 * Returns a locale-independent shortest round-tripping decimal representation, or the
248 * corresponding NaN/infinity spelling.
249 */
250 public override fun toString(): String = coreFloatToString(this)
253/**
254 * IEEE 754 binary64 floating-point values, including infinities, signed zero, and NaNs. Default
255 * equality treats all NaNs as equal and both zeros as equal; [ieeeCompare] provides unordered IEEE
256 * comparison. Arithmetic uses round-to-nearest, ties-to-even.
257 */
258@Intrinsic("core_double")
259public struct Double :
260 Add<Double, Double>,
261 Sub<Double, Double>,
262 Mul<Double, Double>,
263 Div<Double, Double>,
264 Rem<Double, Double>,
265 UnaryPlus<Double>,
266 UnaryMinus<Double>,
267 Increment<Double>,
268 Decrement<Double>,
269 Equals<Double>, Compare<Double>, ToString {
270 /**
271 * Representation constants, raw-bit construction, and scalar codec operations.
272 */
273 public companion object : ValueEncoder<Double>, ValueDecoder<Double>, ValueInspector<Double> {
274 /**
275 * Exposes the floating-point value through scalar inspection.
276 */
277 public override fun inspect(value: Double): ValueView =
278 ValueObservation(typeInfo<Double>().type, None, { ValueShape.Scalar(ScalarValue.Double(value)) })
280 /**
281 * Writes the value through the encoder's matching floating-point scalar operation.
282 */
283 public override fun encode(value: Double, encoder: Encoder): Unit =
284 encoder.singleValue().writeDouble(value)
286 /**
287 * Reads the value through the decoder's matching floating-point scalar operation.
288 */
289 public override fun decode(decoder: Decoder): Double =
290 decoder.singleValue().readDouble()
292 /**
293 * A not-a-number value. Use isNaN to classify NaNs.
294 */
295 public const val NaN: Double = 0.0 / 0.0
296 /**
297 * The positive unbounded floating-point value.
298 */
299 public const val POSITIVE_INFINITY: Double = 1.0 / 0.0
300 /**
301 * The negative unbounded floating-point value.
302 */
303 public const val NEGATIVE_INFINITY: Double = -POSITIVE_INFINITY
304 /**
305 * The largest finite positive value.
306 */
307 public const val MAX_VALUE: Double = 1.7976931348623157e308
308 /**
309 * The smallest positive subnormal value, not the most negative value.
310 */
311 public const val MIN_VALUE: Double = 4.9406564584124654e-324
312 /**
313 * The smallest positive normal value.
314 */
315 public const val MIN_NORMAL: Double = 2.2250738585072014e-308
317 /**
318 * Constructs a value from its exact representation bits, including any NaN payload or
319 * signed zero.
320 */
321 public fun fromBits(bits: Long): Double {
322 @Unsafe { return addressOf(bits).cast<Double>().load() }
323 }
324 }
326 /**
327 * Returns the raw representation bits, preserving signed zeros and NaN payloads.
328 */
329 @NoGC
330 public fun toBits(): Long {
331 @Unsafe { return addressOf(this).cast<Long>().load() }
332 }
334 /**
335 * Returns the same floating-point representation, preserving signed zero and NaN bits.
336 */
337 @NoGC @Intrinsic("double_unary_plus")
338 public override operator fun unaryPlus(): Double
340 /**
341 * Flips the sign bit, including for zero and NaN.
342 */
343 @NoGC @Intrinsic("double_unary_minus")
344 public override operator fun unaryMinus(): Double
346 /**
347 * Adds one at the receiver's floating-point precision.
348 */
349 @NoGC @Intrinsic("double_inc")
350 public override operator fun inc(): Double
352 /**
353 * Subtracts one at the receiver's floating-point precision.
354 */
355 @NoGC @Intrinsic("double_dec")
356 public override operator fun dec(): Double
358 /**
359 * Adds at the receiver's precision using IEEE rounding and non-finite results.
360 */
361 @NoGC @Intrinsic("double_plus")
362 public override operator fun plus(other: Double): Double
364 /**
365 * Subtracts at the receiver's precision using IEEE rounding and non-finite results.
366 */
367 @NoGC @Intrinsic("double_minus")
368 public override operator fun minus(other: Double): Double
370 /**
371 * Multiplies at the receiver's precision using IEEE rounding and non-finite results.
372 */
373 @NoGC @Intrinsic("double_times")
374 public override operator fun times(other: Double): Double
376 /**
377 * Performs IEEE division; zero divisors produce infinity or NaN rather than an integer
378 * arithmetic exception.
379 */
380 @NoGC @Intrinsic("double_div")
381 public override operator fun div(other: Double): Double
383 /**
384 * Computes the remainder using a quotient truncated toward zero. Invalid operations produce
385 * NaN.
386 */
387 @NoGC @Intrinsic("double_rem")
388 public override operator fun rem(other: Double): Double
390 /**
391 * Compares numerical values, treating all NaNs as one equal class and both signed zeros as
392 * equal.
393 */
394 @NoGC @Intrinsic("double_equals")
395 public override operator fun equals(other: Double): Boolean
397 /**
398 * Tests whether the value has any NaN representation. Default self-inequality is not a NaN
399 * test.
400 */
401 @NoGC @Intrinsic("double_is_nan")
402 public fun isNaN(): Boolean
404 /**
405 * Returns true for positive or negative infinity.
406 */
407 @NoGC @Intrinsic("double_is_infinite")
408 public fun isInfinite(): Boolean
410 /**
411 * Returns true for finite values, including subnormal values and both zeros.
412 */
413 @NoGC @Intrinsic("double_is_finite")
414 public fun isFinite(): Boolean
416 /**
417 * Returns -1, 0, or 1 in numerical order, with all NaNs equal and after positive infinity;
418 * signed zeros compare equally.
419 */
420 @NoGC @Intrinsic("double_compare_to")
421 public override operator fun compareTo(other: Double): Long
423 /**
424 * Returns [IeeeComparison.Unordered] if either operand is NaN; otherwise compares numerical
425 * values with both zeros equal.
426 */
427 @NoGC
428 public fun ieeeCompare(other: Double): IeeeComparison {
429 if (isNaN() || other.isNaN()) return IeeeComparison.Unordered
430 val order = compareTo(other)
431 if (order < 0L) return IeeeComparison.Less
432 if (order > 0L) return IeeeComparison.Greater
433 return IeeeComparison.Equal
434 }
436 /**
437 * Truncates toward zero and saturates to the 8-bit signed range. NaN becomes zero.
438 */
439 @NoGC @Intrinsic("double_to_int8")
440 public fun toByte(): Byte
442 /**
443 * Truncates toward zero and saturates to the 16-bit signed range. NaN becomes zero.
444 */
445 @NoGC @Intrinsic("double_to_int16")
446 public fun toShort(): Short
448 /**
449 * Truncates toward zero and saturates to the 32-bit signed range. NaN becomes zero.
450 */
451 @NoGC @Intrinsic("double_to_int")
452 public fun toInt(): Int
454 /**
455 * Truncates toward zero and saturates to the 64-bit signed range. NaN becomes zero.
456 */
457 @NoGC @Intrinsic("double_to_long")
458 public fun toLong(): Long
460 /**
461 * Truncates toward zero and saturates to the 8-bit unsigned range. NaN becomes zero.
462 */
463 @NoGC @Intrinsic("double_to_uint8")
464 public fun toUByte(): UByte
466 /**
467 * Truncates toward zero and saturates to the 16-bit unsigned range. NaN becomes zero.
468 */
469 @NoGC @Intrinsic("double_to_uint16")
470 public fun toUShort(): UShort
472 /**
473 * Truncates toward zero and saturates to the 32-bit unsigned range. NaN becomes zero.
474 */
475 @NoGC @Intrinsic("double_to_uint")
476 public fun toUInt(): UInt
478 /**
479 * Truncates toward zero and saturates to the 64-bit unsigned range. NaN becomes zero.
480 */
481 @NoGC @Intrinsic("double_to_ulong")
482 public fun toULong(): ULong
484 /**
485 * Converts to binary32 with nearest-even rounding. Overflow may produce infinity; a zero keeps
486 * its sign.
487 */
488 @NoGC @Intrinsic("double_to_float")
489 public fun toFloat(): Float
491 /**
492 * Converts to binary64. Every finite Float value is represented exactly; a zero keeps its
493 * sign.
494 */
495 @NoGC @Intrinsic("double_to_double")
496 public fun toDouble(): Double
498 /**
499 * Returns a locale-independent shortest round-tripping decimal representation, or the
500 * corresponding NaN/infinity spelling.
501 */
502 public override fun toString(): String = coreDoubleToString(this)
505/**
506 * A transparent alias for [Float] with the same representation and operations.
507 */
508public typealias Float32 = Float
509/**
510 * A transparent alias for [Double] with the same representation and operations.
511 */
512public typealias Float64 = Double