korlibs-archive / kbignum Goto Github PK
View Code? Open in Web Editor NEWBig Integers and decimals in Kotlin Common
Home Page: https://korlibs.soywiz.com/kbignum
License: MIT License
Big Integers and decimals in Kotlin Common
Home Page: https://korlibs.soywiz.com/kbignum
License: MIT License
Hello. First of all, thank you for creating multiplatform libs for Kotlin! I spent hours reading code on Github and documentation. It's useful for entire community.
Let's talk about this library.
BigDecimal test 1: ended after 3.947937 ms
BigNum test 1: ended after 61.848497 ms
0.00
0.01
0.02
0.02
0.03
0.03
0.04
0.05
0.05
0.06
BigDecimal test 2: ended after 0.75538 ms
0
0
0
0
0
0
0
0
0
0
BigNum test 2: ended after 2.097193 ms
Code for tests:
val bigDecimals = arrayListOf<BigDecimal>()
val bigNums = arrayListOf<BigNum>()
timeCounter(true, "BigDecimal test 1") {
repeat(10) {
bigDecimals.add(BigDecimal(it.toString()))
}
}
timeCounter(true, "BigNum test 1") {
repeat(10) {
bigNums.add(BigNum(it.toString()))
}
}
val bigDecimal = BigDecimal("173.0")
val bigNum = BigNum("173.0")
timeCounter(true, "BigDecimal test 2") {
repeat(10) {
println(bigDecimals[it].divide(bigDecimal, 2, RoundingMode.CEILING))
}
}
timeCounter(true, "BigNum test 2") {
repeat(10) {
println(bigNums[it].div(bigNum, 2))
}
}
So seems like perfomance could be better. Also there is no working divide method and no any RoundingMode option.
Please, tell me you plans for this lib because I really hope to find BigDecimal alternative for multiplatform configuration. I'm not sure that my knowledge in Math can help you...
Got this error when try to convert BigNum from scale i.e. 10 to scale 2
Hello,
Thanks for all your great work on the Korlibs libraries. Finding a multiplatform arbitrary-precision number library (or even just expect/actualing it on JVM, iOS, and NodeJS) has proved to be a bit of a challenge :).
I have found the following test fails:
assertEquals("-0.0001", "-0.0001".bn.toString())
with
AssertionError: Expected <-0.0001>, actual <0.00-1>.
In the toString() method of BigNum, this line (line 90 in my copy of the source, v1.10.1):
"0." + "0".repeat(-pos) + out
is not taking negativity of the number into consideration, so the "-" is going with "out" instead of being prefixed to the entire number.
As a workaround in my local I have added this extension method:
fun BigNum.toStringCorrected(): String {
//return "BigNum($int, $scale)"
val isNegative = int < BigInt.ZERO
val out = "${int.abs()}"
val pos = out.length - scale
return (if (isNegative) "-" else "") + if (pos <= 0) {
"0." + "0".repeat(-pos) + out
} else {
(out.substring(0, pos) + "." + out.substring(pos)).trimEnd('.')
}
}
I haven't extensively tested it yet, but this could be a possible solution.
Thanks for your efforts!
Richard
v2.4.12
println(BigInt("616e794474736574", 16).toString())
Outputs
jvm (correct):
7020682204035638644
ios (incorrect):
1953719668
v2.4.1.1 has the correct output for both
Hello. Will be this library updated to kotlin 1.4.0 or 1.4.10?
In JavaScript, BigNum("") == BigNum("0")
whereas on the JVM BigNum("")
throws an exception, as expected.
I don't know why there is no such already, explain if it is reasonable
fun div(other: BigNum, precision: Int): BigNum
is broken, it does not take into account precision parameter at all.
The implementation should be something like:
operator fun div(other: BigNum, precision: Int = other.int.significantBits / 2): BigNum {
return BigNum(int * (10.bi pow (other.scale + precision)) / other.int, scale) * BigNum(1.bi, precision)
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.