开发者

Is there a C rounding function like MATLAB's round function?

I need a C rounding function which rounds num开发者_运维百科bers like MATLAB's round function. Is there one? If you don't know how MATLAB's round function works see this link:

MATLAB round function

I was thinking I might just write my own simple round function to match MATLAB's functionality.

Thanks,

DemiSheep


This sounds similar to the round() function from math.h

These functions shall round their argument to the nearest integer value in floating-point format, rounding halfway cases away from zero, regardless of the current rounding direction.

There's also lrint() which gives you an int return value, though lrint() and friends obey the current rounding direction - you'll have to set that using fesetround() , the various rounding directions are found here.


Check out the standard header <fenv.c>, specifically the fesetround() function and the four macros FE_DOWNWARD, FE_TOWARDZERO, FE_TONEAREST and FE_UPWARD. This controls how floating point values are rounded to integers. Make sure your implementation (i.e., C compiler / C library) actually support this (by checking the return value of fesetround() and the documentation of your implementation).

Functions honoring these settings include (from <math.h>):

  • llrint()
  • llrintf()
  • llrintl()
  • lrint()
  • lrintf()
  • lrintl()
  • rint()
  • rintf()
  • rintl()
  • llround()
  • llroundf()
  • llroundl()
  • lround()
  • lroundf()
  • lroundl()
  • nearbyint()
  • nearbyintf()
  • nearbyintl()

depending on your needs (parameter type and return type, with or without inexact floating point exception).

NOTE: round(), roundf() and roundl() do look like they belong in the list above, but these three do not honor the rounding mode set by fesetround()!!

Refer to your most favourite standard library documentation for the exact details.


No, C (before C99) doesn't have a round function. The typical approach is something like this:

double sign(double x) { 
    if (x < 0.0)
        return -1.0;
    return 1.0;
}

double round(double x) { 
    return (long long)x + 0.5 * sign(x);
}

This rounds to an integer, assuming the original number is in the range that can be represented by a long long. If you want to round to a specific number of places after the decimal point, that can be a bit harder. If the numbers aren't too large or too small, you can multiply by 10N, round to an integer, and divide by 10N again (keeping in mind that this may introduce some rounding errors of its own).


If there isn't a round() function in the standard library, you could, if dealing with floating-point numbers, arbitrarily evaluate each value, analyze the number in the place after the place you want to round to, check to see if it's greater, equal-to, or less-than 5; Then, if the value is less than 5, you can floor() the number you're ultimately looking at. If the value of the digit after the place you're rounding to is 5 or greater, you can proceed to having the function floor() the number being evaluated, then add 1.

I apologize for any inefficiency tied to this.


If I'm not mistaken you are looking for something like floor and ceil and you shall find them in <math.h>


The documentation specifies

Y = round(X) rounds the elements of X to the nearest integers.

Not the plural: as per regular MATLAB operations, it operates on all elements of a matrix. The C equivalents posted above only deal with a single value at once. If you can use C++, check out Valarray. If not, then good ol' for loop is your friend.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜