2

This is causing my App to act up. this error is happening on this line modff(floatIndex, &intIndex); What do I need to do to fix this issue?

Edit: it is because of &intIndex

- (BOOL)isFloatIndexBetween:(CGFloat)floatIndex {
  CGFloat intIndex, restIndex;
  restIndex = modff(floatIndex, &intIndex);
   BOOL isBetween = fabsf(restIndex - 0.5f) < EPSILON;
  return isBetween;
  }

5 Answers5

3

As I recall CGFloat is defined as float on 32 bit devices and double on 64 bit devices. Thus you don't want to use CGFloat in a call to modff(). Instead, declare your parameters using a specific type, and use casting.

Something like this (In this case I am using modf and all float variables.

- (BOOL)isFloatIndexBetween:(CGFloat)floatIndex 
{
  float restIndex;
  float first, second;
  first = (float) floatIndex; 
  restIndex = modf(first, &second);
  BOOL isBetween = fabsf(restIndex - 0.5f) < EPSILON;
  return isBetween;
}
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • +1. Also, [importing `tgmath.h`](http://stackoverflow.com/a/7695011/1445366) will give you access to some functions that let you handle this differently. See also [this answer](http://stackoverflow.com/a/5352779/1445366). – Aaron Brager Feb 03 '15 at 01:20
1

You are passing CGFloats (typedef'ed to double on your system) to functions that expect floats.

You can either change modff and fabsf to modf and fabs, respectively (slower but more precise), or change intIndex and restIndex to be floats instead of doubles (faster but less precise).

NobodyNada
  • 7,529
  • 6
  • 44
  • 51
1

Learning to speak compiler error/warning is an invaluable skill. In this case, it is telling you that modff is expecting a float (that is, a single-precision floating point number), but you're passing it a CGFloat (which is typedef'd as a double, which is a double-precision floating point number). As NobodyNada says, you can either change which function you're using or the type if intIndex.

oltman
  • 1,772
  • 1
  • 14
  • 24
0

Perhaps the easiest way to avoid these types of warnings and errors when using an architecture specific types like CGFloat is to put #import <tgmath.h> in your precompiled header or the imports for this file. That way the type-generic versions of the underly C functions are used. In this case it makes your warnings go away without any code changes. Then it's just a matter of making sure the precision is what you want.

Jon Shier
  • 12,200
  • 3
  • 35
  • 37
0

If you are using 64-bit architectures (like arm64),then you should use CGFloat because it is defined as double and therefore a 8-byte floating point number, whereas float is a 4-byte floating point number.

So you should use these according to architecture.

Gopal Devra
  • 564
  • 2
  • 5
  • 24