Expression

A math expression for evaluating in-game. Supports a wide array of basic math functions, as well as some MathHelper functions

Typical usage involves evaluating named doubles in a math equation with matching variable names.

Default operations and functions supported:

  • {constant values} - any double constant value: 5.0

  • {variable values} - any single-character letter: 'x', 'y'. Must match to the characters the evaluation side expects

  • {+} - addition

  • {-} - subtraction

  • {*} - multiplication

  • {/} - division

  • {%} - modulus

  • {^} - power

  • {(...)} - parentheses

  • {sqrt(...)} - square root

  • {ciel(...)} - round up

  • {floor(...)} - round down

  • {round(...)} - round nearest

  • {ln(...)} - natural logarithm

  • {log(exp, power)} - logarithm of to : log(5, 5) is log5(5)

  • {log10(...)} - log 10

  • {log2(...)} - log 2

  • {abs(...)} - absolute value

  • {sin(...)} - sine (radians)

  • {cos(...)} - cosine (radians)

  • {incr(exp, incr)} - round incrementally: incr(0.913, 0.1) will return 0.90

  • {min(a, b)} - minimum of the two values

  • {max(a, b)} - maximum of the two values

  • {mathHelper methods} - Expression will reflectively evaluate any valid MathHelper method that takes doubles and returns doubles

Author

fzzyhmstrs

Since

0.2.0, min/max since 0.3.7

See also

for use in configs

Samples

import me.fzzyhmstrs.fzzy_config.util.Expression

fun main() { 
   //sampleStart 
   // raw string math equation "x times 10 raised to the y power
val mathString = "(x * 10) ^ y"

// above math equation parsed into an expression
val mathExpression = Expression.parse(mathString)

//we map x and y to their current values
val mathVariables = mapOf(
    'x' to 0.5,
    'y' to 5.0
)

//eval the result, in this case (0.5 * 10) ^ 5.0 = 3125.0
val mathResult = mathExpression.eval(mathVariables) 
   //sampleEnd
}

Inheritors

Types

Link copied to clipboard
object Impl

Functions

Link copied to clipboard
abstract fun eval(vars: Map<Char, Double>): Double

Evaluates the math expression

Link copied to clipboard
open fun evalSafe(vars: Map<Char, Double>, fallback: Double): Double

Evaluates an expression with a fallback. Will fail to fallback instead of throwing. This call is recommended over the 'raw' eval call