Download MetaTrader 5

Division remainder %

To add comments, please log in or register
Are you accustomed to using the standard library? MQL5 has one!
Keith Watford
Moderator
9621
Keith Watford 2014.02.23 11:06 

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?
Fernando Carreiro
2352
Fernando Carreiro 2014.02.23 11:39  

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".

Alain Verleyen
Moderator
30725
Alain Verleyen 2014.02.23 11:43  
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.
Keith Watford
Moderator
9621
Keith Watford 2014.02.23 11:46  

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.

Simon Gniadkowski
Moderator
17999
Simon Gniadkowski 2014.02.23 11:47  
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
Keith Watford
Moderator
9621
Keith Watford 2014.02.23 11:48  
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

Simon Gniadkowski
Moderator
17999
Simon Gniadkowski 2014.02.23 11:50  
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.
Keith Watford
Moderator
9621
Keith Watford 2014.02.23 12:01  
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

To add comments, please log in or register