using modulus(%)operator with double

M

mohi

hi everyone
i know that the fundamental modulus operator (%) is designed to take
two int or long arguments,
but i hav a problem where i need to find the remender generated by the
division of a number which is equal to 2^32(not 2^31) and so it can
only be stored in a double .so how to find like

double divident=pow(2.0,32);

int divisor=36;//say

int /*or whatever type*/ rem= dividnt/divisor; //(logically)

so can anyone help how to find the remender in such a case ?????
mohan
 
T

Tom

Hi mohan,

not sure if I understand you problem correctly, but I think this might
do the trick:

double x,y;
double mod = x-long(x)/long(y)*long(y);

Thomas
 
D

Dario Saccavino

hi everyone
i know that the fundamental modulus operator (%) is designed to take
two int or long arguments,
but i hav a problem where i need to find the remender generated by the
division of a number which is equal to 2^32(not 2^31) and so it can
only be stored in a double .so how to find like

Solution 1: Use an integral type with at least 33 bits. For instance,
you can write:

#include <stdint.h>
int64_t dividend = ((int64_t)1) << 32;
int rem = (int)(dividend % 36);

Solution 2: Use the fact that (a*b)%c == ((a%c)*(b%c))%c, at least if
c>0 and no overflow occurs; so, since 2^32 == 2^16*2^16, you can
write:

int rem = ((65536 % 36) * (65536 % 36)) % 36;

Solution 3: Use double and the fmod function:

#include <math.h>
int rem = (int)fmod(pow(2,32), 36);


Dario
 
G

Greg Herlihy

double divident=pow(2.0,32);

int divisor=36;//say

int  /*or whatever type*/   rem= dividnt/divisor; //(logically)

so can anyone help how to find the remender in such a case ?????

#include <cmath>.
// ...

int remainder = std::fmod( divident, divisor);

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top