Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continued Challenge : Serializing Math for Projecting to C# and Javascript

I have asked a few questions related to this, but from different angles. I am trying yet again another approach.

The design in question relates to a system in which the client is able to create various entities in the system, and as needed build mathematical formulas to tether those entities together in some meaningful way. This is an important distinction because these entities do not exist at design time.

A client might then create a few entities. a, b, c, n, r and x. So then, a client might say "I need a to equal [ b + c ]( n * r ) + [ x ]".

a, b, c, n, r and x all make sense to the client. They have names and values in the context of what the client is viewing. But to the underlying system, they did not exist at design time - so this formula does not exist.

So then, is there any way to take this equation and serialize it in such a way that it can be deserialized and executed against a given list of variables? I am using C#, ASP.NET MVC, RavenDB, and JavaScript.

The equations will be defined by the administrator of the software, and are relative to each individual user. They need to be stored in the database so that they can be executed server side, and also client side.

like image 924
Ciel Avatar asked May 22 '26 19:05

Ciel


1 Answers

It sounds like you are looking for a formula parser for mathematical expressions.

On server side, you can use NCalc, and although you can use JavaScripts eval() function on client side, in order to have an identical interpretation of input strings, you should consider using a webservice called from Javascript (or jquery) that simply parses a client formula on the server.

BTW, NCalc can even parse customised expressions.

like image 62
Olaf Avatar answered May 24 '26 08:05

Olaf