Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to Calculate Quotient and Remainder in Embedded

Status
Not open for further replies.

jsureshp

Junior Member level 3
Joined
Jul 20, 2010
Messages
26
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Location
Chennai
Activity points
1,492
Hi......
I want to calculate the Quotient and remainder in embedded c.
please anybody give the formula for the Mathematical calculation...
Please provide that formula with example
that will help to easily to understand..............
 

Hello!

Here are the 2 ints to process: a, b
Here is the quotient and remainder: q, r

What about this:
q = a / b;
r = a % b;

And for the second line, here is a variant:

r = a - b * q;

Dora.
 

Hi
Thanks for Reply............


I can't understand the following line...

And for the second line, here is a variant:

r = a - b * q;

can you explain with example.... please...
for example a=45
b=10;
then
Quotient q=45/10=4.5 But here the quotient is floating point... is our embedded system will take only 4 ? or 4.5....
That is my dought

Remainder r=45 % 10= 5
 

Hi,
This means that
Code:
r = a % b;
can also be written as
Code:
r = a - b * q;
You should use
Code:
r = a % b;
as it is easier to understand when you go through the code.

Explaining the statement
Code:
r = a - b * q;
:
Let's take your example, so a = 45, b = 10
Code:
q = a / b;
q=45/10 = 4
So, a-b*q = a-(b*q) = 45-(10*4) = 45 - 40 = 5

So, q stores 4 - the quotient,
r stores 5 - the remainder.

Hope this helps.
Tahmid.
 

Hello!

Quotient q=45/10=4.5

If you use float numbers, then you will never get a remainder.
That's why I assumed that you were talking about integer numbers.

In C, if you divide 2 integers, then you will get the integral part only.
Not only in embedded C, just try it in plain C on Windows, you will get the
same result. With your numbers:

int a, b, q, r;

a = 45;
b = 10;

q = a/b; // At this point you get q = 4

r = a % b; // Modulus operation gives you the remainder.

And another way to "manually" calculate it is:

r = a - b * q;
With the same numbers:

r = 45 - (4 * 10) = 5;

Does it make sense now?

Dora.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top