% (modulo) operator only works on int and long data types in Objective C 2.0

Today, I tried to use a modulo % operator in a function in Objective C 2.0 like so: (Modulo operator finds the remainder of division of one number by another)

-(double) someFunction: (double) value {
  ...
  if (value % 90 == 0)
    return 0;
  ...
}

When I compiled my code, XCode generated and error saying that % function cannot be used for double values. I googled my way on to this stackoverflow question (isn’t it amazing that for almost every problem you have, stackoverflow.com has an answer to your question…), where one of the ways of getting remainder of division where one or both numbers are double is to use c function fmod(). So I rewrote my function and XCode compiled without a problem…

-(double) someFunction: (double) value {
  ...
  if (fmod(value,90) == 0)
    return 0;
  ...
}
Advertisements

,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: