Math problem: double * double = -0

To add comments, please log in or register
s
11
s  

Hello everyone, thats my first post at this board and any help would be appreciated.

MQL often tried to drive me insane (most times my fault), but THIS one is just ridiculous:

20:47:32 2011.10.20 03:15 Marker EURUSD,M15: Stab1Differenz * Stab2Differenz < 0 0.0005 -0.0012 -0

Code looks like this (please note the comments in CAPITAL letters, those are NOT present in the original code):

void Aussenstaebeerkennen()
{ if( iTime(S,0,0) != lbt )
{
Stab1Differenz = NormalizeDouble(iClose(S,0,2) - iOpen(S,0,2),16); // 'S = SYMBOL()' DURING INITIALIZATION
Stab2Differenz = NormalizeDouble(iClose(S,0,1) - iOpen(S,0,1),16); // 16: FIRST THAT WAS 4, DIDN'T WORK EITHER
UeberschussH = NormalizeDouble(iHigh(S,0,1) - iHigh(S,0,2),16);
UeberschussT = NormalizeDouble(iLow(S,0,1) - iLow(S,0,2),16);

if( Stab1Differenz * Stab2Differenz < 0 )
{ Print(" Stab1Differenz * Stab2Differenz < 0 ",Stab1Differenz, " ",Stab2Differenz, " ", NormalizeDouble(Stab1Differenz * Stab2Differenz,40) );

// THIS PRODUCES THE OUTPUT SHOWN ABOVE. I ALSO TRIED NormalizDouble(...,16) AND WITH NO NormalizeDouble()

// THE REST ISN'T VERY IMPORTANT
if( Stab1Differenz>0 )
{ //Print("Stab1Differenz>0");
if( UeberschussH>=uenoe )
{ //Print("UeberschussH>=uenoe",Stab3Tief,"<=",Stab2Tief," ?");
Umkehrstabshort = true;}}
else
{ //Print("else");
if( UeberschussT<=(uenoe*(-1)) )
{ //Print("UeberschussT<=(uenoe*(-1))",Stab3Hoch,">=",Stab2Hoch," ?");
Umkehrstablong = true;}}
}
}
}

Hope the language (its german) wont hurt...

...thx for helping!

William Roeder
22318
William Roeder  
  1. Don't use NormalizeDouble EVER. It's unnecessary
  2. If you want to print more than 4 digits, use DoubleToSTR(p, x)
  3. A double is only accurate to 15 significant digits
dabbler
1091
dabbler  

Ok, this is just a question of reading the help files for each function a bit more carefully :-)

Here is a little script to test the problem more easily ...

int start(){
   
   double a = 0.0005;
   double b = -0.0012;
   
   double c= a * b;
   
   Print( DoubleToStr(c,8) );
   Print( NormalizeDouble( c, 8) );
   Comment( DoubleToStr(c,8) + "\n" + NormalizeDouble( c, 8) );   
   
   return(0);
}

Note

https://docs.mql4.com/convert/normalizedouble

You are only allowed to use 0-8 for this function parameter not 40!


Note

https://docs.mql4.com/common/print

Even though you used NormalizeDouble, the result is still a double and still only prints to 4 digits using the Print function.

s
11
s  

Alright, thank you guys for your replies.

Actually the code worked well but I thought it wouldnt because it said x!=0, y!=0, x*y=-0 . BUT terminal knew the result wasn't -0, as you said dabbler, it just said so because Print() shows doubles only to the 4th digit instead of sth like -0.0000004. Isn't that odd? What's the problem with showing me the exact Double?? I used DoubleToStr(...,10) then and that cleared things up:

I must admit I had a hard time understanding your replies, because that looked like some math problem to me ... btw, I actually normally don't use NormalizeDouble(), it was just for debuggin this time.

So what have we learned out of this?

NEVER trust MQL.

Simon Gniadkowski
17271
Simon Gniadkowski  
DoubleToStr only goes up to 8 digits . . . ;-) there is a function that will do more, it was mentioned in a post during the last week or so.
William Roeder
22318
William Roeder  
RaptorUK:
DoubleToStr only goes up to 8 digits . . . ;-) there is a function that will do more, it was mentioned in a post during the last week or so.
Working with Doubles in MQL4 - MQL4 Articles DoubleToStrMorePrecision
Simon Gniadkowski
17271
Simon Gniadkowski  
WHRoeder:
Working with Doubles in MQL4 - MQL4 Articles DoubleToStrMorePrecision



Bookmarked ;-) thank you.
To add comments, please log in or register