casting bug

Discussion in 'C++' started by sergio, Jan 1, 2005.

  1. sergio

    sergio Guest

    hi, i've been proggraming for years, and have no ideia what the bug is
    in this simple code, does someone has a clue? I'm including all the
    code (20 lines).

    the output is:
    2
    2
    3
    3

    instead of the expected:
    3
    3
    3
    3

    #include <iostream.h>
    #include <math.h>

    template <class T>
    T f(T index,T N)
    {
    double t=(log(8)/log(2));
    //at this point of the code t=3, but after the cast it turns to
    2!

    return (T)t;
    }

    int main()
    {
    cout<<f<int>(8,2)<<endl;
    cout<<f<short>(8,2)<<endl;
    cout<<f<float>(8,2)<<endl;
    cout<<f<double>(8,2)<<endl;

    return 0;
    }

    thanks for any help
     
    sergio, Jan 1, 2005
    #1
    1. Advertising

  2. sergio

    Jack Klein Guest

    On 1 Jan 2005 12:15:02 -0800, "sergio" <> wrote
    in comp.lang.c++:

    > hi, i've been proggraming for years, and have no ideia what the bug is
    > in this simple code, does someone has a clue? I'm including all the
    > code (20 lines).
    >
    > the output is:
    > 2
    > 2
    > 3
    > 3
    >
    > instead of the expected:
    > 3
    > 3
    > 3
    > 3
    >
    > #include <iostream.h>
    > #include <math.h>
    >
    > template <class T>
    > T f(T index,T N)
    > {
    > double t=(log(8)/log(2));
    > //at this point of the code t=3, but after the cast it turns to
    > 2!
    >
    > return (T)t;
    > }
    >
    > int main()
    > {
    > cout<<f<int>(8,2)<<endl;
    > cout<<f<short>(8,2)<<endl;
    > cout<<f<float>(8,2)<<endl;
    > cout<<f<double>(8,2)<<endl;
    >
    > return 0;
    > }
    >
    > thanks for any help


    The calculations used by the log() function do not produce exact
    results. Instead you get slightly inexact results. When you divide
    the two inexact results, you get an inexact answer, and one that is
    less than exactly 3.0. Conversion of a floating point value to an
    integer type throws away any fractional part.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jan 1, 2005
    #2
    1. Advertising

  3. sergio

    Mike Wahler Guest

    "sergio" <> wrote in message
    news:...
    > hi, i've been proggraming for years, and have no ideia what the bug is
    > in this simple code, does someone has a clue? I'm including all the
    > code (20 lines).
    >
    > the output is:
    > 2
    > 2
    > 3
    > 3
    >
    > instead of the expected:
    > 3
    > 3
    > 3
    > 3
    >
    > #include <iostream.h>
    > #include <math.h>
    >
    > template <class T>
    > T f(T index,T N)
    > {
    > double t=(log(8)/log(2));
    > //at this point of the code t=3, but after the cast it turns to
    > 2!


    Actually, it's very likely not exactly 3.0 (but very close).
    On my compiler, 't' is equal to 2.9999999999999996. So of
    course converting that value to an integer type will discard
    the fractional portion, yielding 2. Most floating point
    values are not exactly representable in binary; see:
    http://www.parashift.com/c -faq-lite/newbie.html#faq-29.16
    http://www.parashift.com/c -faq-lite/newbie.html#faq-29.17
    http://docs.sun.com/source/806-3568/ncg_goldberg.html

    > return (T)t;
    > }
    >
    > int main()
    > {
    > cout<<f<int>(8,2)<<endl;
    > cout<<f<short>(8,2)<<endl;
    > cout<<f<float>(8,2)<<endl;
    > cout<<f<double>(8,2)<<endl;
    >
    > return 0;
    > }


    -Mike
     
    Mike Wahler, Jan 1, 2005
    #3
  4. sergio

    Ron Natalie Guest

    sergio wrote:

    > double t=(log(8)/log(2));
    > //at this point of the code t=3, but after the cast it turns to


    No, at this point t is some floating point value close to 3.0.
    The conversion to an integer type discards the fractional part,
    so if the value is slightly less than 3.0, it will end up as 2.
    You probably want to round when converting to int.
     
    Ron Natalie, Jan 1, 2005
    #4
    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. kevin
    Replies:
    11
    Views:
    5,809
    Andrew McDonagh
    Jan 8, 2005
  2. Jonathan Fielder

    Casting double to float - compiler bug?

    Jonathan Fielder, Aug 6, 2003, in forum: C Programming
    Replies:
    4
    Views:
    494
    Jonathan Fielder
    Aug 7, 2003
  3. Jobs'R'Us

    Newbie: bug in argument casting to digit

    Jobs'R'Us, Aug 16, 2004, in forum: C Programming
    Replies:
    5
    Views:
    286
    Barry Schwarz
    Aug 17, 2004
  4. Wally Barnes
    Replies:
    3
    Views:
    528
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    565
    James Kanze
    Dec 20, 2009
Loading...

Share This Page