objective c - Rounding when [int] = [float] + [int] (Obj-C, iOS?) -


in case:

float = 0.99999f; int b = 1000; int c = + b; 

in result c = 1001. discovered happens because b converted float (specific ios), a + b doesn't have enough precision 1000.9999 , (why?) rounded higher value. if a 0.999f c = 1000 - theoretically correct behavior.

so question why float number rounded higher value? behavior (or convention) described?

i tested on iphone simulator, apple llvm 4.2 compiler.

in int c = + b, integer b converted float first, 2 floating point numbers added, , result truncated integer.

the default floating point rounding mode fe_tonearest, means result of addition

0.99999f + 1000f 

is nearest number can represented float, , number 1001f. float truncated integer c = 1001.

if change rounding mode

#include <fenv.h> fesetround(fe_downward); 

then result of addition rounded downward (approximately 1000.99993f) , c = 1000.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -