keelar / exprk Goto Github PK
View Code? Open in Web Editor NEWA simple mathematical expression evaluator for Kotlin and Java, written in Kotlin.
License: MIT License
A simple mathematical expression evaluator for Kotlin and Java, written in Kotlin.
License: MIT License
This unit test:
@Test
fun `test that scientific notation BigDecimals are parsed and equivalent to the plain representation`() {
val expr = Expressions()
val scival = BigDecimal("1E+7")
expr.define("SCIVAL", scival)
assertEquals(scival.toPlainString(), expr.eval("SCIVAL").toPlainString())
}
Produces this result:
com.github.keelar.exprk.ExpressionException: Expected end of expression, found 'E'
at com.github.keelar.exprk.internal.Parser.parse(Parser.kt:15)
at com.github.keelar.exprk.Expressions.parse(Expressions.kt:173)
at com.github.keelar.exprk.Expressions.parse(Expressions.kt:169)
at com.github.keelar.exprk.Expressions.define(Expressions.kt:141)
at com.github.keelar.exprk.Expressions.define(Expressions.kt:135)
at com.github.keelar.exprk.TestExpressions.test that scientific notation BigDecimals are parsed and equivalent to the plain representation(TestExpressions.kt:13)
Hi,
How to import this library with gradle for my android project ?
E.g:
Instead of using define("x",2), I need to use eval("x=2")
First of all, thanks for your code.
I found a bug, and would like to report it
It's about exponential(^) operation between two bigDecimal number.
I think that a calculation which expected to take a long time to calculate must throw exception about overflow.
e.g.
Expressions().eval("34^3.0E+24")
Expressions().eval("34^3.0E+2451223111")
The code above throw exception about overflow.
But below is not
34^3.0E+2451223
I think it has to have some validation about each power calculation.
Thank you
Hi
Is it possible to define string expression:
e.g:
expressions.define("x", "durim")
It should be possible to obtain and configure an Evaluator
separately from Expressions
, so different functions/constants/etc. can be configured.
(This will also require Expr
and its various subclasses, probably Parser
, and likely other interfaces and classes to be made public. I honestly don't see much reason to restrict visibility of much in the library, it's already nice and tight. IMO the easiest fix is just to make everything public.)
arguments.min()!! and arguments.max()!! throw error in recent kotlin versions. Consider changing it with arguments.minOrNull()!! and arguments.maxAndNull()!! in
Replace this part of the code:
Expressions.kt
evaluator.addFunction("min", object : Function() {
override fun call(arguments: List<BigDecimal>): BigDecimal {
if (arguments.isEmpty()) throw ExpressionException(
"min requires at least one argument")
return arguments.min()!!
}
})
evaluator.addFunction("max", object : Function() {
override fun call(arguments: List<BigDecimal>): BigDecimal {
if (arguments.isEmpty()) throw ExpressionException(
"max requires at least one argument")
return arguments.max()!!
}
})
with this:
Expressions.kt
evaluator.addFunction("min", object : Function() {
override fun call(arguments: List<BigDecimal>): BigDecimal {
if (arguments.isEmpty()) throw ExpressionException(
"min requires at least one argument")
return arguments.minOrNull()!!
}
})
evaluator.addFunction("max", object : Function() {
override fun call(arguments: List<BigDecimal>): BigDecimal {
if (arguments.isEmpty()) throw ExpressionException(
"max requires at least one argument")
return arguments.maxOrNull()!!
}
})
first of all, Thank you for your code!
my question is that how can i configure exponential result?
for example, the result is 10.01+E3 for 10000+10. I want to use exponential only for much larger numbers.
Thank you again.
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.