简体   繁体   中英

mathematical printf style calculation

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++?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM