Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

rounding

matlab

I need a C rounding function which rounds numbers 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

like image 992
DemiSheep Avatar asked Aug 30 '10 13:08

DemiSheep


1 Answers

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.

like image 87
nos Avatar answered Nov 15 '22 09:11

nos