kotlin-multiplatform-bignum icon indicating copy to clipboard operation
kotlin-multiplatform-bignum copied to clipboard

bigDecimal.doubleValue(true) fail

Open D10NGYANG opened this issue 2 years ago • 1 comments

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

  • [JVM, JS, Native, iOS]

If JS (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

If Smartphone (please complete the following information):

  • Device: [e.g. Google Pixel 3]
  • OS: [e.g. Android 9.0]
  • Browser [e.g. Chrome]
  • Version [e.g. 22]

Additional context

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

D10NGYANG avatar Aug 12 '22 02:08 D10NGYANG

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.

ionspin avatar Aug 12 '22 07:08 ionspin