Thread: does C++ have a round () function?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    20

    Question does C++ have a round () function?

    I have 2 floating point numbers with different significant digits.

    I want to round a floating point number from

    0.832557389... to 0.83256000... so i can compare it to another double that holds the value 0.83256000... to see if they are equal. it sure would be nice if there was a floating point round function. my Microsoft help file shows rounding for other languages but not C++.

    the modulo won't take a floating point number as an arguement and demote it automatically.

    I can't believe there isn't a built in function to do this!! Any ideas?

    thanks,
    susan
    Last edited by Susan; 02-09-2002 at 10:21 PM.

  2. #2
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    double a = .832557389;
    a-=.000005;
    a*=100000000;
    double b=int(a);
    b/=100000000;

    B should be the number you wanted to round it to. In the likely event I made a mistake on this code, just look at how I did it and use that strategy.

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    #include <cmath>
    using namespace std;
    
    int RoundtoNearestInt(double input)
    {
    return (floor(input+0.5));
    }
    now use tims strategy to incorporate a second parameter NumofDigitsAfterDot to round a float off
    Last edited by Stoned_Coder; 02-09-2002 at 01:00 PM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    20
    thanks S. C. and Tim, i'll give it a try.

    I had the idea about multiplying by 100000 but i was looking at ceil() and had not considered floor(). neat trick. i like it.

    now on to tim's part.
    thanks,
    susan
    Last edited by Susan; 02-09-2002 at 01:29 PM.

  5. #5
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    What about just using a delta when comparing?

    double a;
    double b;

    // stuff

    if (1.01 * a > b && .99 a < b) { // they are nearly equal
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  6. #6
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Code:
    if (abs (a - b) < .00001)
       ...
    ie if delta is sufficiently small.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    20
    Hadn't thought of the delta. I tend to be very litteral and exact was my first instinct. Thanks for pointing out another way to go. Will consider this and save for future applications.
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. I need some quick help with code.
    By stehigs321 in forum C Programming
    Replies: 35
    Last Post: 10-30-2003, 10:07 PM