Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 24th September 2009, 04:32 PM   #1
Default Dividing numbers in PIC C

Hi

I have (I think) a simple problem. I am using the C18 compiler to write code to be used on a pic 18f4620 but at the moment am only compiling the code and running it with MPSIM.

I need to divide 2 numbers , which will result in a floating point number. However when I try this with the following code and watch variable j it returns a value of 2.0, not 2.5.

double j;
unsigned char i, Byte0, Byte1, Byte2, Byte3, x, y, z;
unsigned long FTW, bit32, Fo, Fref;

void main (void)
{
y=5;
z=2;
j=y/z;
}

I obviously do not understand the way floats are used in Microchip C.

Could somebody give me some advise please?
Thanks
namtey is offline  
Old 24th September 2009, 04:48 PM   #2
Default

You have to make one of the vars a float, try, j=(double)y/(double)z;

Mike.
Pommie is online now  
Old 24th September 2009, 04:49 PM   #3
Default

im unfamiliar to the complier and pic's but im guessing you would to change "j" to a float variable

{
float a , b, j;
a = 5;
b = 2;

j = a/b;
}

I havnt coded for a long time, but im sure a double is classed as a int not a floating number.

if not could you not just shift ( << . >> ) your number to remove the decimal point and translate later (depends what your doing)

Last edited by Siksissk; 24th September 2009 at 04:50 PM.
Siksissk is offline  
Old 24th September 2009, 04:54 PM   #4
Default

Thanks for the help.

You were right, I had to make all the variables in the calculation of type float for it to work.

Not sure why this is the case though, but hey!
namtey is offline  
Old 24th September 2009, 04:58 PM   #5
Default

The compiler uses the simplest maths it can unless you force it to use floats.

Mike.
Pommie is online now  
Old 25th September 2009, 12:57 PM   #6
Default

In order to make your code more readable, you should make it obvious that you are dividing floats and not integers. In additional to changing the variable to floats, I suggest that you append a .0 to the end of the numbers to make it 100% obvious what you are doing. Yes, someone can look at variable declarations, or context, and figure it out, but spending a few seconds now may save minutes later.

Code:
double j, y, z;
unsigned char i, Byte0, Byte1, Byte2, Byte3, x;
unsigned long FTW, bit32, Fo, Fref;

void main (void)
{
   y=5.0;
   z=2.0;
   j=y/z;
}
__________________
A rectangular bear is just a polar bear after a coordinate transform. -- I dunno who.
A recent study shows that research causes cancer in rats. -- I dunno who said that one either.
Noggin is offline  
Old 25th September 2009, 01:19 PM   #7
Default

Quote:
Originally Posted by Noggin View Post
In order to make your code more readable, you should make it obvious that you are dividing floats and not integers. In additional to changing the variable to floats, I suggest that you append a .0 to the end of the numbers to make it 100% obvious what you are doing. Yes, someone can look at variable declarations, or context, and figure it out, but spending a few seconds now may save minutes later.

Code:
double j, y, z;
unsigned char i, Byte0, Byte1, Byte2, Byte3, x;
unsigned long FTW, bit32, Fo, Fref;

void main (void)
{
   y=5.0;
   z=2.0;
   j=y/z;
}
That is a Microsoft solution, bloated and unnecessary. Casting to floats makes it perfectly obvious what is being done.

Mike.
Pommie is online now  
Old 25th September 2009, 08:35 PM   #8
Default

Respectfully, I disagree.
__________________
A rectangular bear is just a polar bear after a coordinate transform. -- I dunno who.
A recent study shows that research causes cancer in rats. -- I dunno who said that one either.
Noggin is offline  
Old 25th September 2009, 11:03 PM   #9
Default

Code:
double j;
unsigned char  x, y, z;
 
void main (void)
{
    y=5;
    z=2;
    j=y/z;
}
There is no mystery here.

The compiler generates code that evaluates the RHS and then assigns it to the LHS.
The RHS is y/z and both are int so it evaluates to 2.
To assign int 2 to the LHS there is an implicit conversion to double and 2 becomes 2.0.

In general I do not like to use floats or doubles. Most of the time you can scale the numbers to avoid their use.

3v0
__________________
Please post questions to the forums. PM's are for personal communication.

BCHS/3v0's Tutorials
Junebug USB PIC programmer kit., USB Bit Whacker,
The 15 Minute Printed Circuit Board! (+drill time)

Last edited by 3v0; 25th September 2009 at 11:04 PM.
3v0 is offline  
Old 25th September 2009, 11:42 PM   #10
Default

Agree with not using floating point math as a general practice. Fixed point decimal is a commonplace replacement, lots of DSP's use it (5.23 and similar).

Last edited by speakerguy79; 25th September 2009 at 11:43 PM.
speakerguy79 is offline  
Old 26th September 2009, 07:47 PM   #11
Default

Quote:
Originally Posted by Noggin View Post
In order to make your code more readable, you should make it obvious that you are dividing floats and not integers. In additional to changing the variable to floats, I suggest that you append a .0 to the end of the numbers to make it 100% obvious what you are doing. Yes, someone can look at variable declarations, or context, and figure it out, but spending a few seconds now may save minutes later.

Code:
double j, y, z;
unsigned char i, Byte0, Byte1, Byte2, Byte3, x;
unsigned long FTW, bit32, Fo, Fref;

void main (void)
{
   y=5.0;
   z=2.0;
   j=y/z;
}
I understand where you are coming from with this...

But since everyone else says no heh... i suggest something similar...

Code:
double j, y, z;
unsigned char i, Byte0, Byte1, Byte2, Byte3, x;
unsigned long FTW, bit32, Fo, Fref;

void main (void)
{
   //y,z,j type = double
   y=5;
   z=2;
   j=y/z;
}
[/QUOTE]

This also doesnt alter any code so a small comment saves time so you dont have to jump to the top of code or another file to determine the type.
AtomSoft is offline  
Old 26th September 2009, 08:16 PM   #12
Default

I'm with 3v0 on this one. Using floats is poor practice in microcontrollers unless absolutely necessary.

Why not just scale y with a binary factor and keep the result scaled?
y = (5*256);
z = 2;
j = (y/z);
(that only needs unsigned int for y)

y = (5 * 16); etc
works ok for y 0-15, and might allow you still to use unsigned char for all vars, a massive saving.
Mr RB is online now  
Old 27th September 2009, 03:37 AM   #13
Default

I'm actually more curious as to if Pommie was saying that adding trailing 0's is the MS bloated solution or if he meant that using floats/doubles is the bloated solution. I use integer math every time I can, but there are a few times I had to use floats.

If you are going to use floats, I still say that you should put a '.0' to the number, but I do agree that floats should be avoided when possible.

Quote:
Originally Posted by Mr RB View Post
I'm with 3v0 on this one. Using floats is poor practice in microcontrollers unless absolutely necessary.

Why not just scale y with a binary factor and keep the result scaled?
y = (5*256);
z = 2;
j = (y/z);
(that only needs unsigned int for y)

y = (5 * 16); etc
works ok for y 0-15, and might allow you still to use unsigned char for all vars, a massive saving.
I don't really like using the *256 or *16, I prefer using *10 or *100 as it makes it much easier to debug. I understand the benefits of using the 256 or 16 as you can do it as shifts, however, if it is scaled by 10 it takes no math to debug. Just insert a break point and look at the variable. Don't need a calculator to figure out what the value is. However, I work in PIC24, PIN32, and ARM7 the vast majority of the time and the only thing I have to do is make sure that a function executes in 10mS or less and I'm golden.
__________________
A rectangular bear is just a polar bear after a coordinate transform. -- I dunno who.
A recent study shows that research causes cancer in rats. -- I dunno who said that one either.

Last edited by Noggin; 27th September 2009 at 03:43 AM.
Noggin is offline  
Old 27th September 2009, 06:23 AM   #14
Default

I was saying that making all variables floats was a bloated way to achieve the desired outcome. In the example given, j needs to be a float, the others could be chars. Also, casting to double makes it extra obvious what is being done.

Mike.
Pommie is online now  
Old 27th September 2009, 06:36 AM   #15
Default hex to bcd conversion

hellooooooo
can anybody help to explain me the logic behind hex to bcd conversion..
m using pic16f887..
i m having the logic : adding 06 to hex number and if carry out of lower nibble does not occur then add FA ..then add 60 to hex no., if carry out of upper nibble does not occur then add A0 to it..

this logic is working with some of the hex numbers and not working for many other numbers like AA, BA...etc..

so can nybody help from this forum..m new to this forum...

thanx in advance






Quote:
Originally Posted by namtey View Post
Hi

I have (I think) a simple problem. I am using the C18 compiler to write code to be used on a pic 18f4620 but at the moment am only compiling the code and running it with MPSIM.

I need to divide 2 numbers , which will result in a floating point number. However when I try this with the following code and watch variable j it returns a value of 2.0, not 2.5.

double j;
unsigned char i, Byte0, Byte1, Byte2, Byte3, x, y, z;
unsigned long FTW, bit32, Fo, Fref;

void main (void)
{
y=5;
z=2;
j=y/z;
}

I obviously do not understand the way floats are used in Microchip C.

Could somebody give me some advise please?
Thanks
JAISHREE_KHATAR is offline  
Reply

Tags
dividing, numbers, pic

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
PIC ASM - Summation two 16 bit numbers (16f84) byrusber Micro Controllers 10 28th October 2007 09:41 PM
Random Numbers for PIC PDubya Micro Controllers 12 8th July 2006 11:01 PM
Voltage Dividing Networks Trevor Rymell Electronic Projects Design/Ideas/Reviews 19 12th April 2006 02:34 PM
Inputting 16 bit numbers into a 16 series pic bryan1 Micro Controllers 3 10th November 2005 08:31 AM
Delaying digital signal (frequency dividing) onder Electronic Projects Design/Ideas/Reviews 3 8th March 2004 12:45 AM



All times are GMT. The time now is 05:54 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker