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 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
Similar Posts
- The NetSpell project
- Google Toolbar Spell Check API
- SvnBackup - Backup Tool For Subversion Repositories
Hi Paul,
Have you decided the license for the MathExpressions component?
Can I use it in a non open source project?
Thanks
Hi,
The license is BSD, free to use however you need. Hope you find the code useful.
~ Paul
Great Paul,
I'll recommend adding a small text file with a mention for this site and your name.
Thanks
Carlos
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.
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).
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;
}
Cool program, but 1+2^3 doesn't work as expected because ^ isn't given highest precedence.
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.
In regards to "5 + (-1)" not working, another workaround is subtracting by 0.
5 + (0-1)
Same result, although a patch would be better.
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