ionspin / kotlin-multiplatform-bignum

A Kotlin multiplatform library for arbitrary precision arithmetics
Apache License 2.0
345 stars 41 forks source link

bigDecimal.doubleValue(true) fail #247

Closed D10NGYANG closed 1 year ago

D10NGYANG commented 1 year ago

Describe the bug

assertTrue {
    val bigDecimal = "12.374".toBigDecimal()
    val doubleExpected = 12.374 // we know this can be represented
    val narrowed = bigDecimal.doubleValue(true)
    narrowed == doubleExpected
}

run with jvm result:

Value cannot be narrowed to float
java.lang.ArithmeticException: Value cannot be narrowed to float
    at com.ionspin.kotlin.bignum.decimal.BigDecimal.doubleValue(BigDecimal.kt:1954)
    at com.d10ng.latlnglib.Test.testA(Test.kt:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:110)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
    at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:62)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker$2.run(TestWorker.java:176)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.executeAndMaintainThreadName(TestWorker.java:129)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:100)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:60)
    at org.gradle.process.internal.worker.child.ActionExecutionWorker.execute(ActionExecutionWorker.java:56)
    at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:133)
    at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:71)
    at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
    at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)

run with js result:

ArithmeticException: Value cannot be narrowed to float
ArithmeticException: Value cannot be narrowed to float
    at Object.captureStack(/var/folders/7z/btl59m41741_t0txp5dcg3lw0000gn/T/_karma_webpack_110625/commons.js:53702)
    at ArithmeticException.constructor(/var/folders/7z/btl59m41741_t0txp5dcg3lw0000gn/T/_karma_webpack_110625/commons.js:54041)
    at ArithmeticException.constructor(/var/folders/7z/btl59m41741_t0txp5dcg3lw0000gn/T/_karma_webpack_110625/commons.js:54067)
    at RuntimeException.init(/var/folders/7z/btl59m41741_t0txp5dcg3lw0000gn/T/_karma_webpack_110625/commons.js:54078)
    at <global>.new ArithmeticException(/var/folders/7z/btl59m41741_t0txp5dcg3lw0000gn/T/_karma_webpack_110625/commons.js:54238)
    at BigDecimal.doubleValue_6taknv$$default(/var/folders/7z/btl59m41741_t0txp5dcg3lw0000gn/T/_karma_webpack_110625/commons.js:2963)
    at NarrowingOperations.doubleValue(/var/folders/7z/btl59m41741_t0txp5dcg3lw0000gn/T/_karma_webpack_110625/commons.js:1199)
    at com.d10ng.latlnglib.Test.testA(/var/folders/7z/btl59m41741_t0txp5dcg3lw0000gn/T/_karma_webpack_110625/commons.js:140)
    at <global>.<unknown>(/var/folders/7z/btl59m41741_t0txp5dcg3lw0000gn/T/_karma_webpack_110625/commons.js:165)
    at Context.<anonymous>(/var/folders/7z/btl59m41741_t0txp5dcg3lw0000gn/T/_karma_webpack_110625/commons.js:12292)

com.d10ng.latlnglib.Test.testA FAILED
    ArithmeticException at /var/folders/7z/btl59m41741_t0txp5dcg3lw0000gn/T/_karma_webpack_110625/commons.js:140
1 test completed, 1 failed

To Reproduce Steps to reproduce the behavior:

  1. Operation
  2. Values to reproduce issue

Expected behavior A clear and concise description of what you expected to happen.

Platform

If JS (please complete the following information):

If Smartphone (please complete the following information):

Additional context

lib version: "com.ionspin.kotlin:bignum:0.3.7"

ionspin commented 1 year ago

Hi @D10NGYANG I don't think 12.374 can be represented precisely. Here's a screenshot from https://www.h-schmidt.net/FloatConverter/IEEE754.html

image

As you can see 12.374 will be represented as 12.37399959564208984375.