![]() | ![]() | ![]() |
| |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
![]() |
| | Tools |
| | #1 |
|
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 | |
| |
| | #2 |
|
You have to make one of the vars a float, try, j=(double)y/(double)z; Mike. | |
| |
| | #3 |
|
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. | |
| |
| | #4 |
|
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! | |
| |
| | #5 |
|
The compiler uses the simplest maths it can unless you force it to use floats. Mike. | |
| |
| | #6 |
|
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. | |
| |
| | #7 | |
| Quote:
Mike. | ||
| |
| | #8 |
|
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. | |
| |
| | #9 |
| Code: double j;
unsigned char x, y, z;
void main (void)
{
y=5;
z=2;
j=y/z;
}
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. | |
| |
| | #10 |
|
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. | |
| |
| | #11 | |
| Quote:
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;
}
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.
__________________ AtomSofts eBay Store AtomSoftTech: C18 TIPS & TRICKS v9 PDF Nokia 6100 Driver/Software My Name: Jason Lopez http://atomsofttech.info/ | My YouTube Videos! My Favorite Store: dipmicro Electronics | ||
| |
| | #12 |
|
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. | |
| |
| | #13 | |
|
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:
__________________ 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. | ||
| |
| | #14 |
|
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. | |
| |
| | #15 | |
|
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:
| ||
| |
|
| 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 |