Maths error?

Questions about MultiCharts and user contributed studies.
Nick
Posts: 496
Joined: 04 Aug 2006
Has thanked: 4 times
Been thanked: 24 times

Maths error?

Postby Nick » 16 Feb 2012

Hi,

I have the following line in a function.

s = s + SumWeightsOld * Square(VWAPOld) + w * Square(p) - SumWeights * Square(VWAP);

where s SumWeightsOld & VWAP old are zero on the first pass. The expression is returning -0 (-zero).

This causes a floating point error in a subsequent Sqrt function.

I believe that - zero is a legitimate entity in most computing implementations but in that case Sqrt (-0) should return zero rather than a floating point error?

Funnily enough it has always worked fine and returned positive zero when it should until I applied it to currency futures (p=1.3 instead of p in the 100's or 1000's).

Not a biggy but FP errors are a bugger to track down sometimes. Whilst a run time debugger is probably too much to ask for some rudimentary run time info when a script crashes would be great. Maybe just some sort of dump of variables would be a good start seeing as most crashes are variables going beyond bounds.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Maths error?

Postby Henry MultiСharts » 17 Feb 2012

Hello Nick,

Math.sqrt(-0) does not cause Floating point error in our environment with MC 8.0 beta 1.
You can add a condition to check for the negative value and avoid using it.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Maths error?

Postby JoshM » 18 Feb 2012

Math.sqrt(-0) does not cause Floating point error in our environment with MC 8.0 beta 1.
True, it also doesn't trigger an floating point error in MC7.4:

Code: Select all

Variables:
myValue(0);

if LastBarOnChart_s then begin

Print("MyValue: ", NumToStr(myValue, 10));
Print("MyValue SquareRoot: ", SquareRoot(-myValue));

end;
gives..

Code: Select all

MyValue: 0.0000000000
MyValue SquareRoot: 0.00
However Nick, have you considered that your zero looks like a zero but isn't a real zero?

For example:

Code: Select all

Variables:
myValue(0.00000000000001);

if LastBarOnChart_s then begin

Print("MyValue: ", NumToStr(myValue, 15));
Print("MyValue SquareRoot: ", SquareRoot(-myValue));

end;
gives this..

Code: Select all

MyValue: 0.000000000000010
And then it triggers a floating point exception.

If this is the case here with your problem, a simple 'if (myValue <> 0) then go on..' should be sufficient to solve it.

Xyzzy
Posts: 162
Joined: 19 Mar 2011
Has thanked: 43 times
Been thanked: 79 times

Re: Maths error?

Postby Xyzzy » 18 Feb 2012

For the sake of asking, does MultiCharts store numeric variables internally as floating point numbers, or does it store them as precise decimal numbers? I know there are tradeoffs with each -- the potential of imprecision and rounding errors with the first, and slower performance/increased memory usage with the second.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Maths error?

Postby TJ » 19 Feb 2012

For the sake of asking, does MultiCharts store numeric variables internally as floating point numbers, or does it store them as precise decimal numbers? I know there are tradeoffs with each -- the potential of imprecision and rounding errors with the first, and slower performance/increased memory usage with the second.
see post #6
viewtopic.php?f=16&t=6929

Nick
Posts: 496
Joined: 04 Aug 2006
Has thanked: 4 times
Been thanked: 24 times

Re: Maths error?

Postby Nick » 21 Feb 2012

Hello Nick,

Math.sqrt(-0) does not cause Floating point error in our environment with MC 8.0 beta 1.
You can add a condition to check for the negative value and avoid using it.
Yes it is easy to work around, thanks Henry.

Nick
Posts: 496
Joined: 04 Aug 2006
Has thanked: 4 times
Been thanked: 24 times

Re: Maths error?

Postby Nick » 21 Feb 2012

Thanks Josh (and TJ).

It seems to be an issue with negative rather than floating point storage absvalue(fpnumber) fixes it rather than if fpnumber <>0.

Thanks though it is a good thing to be aware of!


Return to “MultiCharts”