fasseg / exp4j

A tiny math expression evaluator for the Java programming language
http://www.objecthunter.net/exp4j/
Apache License 2.0
475 stars 160 forks source link

Expression building failing with leading numbers in variable name #109

Open apoorv-28 opened 3 years ago

apoorv-28 commented 3 years ago

Expression : 1*2a

Error: Unknown function or variable 'a' at pos 3 in expression '1*2a'

Code:

val map = Map("2a" -> 0.1)
val expressionBuilder = new ExpressionBuilder("1*2a")
  .variables(map.keys.toList: _*)
  .build

Is this not supported or is it a probable bug?

seyfahni commented 3 years ago

It is currently not supported: https://www.objecthunter.net/exp4j/#Variable_declaration

Variable names must start with a letter or the underscore

Kris-kun commented 3 years ago

Well, propably because someting like "2a" is mathematically correct when "a" is a variable ("2a" = "2*a"). So if you had a variable "a" and "2a", the expression couldn't be compiled because it doesn't know which to use ("2a" as a variable or two times "a").