Returning integer portion of double

J

Jon Ripley

Hi,

This function should return the integer portion of a float truncating
down to -infinity. Where int64(12.34) = 12 and int64(-12.34) = -13 but
int64(-12) = -12.

In reality the function is called with a pointer to a double.

void int64 ( double *num )
{
double tmp;

tmp=(long)*num;
if ((tmp < 0) && (*num != tmp))
*num=tmp-1;
else
*num=tmp;
}

When given any positive number it works, when given any non-integer
negative number it works. When given a negative integer it always
returns zero.

I'm stuck, I can't get it to return correct data in the last case.

This is one of many routines I am writing to manipulate the integer
portions of 64-bit floats.

Another:

double mod64(double *x, double *y)
{
return (double)((long)*x % (long)*y));
}

Just causes the calling routine to abort rather messily.

Please help!

TIA,
Jon R.
--
 
B

Barry Schwarz

Hi,

This function should return the integer portion of a float truncating
down to -infinity. Where int64(12.34) = 12 and int64(-12.34) = -13 but
int64(-12) = -12.

In reality the function is called with a pointer to a double.

void int64 ( double *num )
{
double tmp;

tmp=(long)*num;
if ((tmp < 0) && (*num != tmp))
*num=tmp-1;
else
*num=tmp;
}

When given any positive number it works, when given any non-integer
negative number it works. When given a negative integer it always
returns zero.

Please provide a complete compilable program that demonstrates the
behavior you describe. I ran your code and got only the expected
results.
I'm stuck, I can't get it to return correct data in the last case.

This is one of many routines I am writing to manipulate the integer
portions of 64-bit floats.

Another:

double mod64(double *x, double *y)
{
return (double)((long)*x % (long)*y));
}

Just causes the calling routine to abort rather messily.

The cast to double is superfluous. You have an extra right
parenthesis. Again, I ran your code and got the expected results.
You need to provide a compilable example that causes the problem.



<<Remove the del for email>>
 
T

Tim Prince

Jon Ripley said:
Hi,

This function should return the integer portion of a float truncating down
to -infinity. Where int64(12.34) = 12 and int64(-12.34) = -13 but
int64(-12) = -12.

In reality the function is called with a pointer to a double.

void int64 ( double *num )
{
double tmp;

tmp=(long)*num;
if ((tmp < 0) && (*num != tmp))
*num=tmp-1;
else
*num=tmp;
}

When given any positive number it works, when given any non-integer
negative number it works. When given a negative integer it always returns
zero.

I'm stuck, I can't get it to return correct data in the last case.

This is one of many routines I am writing to manipulate the integer
portions of 64-bit floats.

Another:

double mod64(double *x, double *y)
{
return (double)((long)*x % (long)*y));
}

Just causes the calling routine to abort rather messily.
What difference is there between the definition of your first function and
floor() ? Or between the second and fmod() ? The functions you have
written have undefined behavior in the case where the (long) cast overflows,
which is not at all unlikely for those C implementations where long has 31
bits plus sign.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top