Free maths expression parser?

Hi,

Does anybody know of a mathematical expression evaluator that can calculate expressions like “2(1.05^(1/5))”? So it needs to be able to do Math.pow(x, y) using ^ and it would also be cool if it could do implicit multiplication (like 2(1+3)).

It’s for a commercial project.

I’ve been searching but can only find commercial API’s like JEP and JFormula. There must be a free one out there… :-\

Thanks a lot,
keith

If you’re using Java 1.6 then can’t you use the built-in JavaScript implementation?

Thanks for pointing that out, but it doesn’t look like javascript can do 2^3, since when I do 2^3 on this site (http://www.merlyn.demon.co.uk/js-logic.htm) it gives me 6 rather than 8!

^ is the Xor operator in java and javascript (and possibly other languages too)

Ah ok, that explains it! thanks

For some reason, this challenge was appealing to me… so I wrote a parser for this.

It supports the following operators: ()^*/± (parentheses can be nested)

You can try it out here, to see if you can break it, make it calculate the wrong answer, etc:
http://woogley.net/misc/MathEval/Test.jar (self-executing JAR)

And you can grab the code here:
http://woogley.net/misc/MathEval/MathEval.java

Hope it helps

1: 1-1-1
2: (1)(1)
3: (1)^(1/2)
4: +1
5: (1/3)3 <- not really sure if that is valid input

It seems fairly intelligent otherwise

:smiley:

Thanks! Nothing like a good ego killer :slight_smile:

Just kidding… I appreciate you posting the bugs (and identifying that invalid input). All of the above have been fixed

Please let me know if you find any more bugs

My parser handles this, how about yours:

-1±2-3 = -6
-1–2-3 = -2

And in that code I don’t see any operator precedence… ?

Did you try those expressions with the Test jar? My parser handles both correctly, so I don’t see your point?

Also, from the code:


// Loop through all operators in order of precedence,
// recalculate the expression after each operation,
// repeat until there is nothing left to do
for (j = 0;j < OPERATORS.length();j++) {

Hm… my bad…

anyway this one fails to evaluate:

-(-.1)

Fixed - thanks

((12)+1)+1
((12))+1

Seems like the parsers barks on “((”

A quick hack could be to replace all “((” by “(1(”

It also crashes on “))”

“1-3+4” evaluates to -6 ?! as in (1-(3+4))

Operator precendence: - and + are on the same level, just like * and /

The parentheses thing was just being caused by using lastIndexOf (which obviously won’t cover mutltiple same-level groups)

Ah yes, this is an important bug, glad you pointed it out. The program was just using the “Please excuse my dear aunt sally” approach without accounting for same-level ops

All bugs above have been resolved

Thanks,
– Matt

I can only crash it on an empty string or nothing between the ( and the ).

So… although there are a few nasty hacks, I can’t break it anymore with valid input.

Wow!!!

Thanks heaps Woogley! That’s amazing, it works perfect! 8) :smiley:

I can’t believe you just went and made it. Your powers need harnessing!

I’m using it to make a program that helps uni students learn finance. I’m tutoring 3 classes and I can see that they hate actually doing questions because working them out on a calculator is a pain in the butt, but doing it in an MS-excel style formula bar is easy.

They’ll have to work out questions like “What’s the net present value of paying $100 now and recieving $40 every year forever, starting in 2 years from now? The discount rate is 10%.”

Then in that funky formula bar you made they can type “-100 + (40/0.1)/(1.1^2)” and read the output then select the right answer from a choice of 4.

I’ll send you a link to it when I’m finished.

Thanks Riven and irreversible_kev for helping.

“1^–1” gives “empty String” error.

As it appears to have no problem evaluating “–1” on it’s own, so perhaps a bug?
(though I can see why it might not be considered a bug, as it won’t accept “1^+1” either - though it gives a more meaningful error)

I don’t think “*1” should be considered a legal expression. (Neither “/1” or “^1” are valid)

Also, if “–1” is a valid input “±1” should also be valid, shouldn’t it?

@CommanderKeith
Awesome, I’m glad it’s working for you :smiley:
I’d be very interested to see how you are using it

Yeah, I didn’t write the parser thinking anyone would use + as non-op (to denote a number as positive). I’ve rewritten it now so the expressions you mentioned work now

Also fixed

Thanks for pointing this out

edit: whoops, not fixed. 0±1 actually crashes the parser with no error :slight_smile:

edit 2: All of the bugs above have been fixed