every time i make my own rounding routine with precision, i see my compiler suggesting a function long double round(long double x, int precision); so i was wondering if someone knows what library it comes from, i surely wasn't able to find it... thanks

If you're using a C-compiler, it might come from <math.h>. I'm not sure of this, but I vaguely remember that there's a round() function in there somewhere.

To my knowledge, there is no round()-function in C++, but I could very well be corrected in the next few replies.

>I vaguely remember that there's a round() function in there somewhere.
C99 added a bunch of stuff, including a round function. However, the signature doesn't match what the OP is talking about, so I'd guess it's a compiler extension.

>i was wondering if someone knows what library it comes from
Check the compiler documentation. The function you've specified doesn't exist in either the C or C++ standard.

What's a funny thing - decimal rounding of binary floats...
May be it stands in good steads ;):

/**
 *  2008-10-01 Beta version. No warranties...
 *  Rounding functions freeware mini-package.
 *  About rounding forms and MS rounding stuff
 *  see http://support.microsoft.com/kb/196652
 *  Dependencies: <math.h> <float.h>
 *  Languages: standard C and C++
 *  Not optimized!..
 */
/** Not-optimized 10 power n (n > 0) */
static double tenpow(int n) {
    double y = 10.0;
    while (--n > 0)
        y *= 10.0;
    return y;
}
/** 64-bit double precision ~ 15 digits. */
static int nMax = DBL_DIG; /* from float.h */

/** Round off with Banker's method, n in -15..15 */
double Round(double x, int n) {
    bool neg = (x < 0.0);
    double ipart, fpart;
    double y, p;
    int id;
    if (neg)
        x = -x;
    if (n > 0) {
        double yy;
        fpart = modf(x,&ipart);
        if (n > nMax)
            n = nMax;
        p = tenpow(n);
        y = fpart * p;
        fpart = modf(y,&yy);
        if (fpart < 0.5)
            fpart = 0.0;
        else if (fpart > 0.5)
            fpart = 1.0;
        else { /* Banker's Method */
            id = (int)fmod(yy,10.0);
            fpart = (id&1)? 1.0: 0.0;
        }
        yy += fpart;
        y = ipart + yy / p;
    }
    else if (n < 0) {
        if (n < nMax)
            n = -nMax;
        p = tenpow(-n);
        y = x / p;
        y = Round(y,0) * p;
    }
    else { /* n == 0 */
        fpart = modf(x,&ipart);
        if (fpart > 0.5)
            ipart += 1.0;
        else if (fpart < 0.5)
            ;
        else { /* Banker's Method */
            id = (int)fmod(ipart,10.0);
            if ((id&1) != 0)
                ipart += 1.0;
        }
        y = ipart;
    }
    return neg?-y:y;
}
/** Symmetric arithmetic rounding, n in -15..15 */
double round(double x, int n) {
    bool neg = (x < 0.0);
    double ipart, fpart;
    double y, p;
    int id;
    if (neg)
        x = -x;
    if (n > 0) {
        double yy;
        fpart = modf(x,&ipart);
        if (n > nMax)
            n = nMax;
        p = tenpow(n);
        y = fpart * p;
        fpart = modf(y,&yy);
        if (fpart < 0.5)
            yy += 1.0;
        y = ipart + yy / p;
    }
    else if (n < 0) {
        if (n < -nMax)
            n = -nMax;
        p = tenpow(-n);
        y = x / p;
        y = round(y,0) * p;
    }
    else { /* n == 0 */
        fpart = modf(x,&ipart);
        y = (fpart < 0.5)? ipart: ipart + 1;
    }
    return neg?-y:y;
}
This article has been dead for over six months. Start a new discussion instead.