Results 1 to 7 of 7

Thread: rounding to a specific place?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    rounding to a specific place?

    when I have cin >> dprice; get a double value and I type in 8.88, it gets immediately stored as 8.8800000000000008. Now the real questions about this would be like why does it do that, why don't they fix that, and why do people still use C++ when it does stuff like that?
    But my question for the time being is, how can I round that value off to one less place to get rid of the extra value that gets tacked on? Math.h's ceil rounds it up to 9 and floor drops it to 8 and it doesn't seem to take any parameters for the amount of decimal places I want so is there another common library that has a round function or any other way to fix it other than rounding?
    oh and btw, MSND says the C++ version of math.h has a round function that does just that but the compiler says it doesn't.
    Last edited by Desolator144; Feb 28th, 2007 at 05:51 PM.
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: rounding to a specific place?

    The reason 8.88, it gets is stored as 8.8800000000000008 is because of the way floating point numbers are represented in computers (the IEEE floating point standard). I won't go into details about the format, but the result is that not all decimal numbers can be represented exactly. When you decide "8.88", the number is stored as the closest possible approximation to that value. This will happen in every single programing language , because they all (to my knowledge) use the IEEE FP standard.

    You should just ignore it, because it will be negligible in your calculations. If you want to display the value to a certain number of decimal points, you can do something like this:

    Code:
    #include <iomanip>
    #include <iostream>
    
    // ....
    float f = 8.88f;
    
    std::cout << std::setprecision(3) << f << std::endl;
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    Re: rounding to a specific place?

    actually it becomes a problem because I'm off by a few pennies. I still can't find a damn rounding function so....

    inputting 9.99 as dprice...
    dchange = 10.00 - dprice;
    results in 0.0099999999999997868 so in the next step

    itotalpennies = dchange * 100;
    it calculates to 0.99999999999997868 then of course assigns 0 to pennies and it says you don't owe them any change.
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    Re: rounding to a specific place?

    aha! I finally found a link where what someone says makes sense cuz they explain it :P I add 0.5 that way it rounds the value up a penny if it's ###.50000000001 or down if it's ###.4999999999 when converted to an integer btw if I'm not mistaken, VB.net's decimal data type doesn't have that problem.
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  5. #5
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: rounding to a specific place?

    Quote Originally Posted by Desolator144
    aha! I finally found a link where what someone says makes sense cuz they explain it :P I add 0.5 that way it rounds the value up a penny if it's ###.50000000001 or down if it's ###.4999999999 when converted to an integer btw if I'm not mistaken, VB.net's decimal data type doesn't have that problem.
    The .NET System.Double will have the exact same problem. MSDN says:
    Double complies with the IEC 60559:1989 (IEEE 754) standard for binary floating-point arithmetic.
    which means that it represents floating point numbers internally in exactly the same way as C/C++.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: rounding to a specific place?

    VB.NET's Decimal type doesn't exhibit the precision issue because it's not stored as an approximation.

    It's also slower, because decimal operations can't take advantage of the FPU.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: rounding to a specific place?

    You can do the same thing in C++. Search for "arbitrary precision math libraries". The tricky part is actually separating the wheat from the chaff. One of the best is the GNU multiple precision library, GMPlib:
    http://gmplib.org/
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width