Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mathematical printf style calculation

Tags:

c++

math

I'm looking for an flexible but also considerably fast way to do simple value conversion and calculations at the basis of descriptive calculator strings.

For example something like this:

double r = 1.0;
double d = mathf( "sin(%1)+2*%2", r, M_PI );
double e = mathf( "%1 / 180.0 * %2", r, M_PI );

The important think is the mathematical operations can be evaluated at runtime and loaded from config file. I was even considering some sort of scripting language integration but it seems that doesn't come in sleek and fast?

Any ideas if something like mathf exists for C++?

like image 240
Hhut Avatar asked Apr 22 '13 12:04

Hhut


1 Answers

Try searching around a little more. This is a pretty common thing. It's parsing, and every compiler does it. Makes this a bit like parsception.

Solve equation from string to result in C

Evaluate a simple string mathmatical expression

Convert string to mathematical evaluation

etc etc.

There's two ways to go about it, one is write your own, the second is find a library which seems like what you're looking for. I don't know of anything like that in the C++ standard libs, in ruby and a bunch of other languages for sure, you can just eval the string, but in C++ you're probably going to have to borrow a library from the web or something. Try that last link, it looked promising for that.

like image 98
NathanTempelman Avatar answered Oct 31 '22 23:10

NathanTempelman