Returning integer portion of double

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

  1. Jon Ripley

    Jon Ripley Guest

    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.
    --
    Jon Ripley, Apr 26, 2005
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. Jon Ripley

    Tim Prince Guest

    "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
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johannes Zellner
    Replies:
    22
    Views:
    3,554
  2. da Vinci
    Replies:
    5
    Views:
    8,704
    da Vinci
    Jul 31, 2004
  3. Sydex
    Replies:
    12
    Views:
    6,453
    Victor Bazarov
    Feb 17, 2005
  4. Suresh V
    Replies:
    5
    Views:
    3,680
    SaticCaster
    Jul 5, 2010
  5. Ken Fine
    Replies:
    4
    Views:
    195
    Ken Fine
    Aug 14, 2003
Loading...

Share This Page