Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

doubles and modulus division

Status
Not open for further replies.

BungoMan

Programmer
May 13, 2002
12
0
0
US
bah! it isnt letting me use the % for modulus division with doubles, is there a way around this? i need to return random doubles, here is how im doing it right now...

double randdouble(double minnum,double maxnum)
{
setprecision(4);
double randomdouble;
if(minnum>maxnum)
{
double temp=minnum;
minnum=maxnum;
maxnum=temp;
}
if(minnum<0&&maxnum<0)
{
minnum*=-1;
maxnum*=-1;
randomdouble=rand()%(maxnum+1);
randomint*=-1;
}
else
{
if(minnum<0)
{
int negative=rand()%2;
if(negative==1)
randomint=rand(maxnum+1);
else
{
maxnum=minnum*-1
randomint=rand(maxnum+1);
randomint*=-1;
}
}
else
{
randomdouble=rand()%(maxnum+1)
while(randomdouble<minnum)
randomdouble=rand()%(maxnum+1)
}
}
return randomdouble;
}

i know there are ways for it to be screwed up, ill fix all the loopholes later, but for now this is my main concern... anyone know anything about doubles ant modulus division? (and id really really rather not use (int) to make it an integer type for that operation...)
 
From my understanding of modulus division, it will only work on integers. Using modulus returns the remainder of the division, and if you are using doubles for division there is no remainder.

Using 16 and 5 as integers, 16/5 will return a 3, and 16%5 will return the 1 remainder.

Using them as doubles however simply returns the 3.2 from the division, there is no remainder (it is incorporated into the decimal value).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top