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 support@ccsinfo.com

Float display problem (only 2 digits)

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



Joined: 09 May 2010
Posts: 2

View user's profile Send private message

Float display problem (only 2 digits)
PostPosted: Sun May 09, 2010 3:59 am     Reply with quote

Hi,

When I try to display a float I get only the first 2 digits after the comma...

I am actually working on a PIC24H with PCWHD 4.104

exemple:

Code:

void main()
{
   float temp1=0.00001,temp2=10000,temp3;
   setup_spi( FALSE );
   setup_spi2( FALSE );
   setup_wdt(WDT_OFF);

   setup_timer1(TMR_INTERNAL|TMR_DIV_BY_8);
   setup_timer2(TMR_INTERNAL |TMR_DIV_BY_8 ,0xFFFF);

   while(1)
   {
   temp3=temp1*temp2;
   fprintf(PC,"***test:  temp1:%g   temp2:%g   temp3:%f\r\n",temp1,temp2,temp3);
   
   
   delay_ms(500);
   }
}


and here are the fuses:

Code:

#include<24HJ128GP202.h>
#build(stack = 256)

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOBSS                    //No boot segment
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOWRT                    //Program memory not write protected
#FUSES PR_PLL                   //Primary Oscillator with PLL
#FUSES NOCKSFSM                 //Clock Switching is disabled, fail Safe clock monitor is disabled
#FUSES NOOSCIO                  //OSC2 is clock output
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES WINDIS                   //Watch Dog Timer in non-Window mode
#FUSES WPRES128                 //Watch Dog Timer PreScalar 1:128
#FUSES WPOSTS16                 //Watch Dog Timer PostScalar 1:32768
#FUSES PUT128                   //Power On Reset Timer value 128ms
#FUSES IOL1WAY                  //Allows only one reconfiguration of peripheral pins
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES NORSS                    //No secure segment RAM
#FUSES NOSSS                    //No secure segment
#FUSES NOWRTSS                  //Secure segment not write protected
#FUSES NORBS                    //No Boot RAM defined
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOCOE                    //Device will reset into operational mode
#FUSES NOJTAG                   //JTAG disabled
#FUSES ICSP2                    //ICD uses PGC2/PGD2 pins
#FUSES NOALTI2C                 //I2C mapped to SDA1/SCL1 pins

#use delay(clock=80M, oscillator=10M)

#pin_select U2RX=PIN_B5
#pin_select U2TX=PIN_B1
#use rs232(UART2,baud=115200,parity=N,bits=8,stream=PC)



And I get the result (on the Hyperterminal):

Quote:

***test: temp1:0.00 temp2:10000.00 temp3:0.10
***test: temp1:0.00 temp2:10000.00 temp3:0.10
***test: temp1:0.00 temp2:10000.00 temp3:0.10


The problem seems to be the same with sprintf()...:( Any Ideas?

Thanks,
Thomas
jbmiller



Joined: 07 Oct 2006
Posts: 73
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun May 09, 2010 5:48 am     Reply with quote

According to my Aug 2009 manual in the printf() function details..

f means Float with truncated decimal
g means Float with rounded decimal

So I think the default value for rounding and truncation is 2 decimal points.

Others will know for sure how to define # of places after decimal you want


Jay
cdlaweed



Joined: 09 May 2010
Posts: 2

View user's profile Send private message

PostPosted: Sun May 09, 2010 10:04 am     Reply with quote

Thanks for your reply, you a right I have to specify the number of digits in the printf.... :(
with the syntax "%x.xf" everything works perfectly.
sshahryiar



Joined: 05 May 2010
Posts: 94
Location: Dhaka, Bangladesh

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

PostPosted: Wed May 12, 2010 2:41 am     Reply with quote

void main()
{
float temp1=0.00001,temp2=10000,temp3;
setup_spi( FALSE );
setup_spi2( FALSE );
setup_wdt(WDT_OFF);

setup_timer1(TMR_INTERNAL|TMR_DIV_BY_8);
setup_timer2(TMR_INTERNAL |TMR_DIV_BY_8 ,0xFFFF);

while(1)
{
temp3=temp1*temp2;
fprintf(PC,"***test: temp1:%3.3g temp2:%4.1g temp3:%2.4f\r\n",temp1,temp2,temp3);


delay_ms(500);
}
}


Try this code.
_________________
https://www.facebook.com/MicroArena

SShahryiar
Ttelmah



Joined: 11 Mar 2010
Posts: 19348

View user's profile Send private message

PostPosted: Wed May 12, 2010 3:22 am     Reply with quote

This is actually a 'fix', that seems to have introduced some oddities...

Historically, if you selected '%f' without any width/DP specification, on some numbers, problems appeared with the output. Hence you will find quite a few posts here advising to always use a width specification. CCS appear to have fixed the old problem by applying a 'default' DP value, if you don't specify one. There are also some other 'hidden' default switches taking place.

If you specify a minimum overall width for the output, so (for example):
printf("%1f",val);

The code will switch to behaving almost as it 'used' to do, and (for example), a value of 0.000012, will output as "0.000012". However you will then see an odd behaviour, if you send a large number, to it, with a lot of leading spaces being added, taking the total width output to 16 characters.

If instead you specifiy a width that is 'reasonable', like:
printf("%7f",val);

It'll work normally and correctly, till the number of leading digits exceeds the maximum width specified. At this point, it switches to outputing 16 characters again....

Provided you 'know' the maximum number of leading characters you will have, then specifying just a width is easy, and works.

Overall, a problem to 'watch out for'....

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