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

Printf a negative float number

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







Printf a negative float number
PostPosted: Mon Feb 02, 2004 11:25 pm     Reply with quote

I can't get to send the correct value to the terminal. I'm expecting a value of -25.0625. This is based on the value from a DS1631 of 0xE6F0. Instead, I'm getting a value of 24.9999. Debugging it seems to indicate that this value does not include the fractional part. The other problem is how I can get to print a negative without resorting to concatenating the value with a negative symbol.

I can't also get the fractional part to be output.

I've used a constant value of 0xE6F0 for demonstration. Also, the code has been expanded just to make it clear to me whilst debugging so I can see what happens to each register when stepping through.

What have I done wrong?



#include <16f877.h>
#device ICD=TRUE
#fuses HS, NOLVP, NOWDT, PUT
#use delay(clock=20000000)
#use rs232(DEBUGGER)


signed long test_temp()
{
int datal, datah, x, y;
signed long data = 0;
float z = 0.00;

datah=0xE6;
datal=0xF0;

//process the two's complement...it's a negative
data = datah;
data = (data<<8) | datal;
data = ~data;
data += 1;

//get the whole portion
x = data>>12;
x *= 16;
y = data>>8 & 0b00001111;

//get the fractional part
z = (float)(data>>4 & 0b000000001111);
z /= 16.;

//get the result
data = x + y + z;

return(data);
}


main()
{
signed long value;

do{
delay_ms(1000);
value = test_temp();
printf("%f\r\n", (float)value);
}while(1);
}
Charlie U



Joined: 09 Sep 2003
Posts: 183
Location: Somewhere under water in the Great Lakes

View user's profile Send private message

PostPosted: Tue Feb 03, 2004 7:46 am     Reply with quote

By the time you are done with your function, you are returning a signed long int. This does not have a fractional part.

I haven't had a chance to verify this, but my first thought is that you loose your sign when you shift a signed number. As a test of the printf function, couldn't you first divide your signed long data by 16, which should preserve the sign:

data = data/16;

then cast the result to a float and multiply by 0.0625 and print the result?

return ((float)data*0.0625);

This would require returning a float from your function, but it would allow you to verify the printf question.
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