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.
Maths error?
- Henry MultiСharts
- Posts: 9165
- Joined: 25 Aug 2011
- Has thanked: 1264 times
- Been thanked: 2957 times
Re: Maths error?
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.
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.
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re: Maths error?
True, it also doesn't trigger an floating point error in MC7.4:Math.sqrt(-0) does not cause Floating point error in our environment with MC 8.0 beta 1.
Code: Select all
Variables:
myValue(0);
if LastBarOnChart_s then begin
Print("MyValue: ", NumToStr(myValue, 10));
Print("MyValue SquareRoot: ", SquareRoot(-myValue));
end;
Code: Select all
MyValue: 0.0000000000
MyValue SquareRoot: 0.00
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;
Code: Select all
MyValue: 0.000000000000010
If this is the case here with your problem, a simple 'if (myValue <> 0) then go on..' should be sufficient to solve it.
Re: Maths error?
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.
- TJ
- Posts: 7740
- Joined: 29 Aug 2006
- Location: Global Citizen
- Has thanked: 1033 times
- Been thanked: 2221 times
Re: Maths error?
see post #6For 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.
viewtopic.php?f=16&t=6929
Re: Maths error?
Yes it is easy to work around, thanks Henry.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.
Re: Maths error?
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!
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!