Remainder (Modulus, Modulo)


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2009
    Posts
    19

    Default Remainder (Modulus, Modulo)

    Can someone give me a hand understanding how to use the remainder (Modulus) operator properly.

    For instance I'm trying to divide 480 by 148. On my calculator I get 3.24.

    When I try to use // in something like this I get 36 instead of 24:

    Code:
    Num_1 var WORD
    Num_2 var WORD
    Int_part var WORD
    Fract_part var WORD
    
    Num_1 = 480 : Num_2 = 148
    Int_part = Num_1 / Num_2 
    Fract_part = Num_1 // Num_2
    the variable Fract_part doesn't have the 24 I was expecting, it contains 36 ...?

    (BTW; I'm trying to work around floating point math on a 16F micro)

  2. #2
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Do you remember way back when, the teacher wouldn't let you use a calculator, and you were allowed to stop with a fraction, instead of having to solve for x decimal points? I do, but I am old. Anyway, the remainder is just that, a remainder. If you want a decimal, you will have to do some more work.

    148 goes into 480 3 times, but there are 36 left over.

  3. #3
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default

    The answers are correct. Surprised? Here's how it works

    480/148 = 3 in integer math.
    = 3.24 in fractional math

    480 mod 148 = 36 how?
    480 - (148*3) = 36

    Then you might ask, what is the 0.24 and 36.

    When you multiply 0.24 by the divisor 148, you get the fraction that remains 36.

    If you want to do fixed place math, multiply the upper number by 100 and you will get a result that has the additional 2 digits and you put the decimal after dividing by 100.

    eg : 480 / 148 now becomes (480*100)/148
    result will be 324. This is actually 100 times greater because we multiplied by 100. So, while printing the value, you print 324/100, ".", 324 mod 100 to get the proper value of 3.24

    You might find more ports out here explaining flxed point math - search

  4. #4
    Join Date
    Nov 2009
    Posts
    19


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Jerson View Post
    The answers are correct. Surprised? Here's how it works

    480/148 = 3 in integer math.
    = 3.24 in fractional math

    480 mod 148 = 36 how?
    480 - (148*3) = 36

    Then you might ask, what is the 0.24 and 36.

    When you multiply 0.24 by the divisor 148, you get the fraction that remains 36.

    If you want to do fixed place math, multiply the upper number by 100 and you will get a result that has the additional 2 digits and you put the decimal after dividing by 100.

    eg : 480 / 148 now becomes (480*100)/148
    result will be 324. This is actually 100 times greater because we multiplied by 100. So, while printing the value, you print 324/100, ".", 324 mod 100 to get the proper value of 3.24

    You might find more ports out here explaining flxed point math - search
    Ahhh that makes some sense, thank you.

Similar Threads

  1. Optimizing DIV
    By skimask in forum mel PIC BASIC Pro
    Replies: 41
    Last Post: - 22nd September 2008, 04:58
  2. Modulus Question
    By DynamoBen in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd March 2006, 01:53
  3. GPS waypoint distance in Km
    By Art in forum Code Examples
    Replies: 1
    Last Post: - 28th August 2005, 01:59
  4. Mathematics and remainder for PLL MC145158
    By RFsolution in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 24th August 2005, 04:32
  5. Retrieving Div32 Remainder
    By Darrel Taylor in forum Code Examples
    Replies: 4
    Last Post: - 20th August 2003, 03:53

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts