The regular '/' expression will do exactly that. There's no C library
function to do it.
Of course not. / returns only one result, the OP clearly wants two results.
typedef float FloatingPoint; // Could be double or long double
#define FloatingPointFormat "%f" // Could be "%lf" or "%Lf"
void complete_divide(FloatingPoint dividend,FloatingPoint divisor,
FloatingPoint* quotient,FloatingPoint* remainder){
(*quotient)=dividend/divisor;
(*remainder)=dividend-(divisor*(*quotient));
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char** argv){
if(argc==3){
FloatingPoint n=atof(argv[1]);
FloatingPoint d=atof(argv[2]);
FloatingPoint q;
FloatingPoint r;
complete_divide(n,d,&q,&r);
printf("Divisor = "FloatingPointFormat": dividend = "FloatingPointFormat"\n",n,d);
printf("Quotient = "FloatingPointFormat": remainder = "FloatingPointFormat"\n",q,r);
printf("Remainder %s 0\n",((r==0.0)?"==":"!="));
}
return(0);
}
--
__Pascal Bourguignon__
#include <math.h> // gcc -lm
math.h contains the following functions (and a lot more)
double remainder(double, double);
float remainderf(float, float);
long double remainderl(long double, long double);
double fmod(double x, double y);
float fmodf(float x, float y);
long double fmodl(long double x, long double y);
and remember: floating point calculations have inherent inaccuracy
so run some rigorous tests with numbers to make sure you do not
need to compensate for FP machine precision limitations.
Wikipedia has a good tutorial under "floating point"
http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
also floor(), round() and ceil() are your friend if you get
numbers like 9.999999999999997 e -1 when you want 1.0
because converting to an int ALWAYS ROUNDS DOWN. .99999999 -> int =
0.
An old programmer's trick for rounding is like this:
double rounded = (double) ((int) ((unrounded*1000)+.5)/1000 );
where 1000 shifts the number up 3 decimal places,
adds .5 then cuts it off and shifts it down again.
10,000 would do 4 places, etc.
Considering the OP wants a zero remainder, it can be assumed that he
does not care about the remainder itself, so unless his purpose is to
use the function pointer somewhere, floating point '/' is all he needs.
He wrote: "zero remainder or a renainder as near to zero as possible".
The second alternative will occur with IEE754 upon denormalization.
[pjb@galatea :0.0 tmp]$ ./d 1e-34 10.0
Divisor = 1e-34: dividend = 10
Quotient = 1e-35: remainder = 0
Remainder == 0
[pjb@galatea :0.0 tmp]$ ./d 1e-36 10.0
Divisor = 1e-36: dividend = 10
Quotient = 1e-37: remainder = 8.96831e-44
Remainder != 0
--
__Pascal Bourguignon__