support for Decimal.isInfitite and isNan
This commit is contained in:
parent
a976702caf
commit
6e9333844e
@ -166,6 +166,17 @@ assertEquals(2.9, "2.9".d.toReal())
|
||||
|
||||
Use `toReal()` only when you are willing to return to binary floating-point semantics.
|
||||
|
||||
## Non-Finite Checks
|
||||
|
||||
`Decimal` values are always finite, so these helpers exist for API symmetry with `Real` and always return `false`:
|
||||
|
||||
```lyng
|
||||
import lyng.decimal
|
||||
|
||||
assertEquals(false, "2.9".d.isInfinite())
|
||||
assertEquals(false, "2.9".d.isNaN())
|
||||
```
|
||||
|
||||
## Division Context
|
||||
|
||||
Division is the operation where precision and rounding matter most.
|
||||
|
||||
@ -94,6 +94,12 @@ object ObjDecimalSupport {
|
||||
decimalClass.addFn("toReal") {
|
||||
ObjReal.of(valueOf(thisObj).doubleValue(false))
|
||||
}
|
||||
decimalClass.addFn("isInfinite") {
|
||||
ObjFalse
|
||||
}
|
||||
decimalClass.addFn("isNaN") {
|
||||
ObjFalse
|
||||
}
|
||||
decimalClass.addFn("toString") {
|
||||
ObjString(valueOf(thisObj).toStringExpanded())
|
||||
}
|
||||
|
||||
@ -161,6 +161,10 @@ extern class Decimal() {
|
||||
extern fun toInt(): Int
|
||||
/** Convert to `Real`. */
|
||||
extern fun toReal(): Real
|
||||
/** Return true if this decimal is positive or negative infinity. Always false for Decimal. */
|
||||
extern fun isInfinite(): Bool
|
||||
/** Return true if this decimal is NaN (not a number). Always false for Decimal. */
|
||||
extern fun isNaN(): Bool
|
||||
/**
|
||||
* Convert to a plain decimal string without scientific notation.
|
||||
*
|
||||
|
||||
@ -51,6 +51,21 @@ class DecimalModuleTest {
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDecimalIsInfiniteAndIsNaN() = runTest {
|
||||
val scope = Script.newScope()
|
||||
scope.eval(
|
||||
"""
|
||||
import lyng.decimal
|
||||
|
||||
assertEquals(false, 0.d.isInfinite())
|
||||
assertEquals(false, "12.34".d.isInfinite())
|
||||
assertEquals(false, 0.d.isNaN())
|
||||
assertEquals(false, "12.34".d.isNaN())
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDecimalModuleMixedIntOperators() = runTest {
|
||||
val scope = Script.newScope()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user