2

I'm compiling my first project with 64 bit support enabled. I'm running into a bunch of compiler warnings about implicit conversions to float. This is happening because I'm using fabsf() and assigning the result to a CGFloat (which is a double, not float on the new 64 bit architecture).

According to the answer on this question:

CGFloat-based math functions?

I just need to #include <tgmath.h> to solve this problem and probably change fabsf to fabs. I have at least one file where this doesn't seem to be helping. I still get the warning: implicit conversion loses floating-point precision 'double' to 'CGFloat' aka (float). Here's the line that generates that warning:

CGFloat deltaX = fabs(item.center.x-point.x);

Has anyone else run across this? How did you solve it? I'd rather not disable this warning or litter my code with a ton of typecasts.

Community
  • 1
  • 1
Rob Jones
  • 4,925
  • 3
  • 32
  • 39

2 Answers2

3

I guess, you are using CGPoint types, so the conversion doesn't occur within fabs(DOUBLE -> FLOAT), but on assignment CGFloat = DOUBLE. That's probably because compiler used fabs from math.h which operates on doubles.

Problem with math.h is that it's internally imported by OSX headers (carbon if I remember correctly), so I guess some iOS header might also do that. After quick look, it seems that basic set of frameworks doesn't import math.h, so probably you should look for it being manually imported. In case it's imported internally by some system libs, you'll probably won't be able to use those libs and tgmath in a single implementation file.

If you want to check if there are some math.h dependencies, you can use a dirty trick to prevent it's inclusion - add this line to a file (or better on top of prefix file):

#define __MATH_H__
Rychu
  • 1,028
  • 1
  • 9
  • 20
  • Yes, in the example from my question I'm directly accessing the x/y fields of a CGPoint. In this case, if I preprocess the file, I do not see the tgmath fabs macro expanded at the point where it's used. I've clearly included tgmath.h in this file. Looking at the preprocessed file, it generates one of those new `@import` lines. – Rob Jones Mar 08 '14 at 19:21
  • I've found that importing `tgmath.h` in the pch will affect a change in behavior. That is, the line in question is preprocessed to `CGFloat deltaX = __tg_fabs((__typeof__(__tg_promote((item.center.x-point.x))))(item.center.x-point.x));`, which is what I expected from importing `tgmath.h` in the file in question. Thanks for the help! – Rob Jones Mar 08 '14 at 19:45
1

I was able to get the tgmath.h functions to work by including the header at the top of my PCH file.

At some point (read: Xcode update) I had to start disabling Modules to get this to work. The details of such are in the question Dima links to below.

Community
  • 1
  • 1
Rob Jones
  • 4,925
  • 3
  • 32
  • 39
  • Is there an easy way for me to check if this is actually working? After doing that, I am still getting cast warnings and cmd+clicking into the function declaration still takes me into math.h rather than tgmath.h. Because of this, it doesn't seem like this is working. – Dima Apr 27 '14 at 23:48
  • 1
    After messing with my project settings I was finally able to get it to work by removing the "Enable Modules" setting. No idea why this is the case so I am going to open up a new question. – Dima Apr 28 '14 at 05:21
  • Here is my question: http://stackoverflow.com/questions/23333287/why-does-tgmath-h-only-work-if-modules-are-disabled – Dima Apr 28 '14 at 05:42
  • 1
    @Dima I've actually run into this same problem too. I can't remember when it started, if it was after a specific Xcode release, but what you suggest is the exact way I dealt with it too. Cheers for including the info here. I'll augment my answer as well. – Rob Jones Apr 30 '14 at 02:54