Division remainder %

 

Up until now, I guess that I have only used integers when using % in my code.

 double remainder = 100 % 30;

works fine

double remainder = 100 % 30.1;

Compiler reports error '%' - illegal operation use

as does

double remainder = 100.00 % 30;

double remainder = 100 % 30.00;

So both values have to be an integer?
 

The usual mathematical definition of Modulo operations is usually only with Integers.

Some languages such as C# allow for the use of floating point calculations, however, assuming that MQL is built upon a Unix C foundation, it is most probably only valid for integers.

Unfortunately, the MQL documentation is not very clear on this point, plus it is misspelled as "Module" instead of "Modulo" or "Modulus".

 
FMIC:

The usual mathematical definition of Modulo operations is usually only with Integers.

Some languages such as C# allow for the use of floating point calculations, however, assuming that MQL is built upon a Unix C foundation, it is most probably only valid for integers.

Unfortunately, the MQL documentation is not very clear on this point, plus it is misspelled as "Module" instead of "Modulo" or "Modulus".

Compiler seems very clear though. Only integers.
 

Thanks FMIC,

I just wanted to make sure that it wasn't a bug.

I can easily sort out what I want to do with MathFloor, I think.

 
GumRai:

Up until now, I guess that I have only used integers when using % in my code.

works fine

Compiler reports error '%' - illegal operation use

as does

So both values have to be an integer?
Have you tested MathMod() ? does it still have the same issues it had before ? https://www.mql5.com/en/forum/143605
 
angevoyageur:
Compiler seems very clear though. Only integers.

"illegal operation use" wasn't very clear to me :(

I don't see anywhere that it says only integers

 
GumRai:

"illegal operation use" wasn't very clear to me :(

I don't see anywhere that it says only integers

Nope, neither did I, I also looked through the documentation.
 
RaptorUK:
Have you tested MathMod() ? does it still have the same issues it had before ? https://www.mql5.com/en/forum/143605


Thanks Raptor, I didn't even know about MathMod(), I will look into it.

I was thinking along the lines of

  double a = 1.5;
  double b = 3.1;
  double c = MathFloor(b/a);
  double remainder = b-c*a;
  Alert(remainder);

But of course that returns

0.100000000000001

 
Keith Watford:


Thanks Raptor, I didn't even know about MathMod(), I will look into it.

I was thinking along the lines of

But of course that returns

0.100000000000001


You can use a variation of the below function (showed in https://www.mql5.com/en/articles/1561).

//+------------------------------------------------------------------+
//| correct comparison of 2 doubles                                  |
//+------------------------------------------------------------------+
bool CompareDoubles(double number1,double number2)
{
   if(NormalizeDouble(number1-number2,8)==0) return(true);
   else return(false);
}
Working with Doubles in MQL4
Working with Doubles in MQL4
  • www.mql5.com
The MQL programming opens new opportunities for the automated trading, many people all over the world already have appreciated it. When we are writing an Expert Advisor for trading, we must be sure that it will work correctly. Many newbies often have some questions when the results of some mathematical calculations differ from those expected...
 
Gustavo Hennemann:


You can use a variation of the below function (showed in https://www.mql5.com/en/articles/1561).

Why are you replying to a topic that is over 4 years old?

 
Keith Watford:

Why are you replying to a topic that is over 4 years old?

It looks like, I'm facing this situation but I didn't see the date of the topic. Is there any problem?
Reason: