View previous topic :: View next topic |
Author |
Message |
scanan
Joined: 13 Aug 2004 Posts: 59 Location: Turkey
|
Weird Division [SOLVED] |
Posted: Wed Dec 02, 2015 8:08 am |
|
|
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: 19552
|
|
Posted: Wed Dec 02, 2015 8:30 am |
|
|
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: 59 Location: Turkey
|
|
Posted: Wed Dec 02, 2015 10:42 am |
|
|
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: 19552
|
|
Posted: Thu Dec 03, 2015 3:23 am |
|
|
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: 59 Location: Turkey
|
|
Posted: Fri Dec 04, 2015 2:10 pm |
|
|
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. |
|
|
|