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.

[SOLVED] Strange Problem in Keil?

Status
Not open for further replies.

dariush_abbasi

Member level 3
Joined
Oct 2, 2010
Messages
61
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,664
Hi dear friends.
I have a strange problem :
when i use this code :
Code:
int Whole = 0;
int On = 0;
int Percent = 0;
int Temp ;

   On = 42;
   Whole = 100;
   Percent   = (On*100) / Whole;
   Temp = Percent;
The LCD shows 42 which is normal.
but using this code:
Code:
int Whole = 0;
int On = 0;
int Percent = 0;
int Temp ;

   On = 42;
   Whole = 100;
   Percent   = (On / Whole)*100;
   Temp = Percent;
The LCD shows 00 which i can't figure out why!
 

dariush_abbasi said:
The LCD shows 00 which i can't figure out why!

case 1:
(On*100)/Whole = (42*100)/100 = 4200/100 = 42

case 2:
(On/Whole)*100 = (42/100)*100 = 0*100 = 0

You are not using float variables and the intigers are not capable of holding floating point values. No approximation takes place. All the floating points are going bye bye. Even if the result was 0.999999, you would get 0 as well. This is how C works.


Hope that helped,
Alexandros
 
Percent = (On / Whole)*100;
First (On/Whole) is evaluated. Since all elements and result are integer, result of division is rounded 42/100=>0 and 0*100=0
In firs case On*100=42*100=4200 and 4200/100 =42
 
I have a strange problem :
when i use this code :


Code:
int Whole = 0;
int On = 0;
int Percent = 0;
int Temp ;

   On = 42;
   Whole = 100;
   Percent   = (On / Whole)*100;
   Temp = Percent;

The LCD shows 00 which i can't figure out why!

The result displayed, 00, is the expected result.

The issue is the following line:

Code:
Percent   = (On / Whole)*100;

If you evaluate it in order of precedence:

On / Whole = 42 / 100 = 0

0 * 100 = 0

Therefore Temp = Percent = 0, such is the case with integer math. Of course if either the variable "On" or "Whole" were floating point, then the result would be:

Temp = Percent = 42

BigDog
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top