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

Weird Division [SOLVED]

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



Joined: 13 Aug 2004
Posts: 58
Location: Turkey

View user's profile Send private message Visit poster's website

Weird Division [SOLVED]
PostPosted: Wed Dec 02, 2015 8:08 am     Reply with quote

hi,
what could be the problem ?
ccs compiler ver 4.135
PIC18F45K80

Code:

signed int32 ldvalue;
float scalefactor;
signed int32 setload=100;

ldvalue=214001;
            printf("ldvalue=%Ld",ldvalue);
            scalefactor=(float)ldvalue/setload;
            printf("scalefactor= %.6f \r\n",scalefactor);



the result shown on the terminal is a negative number

interesting
is it a printf error?
_________________
Dr Suleyman CANAN
R&D Electronic Engineer
https://suleymancanan.wordpress.com

Do whatever you do with amateur spirit -
But always feel professional.


Last edited by scanan on Fri Dec 04, 2015 2:11 pm; edited 2 times in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19492

View user's profile Send private message

PostPosted: Wed Dec 02, 2015 8:30 am     Reply with quote

First, _what compiler version_.
This is one of the key things that must be said for any problem like this.
However, printf on a lot of CCS versions, gives 'unusual' results, if it is used without a width limit. Try with %8.6f.
scanan



Joined: 13 Aug 2004
Posts: 58
Location: Turkey

View user's profile Send private message Visit poster's website

PostPosted: Wed Dec 02, 2015 10:42 am     Reply with quote

Ttelmah wrote:
First, _what compiler version_.
This is one of the key things that must be said for any problem like this.
However, printf on a lot of CCS versions, gives 'unusual' results, if it is used without a width limit. Try with %8.6f.


compiler version ccs 4.135
PIC18F45K80
_________________
Dr Suleyman CANAN
R&D Electronic Engineer
https://suleymancanan.wordpress.com

Do whatever you do with amateur spirit -
But always feel professional.
Ttelmah



Joined: 11 Mar 2010
Posts: 19492

View user's profile Send private message

PostPosted: Thu Dec 03, 2015 3:23 am     Reply with quote

That is certainly round the area where the problem existed if you did not limit the width. Have you tried with the width limit? (understand the value will still print exactly the same, since if the digits in front of the decimal make it overflow the specified width, the format will automatically expand to give the space required). Also since a 'float' only has just over 6 digits of real precision, printing 6 decimals is rather pointless....

Have two versions later (works), and one a couple older. On the older one, it goes wrong in all sorts of ways. Changed the format to %9.8f, and it started working. So you are almost certainly right in the area where there were problems if you specified &f without a width.
scanan



Joined: 13 Aug 2004
Posts: 58
Location: Turkey

View user's profile Send private message Visit poster's website

PostPosted: Fri Dec 04, 2015 2:10 pm     Reply with quote

Thanks for your suggestions.
After some trial I figure out that x.4 is ok.
But 5 6 decimal results as error.
_________________
Dr Suleyman CANAN
R&D Electronic Engineer
https://suleymancanan.wordpress.com

Do whatever you do with amateur spirit -
But always feel professional.
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