Help with MathMod() results

 

am stuck... please explain results.

example: 0.05%0.01=0 BUT "if" condition says otherwise

ideas?

Best 2 u

#property show_inputs
extern double dVolume=0.1;
int start()
{
  Print("MathMod(",DoubleToStr(dVolume,8),",",DoubleToStr(MarketInfo(Symbol(),MODE_LOTSTEP),8),")="
        ,MathMod(dVolume,MarketInfo(Symbol(),MODE_LOTSTEP)));
  if(MathMod(dVolume,MarketInfo(Symbol(),MODE_LOTSTEP))!=0)
    Print("dVolume incorrect");
  return(0);
}

/*
2008.10.29 18:48:15 _quicky GBPUSD,M15: MathMod(0.01000000,0.01000000)=0
2008.10.29 18:48:15 _quicky GBPUSD,M15 inputs: dVolume=0.01; 
2008.10.29 18:48:02 _quicky GBPUSD,M15: loaded successfully

2008.10.29 18:47:45 _quicky GBPUSD,M15: dVolume incorrect
2008.10.29 18:47:45 _quicky GBPUSD,M15: MathMod(0.10000000,0.01000000)=0
2008.10.29 18:47:45 _quicky GBPUSD,M15 inputs: dVolume=0.1; 

2008.10.29 18:50:08 _quicky GBPUSD,M15: dVolume incorrect
2008.10.29 18:50:08 _quicky GBPUSD,M15: MathMod(0.05000000,0.01000000)=0
2008.10.29 18:50:08 _quicky GBPUSD,M15 inputs: dVolume=0.05; 

2008.10.29 18:52:03 _quicky GBPUSD,M15: dVolume incorrect
2008.10.29 18:52:03 _quicky GBPUSD,M15: MathMod(0.11000000,0.01000000)=0.01
2008.10.29 18:52:03 _quicky GBPUSD,M15 inputs: dVolume=0.11; 
*/
 

Compiler complains here:

int start(){ 
Print("6 % 4 = ", 6 % 4);
Print("6.1 % 4.1 = ", 6.1 % 4.1); // compiler error


%' - remainder operator is to be applied to integer values only C:\Program Files (x86)\MetaTrader\Broco Trader\experts\indicators\Phy__Test.mq4 (13, 27)

 
fbj wrote >>

am stuck... please explain results.

example: 0.05%0.01=0 BUT "if" condition says otherwise

ideas?

Best 2 u

my error! i use % to inidcate meaning "modulus" and just use shorthand symbol...

please to run or do test with eg,

1. MathMod(0.1,0.01); -> 0

2. MathMod(0.123,0.01); -> 0.003

both are as expected...

1. 2008.10.30 09:34:25 _quicky CHFJPY,M5: <2> MathMod(0.13000000,0.01000000)=0
2. 2008.10.30 09:34:46 _quicky CHFJPY,M5: <2> MathMod(0.12300000,0.01000000)=0.003
.

so why does (1) that returns zero get 'seen' in if cond as TRUE ???

.

am seriously loosing plot over this - need help

thankyou

.

i have > in cond but have also done !=0 not make diff, i desperate so use > but that not good cuz only check 1 and not check 2 states of tri-state possibility: < = >

.

#property show_inputs
extern double dVolume=0.1;
int start()
{
  double dLotStep=0.01;

  Print("<1> MathMod(",DoubleToStr(dVolume,8),",",DoubleToStr(MarketInfo(Symbol(),MODE_LOTSTEP),8),")="
       ,MathMod(dVolume,MarketInfo(Symbol(),MODE_LOTSTEP)));
  //
  if(MathMod(dVolume,MarketInfo(Symbol(),MODE_LOTSTEP))>0)
    Print("<1> dVolume incorrect");
  else Print("<1> dVolume correct");
  //
  Print("<2> MathMod(",DoubleToStr(dVolume,8),",",DoubleToStr(dLotStep,8),")=",MathMod(dVolume,dLotStep));
  //
  if(MathMod(dVolume,dLotStep)>0) {
    Print("<2> dVolume incorrect");
  }
  else Print("<2> dVolume correct");
  
  return(0);
}

2008.10.30 09:42:52 _quicky CHFJPY,M5: <2> dVolume correct
2008.10.30 09:42:52 _quicky CHFJPY,M5: <2> MathMod(0.01000000,0.01000000)=0
2008.10.30 09:42:52 _quicky CHFJPY,M5: <1> dVolume correct
2008.10.30 09:42:52 _quicky CHFJPY,M5: <1> MathMod(0.01000000,0.01000000)=0
2008.10.30 09:42:52 _quicky CHFJPY,M5 inputs: dVolume=0.01;

.
2008.10.30 09:42:20 _quicky CHFJPY,M5: <2> dVolume incorrect
2008.10.30 09:42:20 _quicky CHFJPY,M5: <2> MathMod(0.12300000,0.01000000)=0.003
2008.10.30 09:42:20 _quicky CHFJPY,M5: <1> dVolume incorrect
2008.10.30 09:42:20 _quicky CHFJPY,M5: <1> MathMod(0.12300000,0.01000000)=0.003
2008.10.30 09:42:20 _quicky CHFJPY,M5 inputs: dVolume=0.123;

.

expect this to be "correct"

2008.10.30 09:42:05 _quicky CHFJPY,M5: <2> dVolume incorrect
2008.10.30 09:42:05 _quicky CHFJPY,M5: <2> MathMod(0.10000000,0.01000000)=0
2008.10.30 09:42:05 _quicky CHFJPY,M5: <1> dVolume incorrect
2008.10.30 09:42:05 _quicky CHFJPY,M5: <1> MathMod(0.10000000,0.01000000)=0
2008.10.30 09:42:05 _quicky CHFJPY,M5 inputs: dVolume=0.1;

 

I suspect you are running into known problems with binary representation of decimal numbers

http://www.h-schmidt.net/FloatApplet/IEEE754.html

 

dear phy - one expression evalutes to ZERO, if condition evaluate and get non zero

so WHY representation of decimal numbers - not we expect interpreter to AT LEAST be consistent in its expression evaluations??

so that both zero or both nonzero.

but not zero and other evaluation non zero

btw

u have some nice links!

 

Convert your values to integers then use MathMod()

Reason: