Problems with fmod.

A

Amit Bhatia

Hello everyone.
Sorry to be cross posting this on comp.lang.c++ (moderated)

This is a simple question that is causing some problem in one of the classes
that I have designed.
I have two doubles:
double a=2.4;
double b=0.15;
const double VERYTINY = 1.e-30; \\I define how small is small here;

and I know that 2.4 / 0.15 = 8

But if I try to use a statement like
if(fmod(a,b)>VERYTINY)
return false;
then it returns false.(?)

However if I use
if(fmodf(a,b)>VERYTINY)
return false;
then it does return true.

So how should I check if two doubles are multiples of each other?

thanks,
amit.
 
M

Mike Wahler

Amit Bhatia said:
Hello everyone.
Sorry to be cross posting this on comp.lang.c++ (moderated)

This is a simple question that is causing some problem in one of the classes
that I have designed.
I have two doubles:
double a=2.4;
double b=0.15;
const double VERYTINY = 1.e-30; \\I define how small is small here;

and I know that 2.4 / 0.15 = 8

But if I try to use a statement like
if(fmod(a,b)>VERYTINY)
return false;
then it returns false.(?)

However if I use
if(fmodf(a,b)>VERYTINY)
return false;
then it does return true.

So how should I check if two doubles are multiples of each other?

http://docs.sun.com/source/806-3568/ncg_goldberg.html

-Mike
 
A

Amit Bhatia

Thanks Mike,
I am able to understand some part of it but not whole of it(in fact not
much of it). I though saw a similar example on 3.0/7.0 , but still...

Can you suggest some solution to this that can be used in C?

thanks,
amit.
 
M

Mike Wahler

Amit Bhatia said:
Thanks Mike,
I am able to understand some part of it but not whole of it(in fact not
much of it). I though saw a similar example on 3.0/7.0 , but still...

Can you suggest some solution to this that can be used in C?

Define an appropriate 'threshold value' that's 'close enough'
for your purposes, and use it for an adjustment when comparing
values.

There also exist third party libraries which can give better
precision than types 'float' or 'double' (typically at a
higher cost in storage and CPU time. None spring to mind
at the moment, try Google.

-Mike
 
A

Amit Bhatia

Mike,

How about this one: it is given by bruce dawson and came up on google..?
What I still am stuck at though is how to choose Ulps.

// Usable AlmostEqual function
bool AlmostEqual2sComplement(float A, float B, int maxUlps)
{
// Make sure maxUlps is non-negative and small enough that the
// default NAN won't compare as equal to anything.
assert(maxUlps > 0 && maxUlps < 4 * 1024 * 1024);
int aInt = *(int*)&A;
// Make aInt lexicographically ordered as a twos-complement int
if (aInt < 0)
aInt = 0x80000000 - aInt;
// Make bInt lexicographically ordered as a twos-complement int
int bInt = *(int*)&B;
if (bInt < 0)
bInt = 0x80000000 - bInt;
int intDiff = abs(aInt - bInt);
if (intDiff <= maxUlps)
return true;
return false;
}

thanks once again,
amit.
 
A

Amit Bhatia

Additionally,
what needs to be changed if I want to be comparing doubles instead of
floats: any sugestions on this one? Will merely changing float->double
everywhere do the job?

amit.
 

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,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top