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

negative floating values

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







negative floating values
PostPosted: Wed Apr 04, 2007 12:20 pm     Reply with quote

hi every one

i have a problem in displaying negative float values...
i am using a Graphic LCD with 160x128 pixels and t6963 controller.
i am displaying a graph with it is a cursor which displays both the values of x and y axis
y axis is no problem as it is a long int and all positive values
x- axis has to be scalled and the resultant values are float
so here is my code...

Code:

if( !SW3 )
{
x=x-1;// to move cursor left
remove_cursor(x+1,y);// remove old cousor .. i have a routine
cursor(x,y);// place cursor in new position
sprintf( array, "%3.2f",(x-19)/4.0 );//the x-axis value is scalled and string made
glcd_text35( 110,9 , array, 1 );// display the vale at my described location
}


now the problem is when value of x falls bellow 19 it should display a negative value instead it is displaying a positive value
that is

63.74 it should show -0.74
63.49 it should show -0.49
63.24 it should show -0.24

any suggestions or help from an expert i want to display negative values
Ttelmah
Guest







PostPosted: Wed Apr 04, 2007 12:44 pm     Reply with quote

'x' is presumably declared as an integer. Hence it is by default 'unsigned'. The arithmetic (x-19), is performed in integer (since both values are integers). Hence it wraps, giving a positive number 256 greater than the right value. You can correct this a whole number of ways:

(x-19.0) will force this arithmetic to use a 'float' type.

((signed)x-19) will convert x to a signed type, and use signed arithmetic.

((float)x-19) will convert x to a float then perform the arithmetic.

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