0

Possible Duplicate:
using eval in Java
Is there a java equivalent of the python eval function?

How can I evaluate complicated mathematical expressions (with trigonometric functions) in Java, given as a String such as the following (say given angles are in degrees):

3 + 8 - ( 8 + sin(30) + 5 - 3 * 2 + (8 - 5 * 6) ) * cosec(45)

Please suggest any production quality third party libraries if available, or suggest suitable algorithm / data structure if I have to do the boilerplate coding myself.

Community
  • 1
  • 1
siva636
  • 16,109
  • 23
  • 97
  • 135
  • JEP: http://www.singularsys.com/jep/ (There should be an older free version available somewhere too, but the licence is cheap enough if it's an important feature.) – millimoose Jan 03 '13 at 16:02
  • @Robert Harvey The threads mentioned as duplicates are not dealing trigonometric functions. – siva636 Jan 03 '13 at 16:10
  • For that, you are going to need an industrial-strength library, like the one @millimoose suggests. – Robert Harvey Jan 03 '13 at 16:13
  • @Robert Harvey Well, then why did you close my thread? It is unique thread and there are things to discuss! (and there are people to answer) – siva636 Jan 03 '13 at 16:17
  • This isn't a discussion forum, or a substitute for Google. Do some of your own research, and come back here if you get stuck. – Robert Harvey Jan 03 '13 at 16:18
  • @Robert Harvey You closed my thread as duplicate, after finding not duplicate, you are saying something else!! – siva636 Jan 03 '13 at 16:23
  • 1
    I can change the close reason, if you like. But nobody here is going to hand you a complete solution. The linked posts provide a good starting point if you decide to code a solution yourself. – Robert Harvey Jan 03 '13 at 16:23
  • @Robert Harvey Thank you for accepting the reason you closed my thread is wrong. (and please mention the correct reason) – siva636 Jan 03 '13 at 16:30
  • @DUKE The answers on the duplicates a) point to third-party libraries as well (http://stackoverflow.com/a/2605050/41655), and b) the ones that suggest an embedded scripting language are also a valid approach even if you need more than standard arithmetic (Rhino will have access to `Math.sin`, and you can declare new toplevel functions in the JS engine if you want to avoid having to write that prefix.) This is enough to justify "duplicate" as a close reason, insofar as you haven't really investigated at whether the duplicates satisfy your requirements. – millimoose Jan 03 '13 at 16:50
  • 1
    @DUKE, see: https://gist.github.com/4446583 – Bart Kiers Jan 03 '13 at 20:06
  • @Bart Kiers **THANKS VERY MUCH!**. This is exactly what I have been looking for. **Thanks again!** – siva636 Jan 04 '13 at 16:40

0 Answers0