# Returning integer portion of double

Discussion in 'C Programming' started by Jon Ripley, Apr 26, 2005.

1. ### Jon RipleyGuest

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.

TIA,
Jon R.
--

Jon Ripley, Apr 26, 2005

2. ### Barry SchwarzGuest

On Mon, 25 Apr 2005 23:27:19 GMT, Jon Ripley <>
wrote:

>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>>

Barry Schwarz, Apr 26, 2005

3. ### Tim PrinceGuest

"Jon Ripley" <> wrote in message
news:r1fbe.151396\$...
> 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.

Tim Prince, Apr 26, 2005