Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolute value function 'fabsf' given an argument of type 'double' but has parameter of type 'float' which may cause truncation of value?

Given this code sample:

CGFloat a = 1;
CGFloat b = 2;
CGFloat c = fabsf(a-b);

The current Xcode beta compiler gives me this warning:

Absolute value function 'fabsf' given an argument of type 'double' but has parameter of type 'float' which may cause truncation of value

Why?

like image 850
zoul Avatar asked Mar 06 '15 11:03

zoul


2 Answers

You can use fabs instead of fabsf.

Link

like image 123
qysnolan Avatar answered Nov 14 '22 22:11

qysnolan


I see, the code works with a plain float type. And when I hunted for the definition of CGFloat, I found this:

#if defined(__LP64__) && __LP64__
# define CGFLOAT_TYPE double
# define CGFLOAT_IS_DOUBLE 1
# define CGFLOAT_MIN DBL_MIN
# define CGFLOAT_MAX DBL_MAX
#else
# define CGFLOAT_TYPE float
# define CGFLOAT_IS_DOUBLE 0
# define CGFLOAT_MIN FLT_MIN
# define CGFLOAT_MAX FLT_MAX
#endif

typedef CGFLOAT_TYPE CGFloat;

So CGFloat is now actually a double, hence the warning.

like image 36
zoul Avatar answered Nov 14 '22 21:11

zoul