Posted by Paul Welter on Saturday, May 05 2007 at 4:38 PM

I'd like to announce the release of a little project I've been working on.  I call it Calculator.NET.  I started this project for a couple reasons.  First, I was annoyed that Windows Vista doesn't come with a better calculator.  Windows XP has Power Calculator, but that doesn't work on Vista.  Next, I was reading a blog about DynCalc by Bart De Smet on how to do mathematical calculations. That gave me the starting point on how to create Calculator.NET.

As part of the project, I created a MathExpressions library that does the bulk of work.  The library supports math expressions, functions unit conversion and variables. Below are some examples of using the library directly.

MathEvaluator eval = new MathEvaluator();
//basic math
double result = eval.Evaluate("(2 + 1) * (1 + 2)");
//calling a function
result = eval.Evaluate("sqrt(4)");
//evaluate trigonometric 
result = eval.Evaluate("cos(pi * 45 / 180.0)");
//convert inches to feet
result = eval.Evaluate("12 [in->ft]");
//use variable
result = eval.Evaluate("answer * 10");
//add variable
eval.Variables.Add("x", 10);            
result = eval.Evaluate("x * 10");

Calculator that evaluates math expressions.
Calculator.NET

Calculator.NET Features

  • Evaluate math expressions including grouping
  • Support trigonometry and other function
  • Common unit conversion of the following types
    • Length
    • Mass
    • Speed
    • Temperature
    • Time
    • Volume
  • Variable support including last answer

Download

File: Calculator.msi 
Size: 1,357,824 kB

File: Calculator.v1.0.0.12.zip 
Size: 614,015 kB

10 Comments Posted in: Projects / Calculator.NET    Tagged as: ,

Similar Posts

  1. The NetSpell project
  2. Google Toolbar Spell Check API
  3. SvnBackup - Backup Tool For Subversion Repositories

Comments

Show/Hide Trackbacks
#1 Comment by Carlos Peix on 1/20/2009 at 9:30 AM

Hi Paul,

Have you decided the license for the MathExpressions component?

Can I use it in a non open source project?

Thanks

#2 Comment by Paul Welter on 1/20/2009 at 10:07 AM
Gravatar for Paul Welter

Hi,

The license is BSD, free to use however you need. Hope you find the code useful.

~ Paul

#3 Comment by Carlos Peix on 1/20/2009 at 10:47 AM

Great Paul,

I'll recommend adding a small text file with a mention for this site and your name.

Thanks

Carlos

#4 Comment by John Lamber on 2/22/2009 at 11:34 AM
Gravatar for John Lamber

Great tool but the speed conversion doesn't work.

For example if I entered this

2[kph->m/s]

and I get this:

Invalid convertion expression: [kph->m

However, if I entered this and it works:

121[knot->kph]

224.092

I think the problem is slash in the m/s is not accepted format.

#5 Comment by Diego Jancic on 3/14/2009 at 11:36 PM
Gravatar for Diego Jancic

Great project!

But I found a bug:

[Test]

public void EvaluateFunctionOverFunction()

{

double expected = Math.Sin(5) / Math.Sin(2);

double result = eval.Evaluate("sin(5) / sin(2)");

Assert.AreEqual(expected, result);

}

expected: <-1,054577134360428>

but was: <-0,70642500869217528>

Same happens with the parsing of all functions.

I'm trying to fix it right now, maybe I'll send you a patch (if I got one).

#6 Comment by Diego Jancic on 3/15/2009 at 1:02 AM
Gravatar for Diego Jancic

Hi,

I found a bug in the Calculator project, here's the test and the fix:

MathEvaluatorTests.cs:

[Test]

public void EvaluateFunctionOverFunction()

{

double expected = Math.Sin(5) / Math.Sin(2);

double result = eval.Evaluate("sin(5) / sin(2)");

Assert.AreEqual(expected, result);

result = eval.Evaluate("(sin(5)) / (sin(2))");

Assert.AreEqual(expected, result);

}

MathEvaluator.cs:

private bool TryEndGroup(char c)

{

if (c != ')')

{

return false;

}

bool ok = false;

while (_symbolStack.Count > 0)

{

string p = _symbolStack.Pop();

if (p == "(")

{

ok = true;

// Add the expression to the stack if it is a function

if (_symbolStack.Count > 0)

{

string nextSymbol = _symbolStack.Peek();

if (char.IsLetter(nextSymbol[0]))

{

IExpression exp = GetExpressionFromSymbol(nextSymbol);

_expressionQueue.Enqueue(exp);

// Remove the peeked element

_symbolStack.Pop();

}

}

break;

}

IExpression e = GetExpressionFromSymbol(p);

_expressionQueue.Enqueue(e);

}

if (!ok)

{

throw new ParseException(Resources.UnbalancedParentheses);

}

return true;

}

#7 Comment by Jake on 7/28/2009 at 4:43 PM

Cool program, but 1+2^3 doesn't work as expected because ^ isn't given highest precedence.

#8 Comment by Bug on 10/07/2009 at 6:54 AM
Gravatar for Bug

there is another bug with - values

5 + (-1) doesn't work

the same error with 5 * (-1)

workaround -> work with variables

it seems to be an parse with - values.

#9 Comment by MajorVictory on 11/09/2009 at 11:57 PM
Gravatar for MajorVictory

In regards to "5 + (-1)" not working, another workaround is subtracting by 0.

5 + (0-1)

Same result, although a patch would be better.

#10 Comment by Leif Klasson on 1/22/2010 at 7:44 AM
Gravatar for Leif Klasson

Hi, and thanks for a great looking idea, keep up the good work.

In Sweden among others we use , as decimal delimiter, this doesn't work.

Possible to fix?

Best regards

Leif

Leave a Comment