objective-cfloating-point

Why is float getting rounded off in this Objective C / Cocoa code


I am doing a simple :

float x = 151.185436;
printf("x=%f",x);

and the result is

x=151.185440

Whats wrong here? I want to retain and print my original value of 151.185436

Thanks Amarsh


Solution

  • floats just aren't very accurate. Try double. And read this: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html