Main Page | Report this Page
Computers Forum Index  »  Computer Languages (Objective-C)  »  Complete devide function in C standard library...
Page 1 of 1    

Complete devide function in C standard library...

Author Message
Amir...
Posted: Sat Oct 24, 2009 10:26 am
Guest
Is there in the C standard library, a complete divide function that
retrieves the quotient as a float number, and a zero remainder or a
renainder as near to zero as possible?
 
Justin Hibbits...
Posted: Sun Oct 25, 2009 2:14 pm
Guest
On Oct 24, 6:26 am, Amir <eghtedari.a... at (no spam) gmail.com> wrote:
Quote:
Is there in the C standard library, a complete divide function that
retrieves the quotient as a float number, and a zero remainder or a
renainder as near to zero as possible?

The regular '/' expression will do exactly that. There's no C library
function to do it.
 
Pascal J. Bourguignon...
Posted: Sun Oct 25, 2009 10:01 pm
Guest
Justin Hibbits <chmeeedalf at (no spam) gmail.com> writes:

Quote:
On Oct 24, 6:26 am, Amir <eghtedari.a... at (no spam) gmail.com> wrote:
Is there in the C standard library, a complete divide function that
retrieves the quotient as a float number, and a zero remainder or a
renainder as near to zero as possible?

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__
 
spikeysnack...
Posted: Mon Oct 26, 2009 2:27 am
Guest
On Oct 25, 11:01 am, p... at (no spam) informatimago.com (Pascal J. Bourguignon)
wrote:
Quote:
Justin Hibbits <chmeeed... at (no spam) gmail.com> writes:
On Oct 24, 6:26 am, Amir <eghtedari.a... at (no spam) gmail.com> wrote:
Is there in the C standard library, a complete divide function that
retrieves the quotient as a float number, and a zero remainder or a
renainder as near to zero as possible?

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.
 
Justin Hibbits...
Posted: Mon Oct 26, 2009 10:22 am
Guest
On Oct 25, 2:01 pm, p... at (no spam) informatimago.com (Pascal J. Bourguignon)
wrote:
Quote:
Justin Hibbits <chmeeed... at (no spam) gmail.com> writes:
On Oct 24, 6:26 am, Amir <eghtedari.a... at (no spam) gmail.com> wrote:
Is there in the C standard library, a complete divide function that
retrieves the quotient as a float number, and a zero remainder or a
renainder as near to zero as possible?

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__

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.
 
Pascal J. Bourguignon...
Posted: Mon Oct 26, 2009 4:00 pm
Guest
Justin Hibbits <chmeeedalf at (no spam) gmail.com> writes:

Quote:
On Oct 25, 2:01 pm, p... at (no spam) informatimago.com (Pascal J. Bourguignon)
wrote:
Justin Hibbits <chmeeed... at (no spam) gmail.com> writes:
On Oct 24, 6:26 am, Amir <eghtedari.a... at (no spam) gmail.com> wrote:
Is there in the C standard library, a complete divide function that
retrieves the quotient as a float number, and a zero remainder or a
renainder as near to zero as possible?

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__

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 at (no spam) galatea :0.0 tmp]$ ./d 1e-34 10.0
Divisor = 1e-34: dividend = 10
Quotient = 1e-35: remainder = 0
Remainder == 0
[pjb at (no spam) 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__
 
 
Page 1 of 1    
All times are GMT
The time now is Sat Mar 20, 2010 9:40 pm