|
|
View previous topic :: View next topic |
Author |
Message |
Fusillade
Joined: 02 Aug 2007 Posts: 31
|
Floating Point Division |
Posted: Wed Aug 22, 2007 4:11 pm |
|
|
Compiler Version: 4.047
Device: 18F2520
I'm new to the CCS compiler. I've been reading through the forum for about an hour to see if this is covered but I can not find the issue. I wanted to test the capabilities of floating point division of the compiler and PIC. I set up the following scenario:
float f_result;
float f_numerator;
float f_denominator;
f_numerator = 10.0;
f_denominator = 10.0;
f_result = 10.0 / 10.0;
f_result = f_numerator/10.0;
f_result = 10.0/f_denominator;
f_result = f_numerator/f_denominator;
When I step through the program, f_numerator and f_denominator are initialized to 10.0000000. The first division results in f_result having the value of 1.00000000. The next division gives me an error and the value is "out of scope".
I'm assuming the complier performed the pre-calculation of the 10.0/10.0 so it was not handled by the device. The second division was the first one performed by the device and it failed.
I checked the EX_FLOAT.C example and they define:
float a, b;
then perform:
printf("\r\na / b = %E\r\n", a / b);
Must floating point division be handled in another manner? Is this just a problem when stepping through the program?
Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 22, 2007 10:24 pm |
|
|
You didn't say what tool you're using to step through the code,
so I assume you're using MPLAB.
I installed PCH vs. 4.047 and compiled the test program shown below.
I then stepped through the code by pressing the F8 key, while observing
the C source code window. The cursor stopped on each line, and then
advanced to the next line, each time I pressed the F8 key. There were
no problems. I used MPLAB vs. 7.41, and I used the Configure menu
to select 18F2520 as the device before I compiled. I also used the
Debugger menu to select MPLAB SIM as the debugger.
I setup a Watch Window, and selected f_result as the variable to
watch. I right-clicked on Properties and made sure that the format
for f_result was 'MCHP Float'. Once I started stepping through the
code, it always gave the result as 1.00000000, and never said 'out of
scope'. It only said that at the beginning, before I started stepping.
Here is the test program:
Code: |
#include <18F2520.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
//=================================
void main()
{
float f_result;
float f_numerator;
float f_denominator;
f_numerator = 10.0;
f_denominator = 10.0;
f_result = 10.0 / 10.0;
f_result = f_numerator/10.0;
f_result = 10.0/f_denominator;
while(1);
} |
|
|
|
Fusillade
Joined: 02 Aug 2007 Posts: 31
|
|
Posted: Thu Aug 23, 2007 7:21 am |
|
|
Yes, I am using MPLAB v7.60.
To make all things equal, I copied your program and I still experienced the same problem. Then I noticed something in your post that made me think of something else.
From the MPLAB manual:
Quote: | Step Into
Single step through program code.
For assembly code, this command executes one instruction (single or multiple cycle instructions) and then halts. After execution of one instruction, all the windows are updated.
For C code, this command executes one line of C code, which may mean the execution of one or more assembly instructions, and then halts. After execution, all the windows are updated.
Step Over
Execute the instruction at the current program counter location. At a CALL instruction, Step Over executes the called subroutine and halts at the address following the CALL. If the Step Over is too long or appears to hang, click Halt. |
I was using Step Into (F7) instead of Step Over (F8). Given the MPLAB description, it should execute the line:
Quote: | f_result = f_numerator/10.0; |
and return to the next line in the program; however, it seems as if the program is stepping into the division operator which appears to be treated as a high level language macro by CCP(?). If this is the case, what other common operators will produce the same results? |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|