pitermarx / NCalc-Edge

A clone of NCalc. Latest source. Published to Nuget.org. version 1.4.3
http://ncalc.codeplex.com/
MIT License
53 stars 35 forks source link

Input string was not in a correct format. #22

Closed mh884 closed 5 years ago

mh884 commented 5 years ago

I'm trying to ignore/replace empty string but not all the empty string throw exception Example: new Expression("20+''").Evaluate() it will threw an exception new Expression("0.5=''").Evaluate() it will threw an exception

'new Expression("20+''").Evaluate()' threw an exception of type 'System.FormatException' Data: {System.Collections.ListDictionaryInternal} HResult: -2146233033 HelpLink: null InnerException: null Message: "Input string was not in a correct format." Source: "mscorlib" StackTrace: " at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)\r\n at System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt)\r\n at NCalc.Numbers.ConvertIfString(Object s)\r\n at NCalc.Numbers.Add(Object a, Object b)\r\n at NCalc.Domain.EvaluationVisitor.Visit(BinaryExpression expression)\r\n at NCalc.Domain.BinaryExpression.Accept(LogicalExpressionVisitor visitor)\r\n at NCalc.Expression.Evaluate()" TargetSite: {Void StringToNumber(System.String, System.Globalization.NumberStyles, NumberBuffer ByRef, System.Globalization.NumberFormatInfo, Boolean)}

new Expression("0.5=''").Evaluate()' threw an exception of type 'System.FormatException' Data: {System.Collections.ListDictionaryInternal} HResult: -2146233033 HelpLink: null InnerException: null Message: "Input string was not in a correct format." Source: "mscorlib" StackTrace: " at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)\r\n at System.String.System.IConvertible.ToDouble(IFormatProvider provider)\r\n at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)\r\n at System.Convert.ChangeType(Object value, Type conversionType)\r\n at NCalc.Domain.EvaluationVisitor.CompareUsingMostPreciseType(Object a, Object b)\r\n at NCalc.Domain.EvaluationVisitor.Visit(BinaryExpression expression)\r\n at NCalc.Domain.BinaryExpression.Accept(LogicalExpressionVisitor visitor)\r\n at NCalc.Expression.Evaluate()" TargetSite: {Double ParseDouble(System.String, System.Globalization.NumberStyles, System.Globalization.NumberFormatInfo)}

pitermarx commented 5 years ago

I'm not sure what the issue is, "0.5=''" is not a valid expression...

mh884 commented 5 years ago

yes it's not a valid expression as it comparing two different types but if you convert both to string then it will be no longer invalid expression as comparing two value in string will not throw an exception

pitermarx commented 5 years ago

Not sure what you mean by converting both to a string. Are you suggesting that NCalc should convert 0.5 to a string?

mh884 commented 5 years ago

Yes, I would like to suggest to convert 0.5 to a string or to tread both sides like a string. compare two sides of type string is not different to compare both sides of the same type as you NCalc is doing now when it comes to final results the result it's 99% the same, By default, when == is applied to reference types, it evaluates to true when the two operands refer to the same object (i.e., they refer to the same memory location). A class may override this behavior by explicitly defining the == and != operators. For example, the string class defines the == operator to evaluate to true if the given strings are the same length and contain the same sequence of characters. Any idea why not? or i was thinking if the string is empty change it to the super-type zero

pitermarx commented 5 years ago

Sorry but I'm not comfortable with this change. I think that it would be a breaking change and as I said in the readme, I want that "This repo will never have breaking changes".