loresoft / Calculator

Calculator.NET - Calculator that evaluates math expressions
Apache License 2.0
97 stars 48 forks source link

Shouldn't this work? #4

Open gap777 opened 8 years ago

gap777 commented 8 years ago

Wrapping an expression in parenthesis seems to cause what otherwise is valid to be invalid:

(3 + Pow(2, 3))

In MathEvaluator.TryComma(), _nestedFunctionDepth (which is 1) < _nestedGroupDepth (which is 2).

What's the purpose of that particular assertion/check? What is it trying to guard against?

Can you think of a code change to support this expression?

pwelter34 commented 8 years ago

3+(2^3)

gap777 commented 8 years ago

Paul, I'm trying to communicate that the library fails to parse things when the superfluous enclosing parenthesis around the whole expression are included.

pwelter34 commented 8 years ago

that code is making a poor assumption that a function would only take 1 param. I've been meaning to fix that, want to support things like round(123.456, 2)

gap777 commented 8 years ago

I just added the "round" function on my fork.

It mostly works fine, except for the case I've raised to your attention.

On Fri, Jul 1, 2016 at 10:50 AM, Paul Welter notifications@github.com wrote:

that code is making a poor assumption that a function would only take 1 param. I've been meaning to fix that, want to support things like round(123.456, 2)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/loresoft/Calculator/issues/4#issuecomment-229966953, or mute the thread https://github.com/notifications/unsubscribe/AGMdeuktd8n4Ni3Le4MHBcu0ZD8OSnxVks5qRSkkgaJpZM4JCmBG .

clumsybiker commented 8 years ago

Round fails at the moment because the "TwoArgumentFunction" section of code uses reflection to find methods on the Math class with signatures that take (Double, Double).

Math.Round takes (Double, Int) causing the reflection: typeof(Math).GetMethod(....... to return null.

I couldn't come up with anything more elegant than if (_function == "Round") then changing the desiredMethodSignatureArgs from Double,Double to Double,Int. This fixes the reflection to find the Math.Round function but then you still have two doubles to pass to it, so it doesn't work ... yet!