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

displaying float value

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



Joined: 08 Apr 2009
Posts: 100
Location: Chennai, India

View user's profile Send private message

displaying float value
PostPosted: Wed Aug 12, 2009 8:26 am     Reply with quote

Dear board, this is my code: When I run this I should get the value of 0.25 but when I compile this code in the PIC simulator IDE I get 0.00 in the USART screen.

My ccs compiler version is V4.023.

I tried like this " s=2.25" and printf. then it is displaying correctly as 2.25.
Code:
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=14000000)
#include <math.h>
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)


void main(){

int i;
float s;
long mylong;
char mystring[4];
int line1[] = " ADC   ";
int line2[] = "light intensity ";

while(1){
            s=25/100;
            printf("\n the float value is : %f", s);
        }
}

Did I make any mistake here ? Please help me actually I am working for PWM LED dimming.

I want to display % value in the LCD screen.

The formula for this is ((ADCdata/255)*100) gives 0-100% LED brightness.

How to program to work this MATH data?
Ttelmah
Guest







PostPosted: Wed Aug 12, 2009 9:01 am     Reply with quote

The problem here, is numeric types.
'25' is an integer.
'100' is an integer.

25/100, using integer maths, gives '0'.

In C (not just CCS), the 'type' used for an operation, is the highest 'type' of the values involved. So, if you use:

25.0/100

25/100.0

or

25(float)/100

25/100(float)

you will get the right result. In each case, you are making one of the values into a 'float', so float arithmetic is used.

While this also applies in 'C' in general, CCS is 'stricter', than is normally the case. This is deliberate, to avoid wasted time (float arithmetic is much slower than integer), but on most modern compilers, there is implicit propogation of things like a constant evaluation as shown, to float by default...

Best Wishes
mutthunaveen



Joined: 08 Apr 2009
Posts: 100
Location: Chennai, India

View user's profile Send private message

thanks
PostPosted: Wed Aug 12, 2009 10:31 pm     Reply with quote

thank u Ttelmah

u r right..... Very Happy

I defined all the variables to float then the float division is working.
Code:

float a,b=2.25,c=3.33;
a=b/c;
printf("\n the a = %f", a);


This is working fine...... but the system working is slow.

Any idea to boost the time taken to calculate.


If you have time clarify this too..... what I have to do for the coding to get the float value like this (22.023621) after dot more than 2 values. Smile
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Thu Aug 13, 2009 7:27 am     Reply with quote

Look up the options for the printf() format string. That is where you determine how many digits to print.

Better yet, if you want speed, avoid floats entirely. If you need an answer to 1.234 digits then calculate integer 1/1000's ( i.e. 1234/1000) then use string functions to insert a decimal point where needed. A few years ago I did a system to measure ships. All the PIC math was done in 24 bit integer millimeters. At the very end I inserted a decimal to send lengths in meters to a PC.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Ttelmah
Guest







PostPosted: Thu Aug 13, 2009 7:46 am     Reply with quote

Or, assuming a reasonably recent compiler, look at %w in the printf.

So (for example):

Code:

#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=14000000)
#include <math.h>
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)


void main(){
   int i;
   int32 s;

   while(1){
      s=2500/100;
      printf("\n the float value is : %5.2w", s);
   }
}

Float is slow.
However, it'll take about 30mSec, to print the output, and only about 5mSec to do the calculation involved.....

The limiting factor, in what is posted, will be the serial, not the calculation.

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