So... I have need of a function that can take four parameters as,
public float (string func, float x, float y, float z) and return the result of the function with those parameters filled in... for example pass:
"1-z*(x+y-(2*x*y))", 0.5, 0.9, 0.2
and return the result of 0.9
I'm pretty sure I can manage it with a nice recursive function to break it down... but it seems like kinda a trivial thing to do... I was wondering if anyone has already done this or has run across it in programming and can give me a few hints... Google has been no help as putting "function" and "string" into a query doesn't yield the desired results... it sounds like a kind-of text-book-case that I should be able to easily find a code example for to snag.
In the end, there are only 255 valid functions so I'll probably end up just putting a case selection in the code instead... but I'd prefer a nice simple code block to use.
C# Evaluate BEDMAS Strings
Moderator: Moderators
Re: C# Evaluate BEDMAS Strings
/me notifies tdwtfSinbadEV wrote:only 255...probably end up just putting a case selection in the code instead...
one solution is apparently to invoke the C# compiler and make it execute the expression as code
you can also include the scripting control/jscript engine and call eval...
lexical analysis of simple expressions could even work
Re: C# Evaluate BEDMAS Strings
How are there only 255 valid functions?
If you give me a valid function I can add "+x" to the end and I have another valid function. So there are infinitely many valid functions.
Just invoke C# compiler or use some scripting engine, as aegis suggests.
And otherwise manually writing a recursive descent parser that parses and evaluates the function in a single pass isn't really hard either (wikipedia seems to have nearly complete sample code.)
If you give me a valid function I can add "+x" to the end and I have another valid function. So there are infinitely many valid functions.
Just invoke C# compiler or use some scripting engine, as aegis suggests.
And otherwise manually writing a recursive descent parser that parses and evaluates the function in a single pass isn't really hard either (wikipedia seems to have nearly complete sample code.)
Re: C# Evaluate BEDMAS Strings
it is for visualizing Fuzzy Cellular Automata, there are apparently only 255 unique variations where F(x,y,z), x, y, and z all between 0.0 and 1.0.
Re: C# Evaluate BEDMAS Strings
Thanks for the suggestions... I decided on compiling and using an inline dynamic assembly.
Re: C# Evaluate BEDMAS Strings
It's BEMDAS.
Re: C# Evaluate BEDMAS Strings
Google Says:
Results 1 - 10 of about 61,500 for BEDMAS. (0.24 seconds)
Results 1 - 10 of about 152,000 for BEMDAS. (0.35 seconds)
Looks like Caydr's right... wacky...
Results 1 - 10 of about 61,500 for BEDMAS. (0.24 seconds)
Results 1 - 10 of about 152,000 for BEMDAS. (0.35 seconds)
Looks like Caydr's right... wacky...
Re: C# Evaluate BEDMAS Strings
I was taught BEDMAS. Both BEDMAS and BEMDAS redirect to the same article on wikipedia. And it makes no difference.
Re: C# Evaluate BEDMAS Strings
Oh SURE the internet MUST be right.
It's really BEDMAS, but BEMDAS is easier since it lets you multiply BEFORE you divide. And as everyone knows, dividing is hard.
It's really BEDMAS, but BEMDAS is easier since it lets you multiply BEFORE you divide. And as everyone knows, dividing is hard.
Re: C# Evaluate BEDMAS Strings
I was taught BODMAS which was later changed to BIDMAS
(orders, indices for anyone wondering)

(orders, indices for anyone wondering)