• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Keelar/ExprK: A simple mathematical expression evaluator for Kotlin and Java, wr ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

Keelar/ExprK

开源软件地址(OpenSource Url):

https://github.com/Keelar/ExprK

开源编程语言(OpenSource Language):

Kotlin 100.0%

开源软件介绍(OpenSource Introduction):

ExprK

A simple mathematical expression evaluator for Kotlin and Java, written in Kotlin.

Features:

  • Uses BigDecimal for calculations and results
  • Allows you to define variables using values or expressions
  • Variable definition expressions can reference previously defined variables
  • Configurable precision and rounding mode
  • Functions and the ability to define new ones

Supported operators

Arithmetic operators

Name Operator
Plus +
Minus -
Multiply *
Divide /
Modulus %
Exponent ^

Logical operators

Name Operator
And &&
Or ||

Pre-defined variables

Variable Value
pi 3.141592653589793
e 2.718281828459045

Pre-defined functions

Function Description
abs(expression) Returns the absolute value of the expression
sum(expression, ...) Returns the sum of all arguments
floor(expression) Rounds the value of the expression down to the nearest integer
ceil(expression) Rounds the value of the expression up to the nearest integer
round(expression) Rounds the value of the expression to the nearest integer in the direction decided by the configured rounding mode
min(expression, ...) Returns the value of the smallest argument
max(expression, ...) Returns the value of the largest argument
if(condition, trueValue, falseValue) Returns trueValue if condition is true(condition != 0), otherwise it returns falseValue

Examples:

val result = Expressions()
    .eval("(5+5)*10") // returns 100

You can define variables with the define method.

val result = Expressions()
    .define("x", 5)
    .eval("x*10") // returns 50

The define method returns the expression instance to allow chaining definition method calls together.

val result = Expressions()
    .define("x", 5)
    .define("y", "5*2")
    .eval("x*y") // returns 50

Variable definition expressions can reference previously defined variables.

val result = Expressions()
    .define("x", 5)
    .define("y", "x^2")
    .eval("y*x") // returns 125

You can add new functions with the addFunction method.

val result = Expressions()
    .addFunction("min") { arguments ->
        if (arguments.isEmpty()) throw ExpressionException(
                "min requires at least one argument")

        arguments.min()!!
    }
    .eval("min(4, 8, 16)") // returns 4

You can set the precision and rounding mode with setPrecision and setRoundingMode.

val result = Expressions()
    .setPrecision(128)
    .setRoundingMode(RoundingMode.UP)
    .eval("222^3/5.5") 



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap