Am I going crazy!

 
Hi, I really hate to post this, but what am I doing wrong.??? The very simple code below though obviously false, returns true. If I swap || for && it behaves properly. Please help!




void OnTick()
  {
//---
   if(Piv1() == true)
      {
      Alert("Piv1==true");
      }
   
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   
  }
//+------------------------------------------------------------------+
   bool Piv1()
   {
   if(1.3898 || 1.4736 == 1.1232)
      {
      return(true);
      }
      else return(false);
      
   }
 
Mark Boc:
Hi, I really hate to post this, but what am I doing wrong.??? The very simple code below though obviously false, returns true. If I swap || for && it behaves properly. Please help!




The

if(1.3898 || ...

results in true (any value different from 0 checked that way is true)

 
Mladen Rakic:

The

results in true (any value different from 0 is true)

Sorry, but I still don't get it.  How can  a statement like  8 || 6 == 5   be considered true?

 
Mark Boc:

Sorry, but I still don't get it.  How can  a statement like  8 || 6 == 5   be considered true?

Expression "if (8)" results in true. The "or" part after that is not evaluated when the first part is already true
 
Mladen Rakic:
Expression "if (8)" results in true. The "or" part after that is not evaluated when the first part is already true

Ok, the computer basically sees it as 8 is 8, so the first part is true. A bit embarrassing but thanks for the clearing that up!

 
Mark Boc:

Ok, the computer basically sees it as 8 is 8, so the first part is true. A bit embarrassing but thanks for the clearing that up!

No, it does not do it that way

Try using 0 instead of 8 and it will become clear what and how it does it

 
Mark Boc:

Ok, the computer basically sees it as 8 is 8, so the first part is true. A bit embarrassing but thanks for the clearing that up!

I guess the issue here is operator precedence so I believe you should add parentheses

if ( (1.3898 || 1.4736) == 1.1232)
 
  1. False is zero, true non-zero
    if(1.3898 || 1.4736 == 1.1232)
    if(true || false)
    if(true)

  2. Doubles are rarely equal.
              The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum
 
whroeder1:
  1. False is zero, true non-zero

  2. Doubles are rarely equal.
              The == operand. - MQL4 and MetaTrader 4 - MQL4 programming forum


You should familiarize yourself with the concepts of truthy and falsy. All expressions evaluate to a boolean result, and when you have a stand-alone expression like you have - it will still resolve to boolean. In your case 

if(1.23456)

is the same as writing 

if(1.23456 > 0.0)

Also, an easy way to compare doubles without having to write any extra code is by importing the CDouble library. 

#include <Double.mqh>
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(Piv1())
      Alert("Piv1==true");
  }
//+------------------------------------------------------------------+
bool Piv1()
  {
   if(CDouble::IsEqual(1.4736, 1.1232))
      return(true);
   return(false);
  }
//+------------------------------------------------------------------+
 
   if(1.3898 || 1.4736 == 1.1232)
      {
      return(true);
      }
      else return(false);
Simplify your code.
          Increase Order after stoploss - MQL4 and MetaTrader 4 - MQL4 programming forum № 3
 
if(1.23456)

is the same thing as 

if(1.23456!=0)
Reason: