CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

float operation not working on 16 bit parts

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
n-squared



Joined: 03 Oct 2006
Posts: 99

View user's profile Send private message Send e-mail Visit poster's website

float operation not working on 16 bit parts
PostPosted: Mon Jun 03, 2013 1:06 am     Reply with quote

Hi
I am trying to do simple float arithmetic on PIC24 parts.
Nothing seems to work right.

I am using PCD V4.140 (tried also V4.137).
MCU is PIC24FJ128GA106 (I also tried other parts).
MPLAB V8.86 and PCWHD 4.140 show the same garbage for data.

Attached is a sample code.


Code:


#include <24FJ128GA106.h>
#device adc=8
#device ICD=2
#device pass_strings=in_ram

#FUSES WDT
#FUSES WPOSTS16                //Watch Dog Timer PostScalar 1:8192
#FUSES ICSP2                   //ICD uses PGC2/PGD2 pins
#FUSES CKSFSM                  //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES FRC_PLL
#FUSES OSCIO

#define SYSTEM_SPEED 32000000
//#use delay(clock=SYSTEM_SPEED,restart_wdt)
#use delay(internal=8MHZ, clock=SYSTEM_SPEED)


#include <stdlib.h>

void main() {
   long int l;
   float a, b;
   int8 buf[80];
   a = 1.0;
   b = 2.1;
   while(TRUE) {
     
      sprintf(buf,"\r\n\nA= %E\r\n", a);
      sprintf(buf,"B= %E\r\n", b);
      sprintf(buf,"\r\na + b = %E", a + b);
      sprintf(buf,"\r\na - b = %E", a - b);
      sprintf(buf,"\r\na * b = %E", a * b);
      sprintf(buf,"\r\na / b = %E\r\n", a / b);
      a += 0.1;
      b -= 0.1;
   }
}



Anybody has an idea?

Thanks
Noam
_________________
Every solution has a problem.
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Mon Jun 03, 2013 1:30 am     Reply with quote

Obvious comment, with the watchdog timer enabled, and not reset anywhere, it is going to restart at some point and give garbage....
Even with the 'restart' in delays, you'd get the same, since you don't use any delays.
However generally, you may just be getting exactly what you'd expect from floats. Load the Pconvert tool. type in 2.1, into the IEEEfloat32 window. The note the hex bytes, and type these back into the hex window. What gets displayed in the IEEE window?. 2.1, can't exactly be represented by a standard float. 2.0999999 is the closest the format can give. This is why when you output such numbers you'd normally round. This is also why if you want to do accurate arithmetic for money etc., you should always use scaled integers rather than floats.....

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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