Forcing Integer Literals to Double

Jul 16, 2009 at 12:41 PM


I am using the following code in flee version to evaluate string expressions passed to a function:

                poExpContext = New ExpressionContext
                poExpOptions = poExpContext.Options

                poExpOptions.ResultType = GetType(Double)


                poExp = ExpressionFactory.CreateGeneric(Of Double)(expression, poExpContext)
                Evaluate = poExp.Evaluate

I noticed a strange behaviour in the calculations:

If I pass '8833 * 1016000 * 0.000023' it calculates 7687.86816

If I pass '1.0 * 8833 * 1016000 * 0.000023' then it correctly calculates 179486.56

The expressions are intended to be decimal/real calculations. Is there any way of forcing the engine to treat all numeric values as real?

Or is there a fix in the later releases for this issue?







Jul 18, 2009 at 1:16 AM

The product of the first two numbers is too large for an integer.  This causes the product to overflow and product the incorrect result.

You can use the context.Options.IntegersAsDoubles option to force the integer literals to be loaded as doubles.  This produces the correct result of 206409.544.  This option was put in in v0.9.24.0.

You can also set the context.Options.Checked option to true so that integer overflows will cause an exception instead of being silently truncated.

Jul 20, 2009 at 10:17 AM

Many thanks for the support Eugene.