C# Evaluate BEDMAS Strings

C# Evaluate BEDMAS Strings

Post just about everything that isn't directly related to Spring here!

Moderator: Moderators

Post Reply
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

C# Evaluate BEDMAS Strings

Post by SinbadEV »

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.
User avatar
aegis
Posts: 2456
Joined: 11 Jul 2007, 17:47

Re: C# Evaluate BEDMAS Strings

Post by aegis »

SinbadEV wrote:only 255...probably end up just putting a case selection in the code instead...
/me notifies tdwtf

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
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Re: C# Evaluate BEDMAS Strings

Post by Tobi »

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.)
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

Re: C# Evaluate BEDMAS Strings

Post by SinbadEV »

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.
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

Re: C# Evaluate BEDMAS Strings

Post by SinbadEV »

Thanks for the suggestions... I decided on compiling and using an inline dynamic assembly.
User avatar
Caydr
Omnidouche
Posts: 7179
Joined: 16 Oct 2004, 19:40

Re: C# Evaluate BEDMAS Strings

Post by Caydr »

It's BEMDAS.
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

Re: C# Evaluate BEDMAS Strings

Post by SinbadEV »

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...
User avatar
Peet
Malcontent
Posts: 4384
Joined: 27 Feb 2006, 22:04

Re: C# Evaluate BEDMAS Strings

Post by Peet »

I was taught BEDMAS. Both BEDMAS and BEMDAS redirect to the same article on wikipedia. And it makes no difference.
User avatar
Caydr
Omnidouche
Posts: 7179
Joined: 16 Oct 2004, 19:40

Re: C# Evaluate BEDMAS Strings

Post by Caydr »

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.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: C# Evaluate BEDMAS Strings

Post by FLOZi »

I was taught BODMAS which was later changed to BIDMAS :(

(orders, indices for anyone wondering)
Post Reply

Return to “Off Topic Discussion”