Solved

VS2005 - C# - Math?

Posted on 2007-11-14
10
769 Views
Last Modified: 2011-10-03
I have a C# Windows app that performs calculations based on equation patterns.  Currently I'm leveraging MS SQL Server's built in abilities to perform the actual calculations.  Here are some of the equation patterns that are being sent to the database:

(A * (B - C)) + (D * (E - F))
(A * B) + (A * C)
(A * B) + (A * C) + (D * E)
(A * B) + (A * C) + (D * E) + (D * F)
(A * B) + (C * (D - E))
(A * B) + (C * D)
(A*B) - C
(A-B) * C
(CEILING((A - B)/C) * D)
(CEILING(A - B)/C) * D
(CEILING(A/B) * C) + (A * D)
(CEILING(A/B) * C) + (A * D) + (CEILING(E/F) * G)
(CEILING(A/B) * C) + (CEILING(A/D) * E)
(CEILING(A/B) * C) + (CEILING(D/E) * F)
(CEILING(A/B) * C) + (D * E)
A
A * (B - C)
A * B
A + (((B - C)/D) * E)
A + (B * (C - D))
A + (B * C)
CEILING((A - B)/C) * D
CEILING(A/B) * C

The values for the elements ('A', 'B', 'C' etc) are values determined by input from the user and/or static values stored in the database and all have a potential for four significant digits (I stated that poorly but you know what I mean right?).  

I'm wondering if there is an open source or near free .net library out there somewhere that can execute full equations like these without major modifications.

The app works great and may move to aspx soon so maybe it's best to keep using SQL Server's math abilities?  What do you folks think?
0
Comment
Question by:wpm0001
10 Comments
 
LVL 25

Expert Comment

by:imitchie
ID: 20286461
C# should be able to handle all those calculations natively, or am I missing something? can you elaborate?
0
 
LVL 22

Expert Comment

by:RedKelvin
ID: 20286501
I don't think this does everything you want, but still of interest
http://www.codeproject.com/dotnet/complex_math.asp
0
 
LVL 1

Author Comment

by:wpm0001
ID: 20286639
imitchie,

I would like to send an entire equation to "something" and have it evaluate the expression without needing to do anything else.  

For the pattern:
(A * B) + (A * C) + (D * E) + (D * F)

SQL Server Accepts:
(5.0000 * 6.0000) + (5.0000 * 0.0125) + (3.0000 * 4.3180) + (3.0000 * 7.0000)

And will return the appropriate value.  SQL Server will accept any valid mathematical statement and return the calculated value.  If C# can do this then I've missed it!  I really hope that I've missed it because it would be awesome!!!

BTW - these equation patterns (that is what I'm call'in them) are stored in a database and the pattern is not known until run-time.  The app picks out the elements that can be entered, etc.

I might not have answered your question.  I tend to be a little wordy sometimes...
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20286653
how about:

http://www.codeproject.com/useritems/Run-Time_Code_Generation1.asp

paste the entire calculation as c# code, compile and run it
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20286656
actually, this one is easier, and straight from the horse's mouth
http://support.microsoft.com/kb/304655
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 25

Expert Comment

by:imitchie
ID: 20286711
that was incomplete, you need this also
http://www.aspcode.net/ProcessStart-and-redirect-standard-output.aspx

so your compiled exe has

void main...
 writeline ... (string) (5.0000 * 6.0000) + (5.0000 * 0.0125) + (3.0000 * 4.3180) + (3.0000 * 7.0000) )

and your original app receives the result in

string sRes = oReader2.ReadToEnd();

and casts as double
0
 
LVL 1

Author Comment

by:wpm0001
ID: 20286748
RedKelvin

Holy Cow - way above my head!  Interesting though.  

Thanks!
0
 
LVL 1

Author Comment

by:wpm0001
ID: 20286847
imitchie?

If the values changed - then what?  

I must be missing something...  (A * B) + (A * C) + (D * E) + (D * F)

In a situation where A, B, C, D, E, and/or F can have different values based on the input from the user how do your posts apply?

You have me confused now.  
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 20286928
It's really pretty simple to do. Just convert it to Reverse Polish Notation and run it through a simple stack processor. Send in the values for the variables along with the equations.

Jim
0
 
LVL 25

Accepted Solution

by:
imitchie earned 250 total points
ID: 20287124
let's say you are passing a string to be evaluated by SQL Server, and getting the results via 'Select ' + MyEquation
you can create a template single-file C# console program to evaluate an equation, and write it to the console. it could have a simple equation like console.write( (string) (2+3+4) ).
you then take the whole file as a string x = ... C# console program.
then depending on the input, you update the equation, i.e.
  string myconsoleapp = "..... upto write( (string) ( Ceiling(" + A + " + " + B + ") ) .... rest of program"
then you can use the SharpCompiler to compile it, and use Process.Start to run it.  Read the console output back

you can use the same pattern for more than just equations.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now