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 1 div by 2 == 0 ??

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



Joined: 30 Oct 2010
Posts: 3

View user's profile Send private message

float 1 div by 2 == 0 ??
PostPosted: Mon May 28, 2012 4:09 pm     Reply with quote

Very single program running on pic18f452

This displays 0.00 on my debug... why??

Code:

void main()
{
float div_result;
int part1;
int part2;

part1 = 1;
part2 = 2;

div_result = part1 / part2;

fprintf(usart_debug,"Ini=%f\r\n",div_result);


   while(1){//loop forever

   }

}

dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Mon May 28, 2012 4:19 pm     Reply with quote

Try this...

div_result = (float) part1 / part2;
_________________
Google and Forum Search are some of your best tools!!!!
pozzari



Joined: 30 Oct 2010
Posts: 3

View user's profile Send private message

PostPosted: Mon May 28, 2012 4:59 pm     Reply with quote

div_result = (float)part1 / (float)part2 worked fine... thanks a lot
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Tue May 29, 2012 1:19 am     Reply with quote

Pozzari, do you understand why that worked?

RF Developer
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Tue May 29, 2012 1:32 am     Reply with quote

Worth also explaining 'why'.
In C, the maths operations, when compiled, look at the 'types' of the value/values _involved in each part of the calculation_ (not the result), and select the maths 'type', required by the 'highest type' of these. Types are ordered:
Code:

Float, signed int32, int32, signed int16, int16, signed int8, int8.

So in the original sum, the compiler says "int8/int8, use int8 arithmetic".

Only one of the values (either), actually needs to be 'cast' to the higher type, since the highest type involved is selected. So:
Code:

div_result = (float)part1 / part2;
div_result = part1 / (float)part2;
div_result = (float)part1 / (float)part2;

will all give the same result. Once one value in the sum is raised to the higher type, the others are automatically raised as needed.

Now, this is very useful, since it means you can elect to use 'lower' maths types (which are much faster/smaller), until a higher type is needed.

So (for instance),
Code:

result = (int16)part1*part2;

Uses int16 arithmetic to multiply the two int8 values - takes about 17 instructions, versus 180 for the 'float' version.
Then:
Code:

result=((int16)part1*part2)/(float)part3;

Uses int16 for the multiplication, and then switches to float for the division. Still saving perhaps 150 instructions.....

Historically, C was written to do this, since it allowed efficient use of the chip's resources. However on things like the PC, with the addition of a hardware maths co-processor, this has become slightly 'bypassed', with all the sums using this hardware feature, and therefore not worrying about the extra performance involved....

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