-2

I have a method loading:

-(void)loading:(MKTileOverlayPath)path ... {
...

Within this method I am trying to calculate something

NSInteger *a = pow(2, path.z) - path.y - 1;

I get an error:

Initializing 'NSInteger *' (aka 'long *') with an expression of incompatible type 'double'

Why is that double? path.z and path.y are, as stated in the documentation MKTileOverlayPath, integer. And pow(2, path.z) cannot result in an float or double neither, when there are only integers... Why is that?

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
four-eyes
  • 10,740
  • 29
  • 111
  • 220

2 Answers2

1

You have two errors:

  1. The standard C pow() produces a double and it's parameters are also double - your integer arguments are implicitly cast for you. There is no version for integer types. The best solution is not to use casts to get an integer value, that introduces the possibility of errors doe to the nature of floating-point math, but to simply write your own integer power function. You can copy the one in this answer - just implement it using the integer type (int, long etc.) you require.

  2. You have confused your variable type, object types are reference types and declared as pointers, e.g. NSString *; simple numeric types are value types and are not pointers, e.g. NSInteger.

CRD
  • 52,522
  • 5
  • 70
  • 86
0

Mathematically it won't result in a non-integer value, but that's not the contract for the pow() function. According to http://www.cplusplus.com/reference/cmath/pow/, the parameters are being implicitly cast to doubles and the return value of the function is always a double.

MButler
  • 61
  • 3
  • I am a little los there. How to write the line that it works?! `NSInteger *resultInt = (int)pow((double)2, (double)path.z) - path.y - 1;` seems not the right way... – four-eyes Mar 29 '18 at 14:41