Comparing negative numbers

 
if (ProfitToday() <= MaxLoss)    {Print("Exceeded. ProfitToday is: " + ProfitToday() + " MaxLoss " + MaxLoss);}

The Print log says:

Exceeded. ProfitToday is: -1.14 MaxLoss -10

I am confused. I thought -1.14 was > -10. But the condition is <= and it's triggering.

I changed the condition to > and it's not triggered anymore, but I can't leave it that way because I can't understand what is really going on.

What am I doing wrong? Isn't -1.4 greater than -10?

 
whoowl:
I thought -1.14 was > -10

This is true:

void OnStart()
  {
   Alert(-1.14 > -10.0);
  }

Check your code

 

It is possible that the ProfitToday() function returns a different result when called again.

Call it only 1 time. To do this, save the result to a variable.

 
whoowl: The Print log says: Exceeded. ProfitToday is: -1.14 MaxLoss -10. I am confused. I thought -1.14 was > -10. But the condition is <= and it's triggering. I changed the condition to > and it's not triggered anymore, but I can't leave it that way because I can't understand what is really going on. What am I doing wrong? Isn't -1.4 greater than -10?

As a "good practice" in programming, functions should not be called more than once when referring to their returned value multiple times.

For one, it is inefficient if the function has to process the data yet again, and for the other, the returned value may be different on subsequent calls just as @Vladislav Boyko has suggested.

Always assign the function's return value to a local variable and then use that variable subsequently.

// Uncompiled, untested, just typed out as an example
double dbProfitToday = ProfitToday();
if( dbProfitToday <= MaxLoss )  Print( "Exceeded ProfitToday is: ", dbProfitToday, " MaxLoss ", MaxLoss );
 
Fernando Carreiro #:

Always assign the function's return value to a local variable and then use that variable subsequently.

Great advice Fernnado - also helps a lot when debugging