Well, I was working on a structure for my own data type, when it came time to do modulus.
I was talking to a friend of mine, who recommended fmod() from math.h, but he told me it was painfully slow.
So I decided to make my own and see if it was faster. Sure enough, the end result operated five times faster than the one in math.h.
Also functional, but perhaps slower(?) is the following:Code:double mfmod(double x,double y) { double a; return ((a=x/y)-(int)a)*y; }
Code:double mfmod(double x,double y) { return x-((int)(x/y))*y; }
The timing code was as follows.
Code:#include <stdio.h>
#include <stdlib.h>
#include "fmod.h"
#include <time.h>
int main(int argc, char *argv[])
{
int x,y;
double t,u;
int i;
x=clock();
for (i=0;i<10000000;i++) u=mfmod(100000.5,3);
y=clock();
printf("Mine: %f ms\r\n\r\n",(((double)y-(double)x)*1000.0)/(double)CLOCKS_PER_SEC);
x=clock();
for (i=0;i<10000000;i++) t=fmod(100000.5,3);
y=clock();
printf("STDC: %f ms\r\n",(((double)y-(double)x)*1000.0)/(double)CLOCKS_PER_SEC);
printf("t: %f u: %f",t,u);
system("PAUSE");
return 0;
}
Have I missed something? Why does mine work faster?