double or float error

Discussion in 'C++' started by Kelvin, Jun 8, 2005.

  1. Kelvin

    Kelvin Guest

    Hi:
    I have
    double d = 0.33

    when I try to display this using:
    cout << d;
    it gives me 0.3329999999999999
    i know it's caused by the way that computer stores float number...
    but how can i get exactly 0.33 without using printf like arguement ??

    thank you


    --
    { Kelvin@!!! }
     
    Kelvin, Jun 8, 2005
    #1
    1. Advertising

  2. Kelvin wrote:
    > Hi:
    > I have
    > double d = 0.33
    >
    > when I try to display this using:
    > cout << d;
    > it gives me 0.3329999999999999
    > i know it's caused by the way that computer stores float number...
    > but how can i get exactly 0.33 without using printf like arguement ??
    >
    > thank you
    >
    >
    > --
    > { Kelvin@!!! }


    Hi Kelvin,
    What's your compiler?
    I've tested by using gcc3.2.3, the result of both cout and printf
    are the same.
     
    Prawit Chaivong, Jun 8, 2005
    #2
    1. Advertising

  3. Kelvin

    Guest

    try
    int OldWidth = cout.width(2);
    cout << d;
    cout. width(OldWidth);
     
    , Jun 8, 2005
    #3
  4. Kelvin

    Guest

    GAH, must have been dreaming or something
    This is wrong:
    > int OldWidth = cout.width(2);
    > cout << d;
    > cout. width(OldWidth);


    should be:

    int OldPrecision = cout.precision(3);
    cout << d;
    cout.precision(OldPrecision);

    Note that this limits the total number of digits shown of a floating
    point value. so if you get a value like 123.456 you get 123 not 123.456
     
    , Jun 8, 2005
    #4
  5. Kelvin

    Kelvin Guest

    "Prawit Chaivong" <> wrote in message
    news:...
    >
    >
    > Kelvin wrote:
    >> Hi:
    >> I have
    >> double d = 0.33
    >>
    >> when I try to display this using:
    >> cout << d;
    >> it gives me 0.3329999999999999
    >> i know it's caused by the way that computer stores float number...
    >> but how can i get exactly 0.33 without using printf like arguement ??
    >>
    >> thank you
    >>
    >>
    >> --
    >> { Kelvin@!!! }

    >
    > Hi Kelvin,
    > What's your compiler?
    > I've tested by using gcc3.2.3, the result of both cout and printf
    > are the same.
    >


    Hi Prawit:
    I'm using the VC 2005 express...
    cout << d; is just an example that would show my problem in the simplist
    way...

    i'm lookin for precision control in C++... which i just couldn't find
    anywhere...

    to round off to a certain digit... i tried:
    double RoundOff( double d, int precision )
    {
    int p = precision;
    while( p-- )
    d *= 10;

    d = (int)d;
    p = precision;
    while( p++ )
    d /= 10;

    return d;
    }

    this function seems fine... but it produces the problem i stated...

    --
    { Kelvin@!!! }
     
    Kelvin, Jun 8, 2005
    #5
  6. Kelvin

    Chris Theis Guest

    "Kelvin" <> wrote in message
    news:l1ype.51998$tt5.50100@edtnps90...
    > Hi:
    > I have
    > double d = 0.33
    >
    > when I try to display this using:
    > cout << d;
    > it gives me 0.3329999999999999
    > i know it's caused by the way that computer stores float number...
    > but how can i get exactly 0.33 without using printf like arguement ??
    >
    > thank you
    >


    You can use the std::setprecision stream manipulator.

    cout << std::setprecision(2) << d << endl; // use 2 or whatever
    precision you want

    HTH
    Chris
     
    Chris Theis, Jun 8, 2005
    #6
  7. Kelvin

    Guest


    > to round off to a certain digit... i tried:
    > double RoundOff( double d, int precision )
    > {
    > int p = precision;
    > while( p-- )
    > d *= 10;
    >
    > d = (int)d;
    > p = precision;
    > while( p++ )
    > d /= 10;
    >
    > return d;
    > }
    >
    > this function seems fine... but it produces the problem i stated...


    Uh no this function is something I'm going to hand the guy who tought
    me C++ when I went to school so that he has a prime example of how not
    work with doubles to show to his students.

    If you want to work extensively with floating point types go and read:
    http://www.physics.ohio-state.edu/~dws/grouplinks/floating_point_math.pdf
    Note that this was written before the standarisation of C++ so you
    might need to adapt the code to current standards.

    A few things that just pop in why this function is wrong.
    What happens if d is larger then 2^31-1 (after multiplication)
    What happens if you try a precision greater then 16.
    What happens if you feed in a double like 1.345e-5 with a precision
    value of 4
    How big will roundoff errors be.
    And the biggest of all the function doesn't do what you think it should
    do. It doesn't hand you a nicely cut to precision length double, it
    hands you a (most likely) mangled representation of the double you put
    in.
     
    , Jun 9, 2005
    #7
  8. maybe you should swap the
    d/=10 and d*=10

    "Kelvin" <> schrieb im
    Newsbeitrag news:l1ype.51998$tt5.50100@edtnps90...
    > Hi:
    > I have
    > double d = 0.33
    >
    > when I try to display this using:
    > cout << d;
    > it gives me 0.3329999999999999
    > i know it's caused by the way that computer stores float number...
    > but how can i get exactly 0.33 without using printf like arguement ??
    >
    > thank you
    >
    >
    > --
    > { Kelvin@!!! }
    >
     
    Michal Idziorek, Jun 9, 2005
    #8
    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. JKop
    Replies:
    4
    Views:
    16,544
  2. Sydex
    Replies:
    12
    Views:
    6,568
    Victor Bazarov
    Feb 17, 2005
  3. bd
    Replies:
    0
    Views:
    651
  4. Shirsoft
    Replies:
    13
    Views:
    627
    Jerry Coffin
    Feb 10, 2007
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,610
    James Kanze
    Oct 8, 2009
Loading...

Share This Page