Objective-C help please (newby explanation needed :))

by newbim
7 replies
Hi. I'm reading a "For Dummies" book on Obj-C and it's talking about modulus. It quotes an example more than once:

"2 divided by 3 equals 2" ????

It does it again. Im not a math genius, and I know in context (where the type is an int and therefore a whole number) but 2 / 3 = 0.666667, not 2.

Can someone explain the simple math to me, I've a feeling I'm going to feel very stupid when I see the explanation, lol.

Cheers,

Newbim.
#explanation #needed #newby #objectivec
  • Profile picture of the author 723Media
    Not sure what they are saying without context but the modulus operator will find the remainder.

    It's often used to set up a condition for something like creating 2 columns.

    You loop through a result set using a variable, say "i".

    If modulus i is divisible by 2, data goes into one column, else it goes into another.
    {{ DiscussionBoard.errors[4249596].message }}
    • Profile picture of the author newbim
      Thanks for that.

      It was talking about modulus and mathmatical functions.

      This is how they use it:

      a = 2
      b = 3

      c = a % b;
      NSLog (@"a %% b = %i", c);

      Results in:

      a % b = 2

      -- I can understand how b % a = 1, because the remainder modulus is 1, but I don't get it the other way round because 3 doesn't go into two at all.

      I know it's probably a really simple thing but Ive been trying to figure it for three days now, need a fresh pair of eyes on it. Haha.


      Thanks.
      Signature
      If what I said helps, let me know, throw me a 'thanks'.
      {{ DiscussionBoard.errors[4258672].message }}
  • Profile picture of the author ashbeats
    Mod as a formula actually works out to:
    a - (b * (a \ b))

    but i still don't know why. But it works.
    {{ DiscussionBoard.errors[4263570].message }}
  • Profile picture of the author Toby Couchman
    Hey Newbim,

    This isn't an answer to your specific question but it might help for your overall knowledge of obj c.

    There are some awesome videos on youtube by the user 'TheNewBoston'. Just search for that user. Freakin awesome tutorials.

    Let me know if it helped
    MB
    {{ DiscussionBoard.errors[4270934].message }}
    • Profile picture of the author ohio1975
      the result of the mod operator is the "left over". whatever you have left over is the answer. 3 goes into 2 zero times, with 2 "left over".

      when i first learned about this operator, i always told myself "left over" (as was mentioned earlier).
      {{ DiscussionBoard.errors[4271722].message }}
      • Profile picture of the author newbim
        Hi everyone.

        Thanks for all the assistance - it has really helped.

        I think I get it now. So the two 'left over' is the original 2 that we were trying to divide into 3... if that makes sense?? haha.

        I know may have seemed like a silly question, but I REFUSE TO LET THE COMPUTERS BEAT ME, haha.

        Thanks again, as always, the warrior community has come through!!

        Cheers,

        Andy. .
        Signature
        If what I said helps, let me know, throw me a 'thanks'.
        {{ DiscussionBoard.errors[4274622].message }}

Trending Topics