Home » C# Programming

Easy math function parser C#

Hey! Today i came up with idea to make math solving program for my exam using Windows Mobile. And the first thing i thoght of - is there any way to parse c# math functions? For example i write Math.pow(2,2) and as reslult i get "4".

1 Answer Found



Math.Ceiling(value) >= value

Math.Floor(value) <= value

You might even get Math.Round, e.g. (value - Math.Round(value)) >= 0.5 && ... <= 0.5

(value >= 0 && Math.Truncate(value) <= value) || (value < 0 && Math.Truncate(value) >= value)

several of these are marked [Pure], but I'm surprised they don't have more contracts. Or are the contracts proposed not quite right in the face of the oddities of floating point arithmetic?




I've beaten my head against the wall with something that should be simple.

What I'm looking for is a percentage between 2 non-zero rooted numbers. Meaning, if I had a range of 50-100, 50% between them would be 75. I know this but how do I calculate this for say 30% or 60%?

Is there a match function that handles this or is it just raw math that I'm missing?

Any help appreciated,



Hello :) I was decompiling this open source program to see how it worked. Then I found some functions I would like to use, but I need to make them work first! Here are the functions that are giving me trouble:


PublicFunction CalcCRCITT(ByVal p AsByte()) As UInt16
   Dim num As UInt32 = &HFFFF
   Dim i AsIntegerFor i = 0 To p.Length - 1

'This line is giving me troubleDim num3 As UInt32 = DirectCast((p(i) << 8), UInt32)

    Dim j AsIntegerFor j = 0 To 8 - 1
     If (((num Xor num3) And &H8000) <> 0) Then
      num = ((num << 1) Xor &H1021)
      num = (num << 1)
     num3 = (num3 << 1)
    Next j
   Next i
  EndFunctionPublicFunction crctable(ByVal p AsByte()) As UInt64
   Dim crc As UInt64 = Me.crcinit_nondirect
   If (Me.refin <> 0) Then
    crc = Me.reflect(crc, Me.order)
   EndIfIf (Me.refin = 0) ThenDim i AsIntegerFor i = 0 To p.Length - 1

'this line too
     crc = (((crc << 8) Or p(i)) XorMe.crctab(CInt(DirectCast(((crc >> (Me.order - 8)) AndCULng(&HFF)), IntPtr))))

    Next i
   ElseDim j AsIntegerFor j = 0 To p.Length - 1

'and this
     crc = CULng(((CInt((crc >> 8)) Or (p(j) << (Me.order - 8))) XorCInt(Me.crctab(CInt(DirectCast((crc AndCULng(&HFF)), IntPtr))))))

Next j
   EndIfIf (Me.refin = 0) ThenDim k AsIntegerFor k = 0 To (Me.order / 8) - 1

     crc = ((crc << 8) XorMe.crctab(CInt(DirectCast(((crc >> (Me.order - 8)) AndCULng(&HFF)), IntPtr))))

    Next k
   ElseDim m AsIntegerFor m = 0 To (Me.order / 8) - 1

'and this

     crc = ((crc >> 8) XorMe.crctab(CInt(DirectCast((crc AndCULng(&HFF)), IntPtr))))

    Next m
   EndIfIf ((Me.refout XorMe.refin) <> 0) Then
    crc = Me.reflect(crc, Me.order)
   crc = (crc XorMe.crcxor)
   Return (crc AndMe.crcmask)
  EndFunctionPublicFunction crctablefast(ByVal p AsByte()) As UInt64
   Dim crc As UInt64 = Me.crcinit_direct
   If (Me.refin <> 0) Then
    crc = Me.reflect(crc, Me.order)
   EndIfIf (Me.refin = 0) ThenDim i AsIntegerFor i = 0 To p.Length - 1

'and this
     crc = ((crc << 8) XorMe.crctab(CInt(DirectCast((((crc >> (Me.order - 8)) AndCULng(&HFF)) Xor p(i)), IntPtr))))

    Next i
   ElseDim j AsIntegerFor j = 0 To p.Length - 1

'and this
     crc = ((crc >> 8) XorMe.crctab(CInt(DirectCast(((crc AndCULng(&HFF)) Xor p(j)), IntPtr))))

    Next j
   EndIfIf ((Me.refout XorMe.refin) <> 0) Then
    crc = Me.reflect(crc, Me.order)
   crc = (crc XorMe.crcxor)
   Return (crc AndMe.crcmask)


If you need the whole class here it is:

Download Class


when i finding the value to round some decimal or double number below problem i faced

system.math.round(12.50)        ans: 12

system.math.round(12.51)        ans: 13

the same problem also in floor function .........these problems in framework 3.5 and framework 4.0 also


I'm trying to calculate a customer tax.

Select 100 * Round(25296 /100 ,1) --returns 25200

 however Math.Round in Visual Basic, return 25300.

 Result = 100 * Math.Round(25296 / 100) ' returns 25300

I really nead any appropriate function to fix it.

Plese help soon.


Hi everyone,

I’ve got a problem with function ‘pow’ that is in math.h .
In the following examples function ‘pow’ gives wrong result on some conditions. Take a look.

a=pow(-8, ( (double)1/3) ); // actually cube root of -8

The result ‘a’ is : -1.#IND000000000000    double, instead of 2.  Debugger shows.

Actually in my project everywhere I use complex numbers (complex <double>). 

complex <double> tcp1(-8, 0 ),tcp2((double)1/(double)3, 0), tcp3(0, 0);


Look at debugger locals variables - the result is:
tcp3 1.0000000000000000+i*1.7320508075688767  std::complex<double>

Ups … error  :(
Could you say, what is wrong with this basic things.

Thank you in advance.




Whether Data(day,year,hour,minute,month,second) and Math functions(double round, decimal round, double floor, decimal floor, double ceiling, decimal ceiling) are supported in WCF data service using ODATA.


When I copy & paste this in

"Sum.Value = 0"

the s has a red rectangle under it add an error message "there were build errors would you like to run..." appear when trying to run.

the tutorial shows sum in blue but mine is black - retyping does not show "sum" in the preview list - the error bow is showing "sum is not declared"

What have I missed?

Many Thanks



I'm building a "Class Library". The complier says some fundamental namespaces, like "System.Math" and "System.Windows" don't exist. I need to use these. Is there anyway i can get access to them?



Greg S.


I am new to c#

when i try to do this

Math.POW(10, value.count)

it shows

'System.Math' does not contain a definition for 'POW' 

what is this?? using system; will not be sufficient?


I've been loving C# since I started a couple weeks ago, it's been pretty darn easy to learn, most stuff is intuitive and simple... until now.

In the process of expanding my horizons with C#, it was inevitable that I eventually try to code the good 'ol Celsius to Fahrenheit converter... Well it's not as easy as I would have thought...

You just have to take the celcius number, multiply by ( 9/5 ) or 1.8, and add 32... Well that is turning out to be more trouble than I would ever imagine...

txtF.Text = (double.Parse(txtC.Text) * (9/5) + 32.0).ToString();

Where's the problem?? When I run the program it displays 69, instead of 98.6. I tried with int, double, float, and tried '32' and '32.0'... I tried flipping the numbers around, and in and out of parenthesis... Nothing. Same result every time.

It almost would seem to me that its making up some random number in place of one of them....


Thank you for any assistance!


Hi,I need parsing a math formula in sql server

like this :

@formula = (A * B) + 10 /(c * 40)

the result :






and then with case I will replace the variables. but first I need to seprate them.


I would like to make a graph drawer but I have no idea where to start from, so if anyone could recommend a method or seen a tutorial or found a project, I would really like to see. I would prefere to be in vb language.



I was wondering if someone could explain the logic behind the MidpointRounding enumeration for the Math.Round function.  The two enumerations are AwayFromZero and ToEven where, based on the given precision, the former will return the nearest value away from zero toward positive or negative infinity (based on the value's sign) and and letter will provide the nearest even number.

To me, this makes no sense because the two enumerations are not mutually exclusive.  Even in the MS help topic...


... it provides an example of 3.5 where both enumerations return the same value (4).  WTF!  What if I need to control the way 3.5 rounds -- either up or down -- to the nearest integer?  I guess I'm out of luck.

Anyhow, can someone explain why these two options do not appear to be mutually exclusive, why these are the only two options (rather than RoundUp and RoundDown) and if there is an alternative to this that can do what I need.  A coworker is working on a project that utilizes an Adobe COM piece that utilizes Java and Java apparently is choosing the opposite rounding than that of .NET.




i have textBox and i will type 

EX :

(13 * X ) /6

and i want to Calculate this function 


How in T-SQL can I calculate and print the difference in minutes in the format nn:ss between 2 datetime values?

I want to use this in a message similar to: This data record was updated [nn:ss] minutes ago by user [username].

Thank you.



I am trying to pull all of the associated data from a SharePoint list row to an InfoPath form repeating table.  I would like to select 1 unique value from the row and have the other fields auto-populate with the associated data from that row. 


SharePoint Columns and also names of InfoPath fields 1, 2, 3, 4, 5, 6, 7.

I have a dropdown that is connected to column 1 and I would like to select 1 and have it populate the rest of the fields from that row.  I am currently doing this with information and C# code from:  http://www.bizsupportonline.net/infopath2007/sharepoint-list-retrieve-value-same-row-repeating-table-infopath.htm I have also added conditional formatting to the repeating table that says if Column 1 in the repeating table doesn’t = the dropdown hide all other rows in the table so that only the row I asked for in the dropdown is visible.

Problem: I would like to be able to do math with this data I have selected/visible, currently it does not allow me to.  I would like to do a calculation like this; if 5= NO, set field 4 and field 7 to the product of 2*3. If 5=YES, set field 7 to 2*3=4+6. I may need to explain that better.

I think this http://msdn.microsoft.com/en-us/library/bb509311(office.12).aspx is trying to answer my question but I am not sure because most of the language is over my head as I have no programming experience. I just got lucky finding, copying, and pasting the code to get where I am.

I hope this makes sense to someone...



I am using the the information supplied by NetResearchMan in this thread: http://social.msdn.microsoft.com/Forums/en-US/smallbasic/thread/2bfaa80d-91db-4df7-aa2f-413e1e6a9f30
Here is the program I am still testing fluid 3D simple wire-frame animation XFH167 (On-line listing: http://smallbasic.com/program/?XFH167).

When you run the program, you see a box in the middle of the maze view frame (on the right).
Think of this box, or square, as a wall.
The wall moves correctly when strafing left/right or moving forward/back.
But when you turn (using the A to turn left and the D to turn right) it kind-of starts off moving the right way, but when you get too far, the wall gets stretched out on the X axis.
Also, when you try to turn the oposite direction, the wall does not revert back along the progression it made to get to it's current state - it, at first, keeps going and then kind-of tries to go back.

I have played with the CamerAngle amount to increase/decrease.
...beyond that, I am not sure what to do.

I was thinking that maybe the xtmp and ztmp was only supposed to be used temporarily and the increased/decreased camera angle would be the only changed variable that would move the box properly, but then ztmp would have no affect on anything, so you have to have (from my program) left_X = x1tmp and z1 = z1tmp.

You might find my use of variables a little strange, in reference to the square's X/Y coordinates.
I figure that since the camera will not be going up and down the Y axis (for now anyway) the top-left corner's X is always going to be equal to the bottom-left corner's X and the same for the top and bottom right's X.
...but the Y's will be different, so I had to have 4 seperate Y's.

I have looked up many different 3D drawing instructions, specifically for Basic (but even more specific for Qbasic) and the instructions seem to get mired in the details of the math.

Admitedly math is my weakness.
I would like to just have the math/code and tell me what variables to change to make it do what.

Once I have the math working right, I should be able to rotate my square so that is looks like part of the wall on either the left or right side of the hallway.


Hello I'm trying to do a function that returns a value to be displayed in a list and I can get one value to show but when my next one is asked and is inputed the next one just shows zero.

Heres my code.


Public Class Form1


    Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click

        Dim newbalance As Double

        Dim fixedAmount As Double

        Dim previous_balance As Double

        previous_balance = newbalance

        newbalance = depositForAccount(previous_balance, fixedAmount)



        lstOutput.Items.Add("Month 1 " & fixedAmount)


        depositForAccount(previous_balance, fixedAmount)

        lstOutput.Items.Add("Month 2 " & newbalance)

    End Sub


    Sub amountFixed(ByRef fixedAmount As Double)

        fixedAmount = CDbl(InputBox("Input fixed amount"))

    End Sub


    Function depositForAccount(ByVal previous_balance As Double, ByVal fixedAmount As Double)

        Return 1.005 * previous_balance + fixedAmount

    End Function

End Class


Is it possible to have a number field accept formula input?

Like =88*88

Instead of typing 7744


<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure