View previous topic :: View next topic |
Author |
Message |
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
printf() formatting bug or feature? |
Posted: Wed Jul 08, 2009 9:15 pm |
|
|
Code: | int32 imW;
imW = 1234;
printf(lcd_putc, "\f\n%2.1wW", imW ); // 123.4W on the display. Expected 1.2W
printf(lcd_putc, "\f\n%2.1wW", imW/100 ); // 1.2W on the display as expected |
I wonder if this is a (minor) bug or a feature?
PCWH 4.081
PIC18F2620 _________________ Read the label, before opening a can of worms.
Last edited by kender on Wed Jul 08, 2009 10:11 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jul 08, 2009 9:57 pm |
|
|
The manual shows these two input values as examples:
Code: |
%3.1w
Input: 18 254
Output: 1.8 25.4
|
This shows that the right-hand number in the format specifier controls the
number of digits to the right of the decimal point in the output.
For example, the code below has the following output:
Code: | imW = 254;
printf("%2.3w\r", imW ); |
|
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Wed Jul 08, 2009 10:16 pm |
|
|
PCM programmer wrote: | This [the manual] shows that the right-hand number in the format specifier controls the number of digits to the right of the decimal point in the output. |
I agree, right-hand number controls the number of digits to the right of the decimal point, as expected. But it looks like the left-hand number doesn't control the total field width (number of digits). _________________ Read the label, before opening a can of worms. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jul 08, 2009 10:41 pm |
|
|
The printf specification says that the Width specifier is the minimum width.
If the number of digits is greater than the Width specifier, they will all be displayed, regardless of the Width value. |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Wed Jul 08, 2009 10:56 pm |
|
|
PCM programmer wrote: | The printf specification says that the Width specifier is the minimum width. |
I see - so, it's a feature.
Not to nitpick (the whole issue is minor and superficial), but that's not clear from compiler's online help. Minimum isn't mentioned there:
CCS compiler help wrote: | w
Unsigned int with decimal place inserted. Specify two numbers for n. The first is a total field width. The second is the desired number of decimal places. |
_________________ Read the label, before opening a can of worms. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu Jul 09, 2009 12:20 am |
|
|
As said, the optional width field in C format specifications is designating a minimum width. That's a common feature of the C language, not specific to CCS C. Otherwise, what do you expect in the said example, if it would mean a strict (maximum) width?
I addition, due to the fact, that "n" in n.m is specifying a total width (including the decimal point and following digits), there's no difference between "1.2" and "4.2", because 4 is the minimum width anyway. |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Thu Jul 09, 2009 12:44 am |
|
|
FvM wrote: | Otherwise, what do you expect in the said example, if it would mean a strict (maximum) width? |
From m.n (m > n) I was expecting to get a mantissa with n decimal places. Then %3.2w with 123, and %3.2w with 1230 would both produce 1.23. The calling code would keep track of the exponent separately (e.g. output the exponent separately).
Again, it works either way. In fact, now (few hours after the O.P.) I like the way it's currently done better. _________________ Read the label, before opening a can of worms. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu Jul 09, 2009 4:21 am |
|
|
Because the %w fixed point format is a CCS private extension of the C standard, it can involve arbitrary definitions. But the present way is plausible according to the minĂmum width meaning of the C standard format specifiers, I think. |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
Posted: Thu Jul 09, 2009 10:04 am |
|
|
FvM wrote: | Because the %w fixed point format is a CCS private extension of the C standard, it can involve arbitrary definitions. |
*Smiling and firing the pilot light on the flamethrower.* Ah-ha. Somebody's got a flexible opinion. Look at this line:
FvM wrote: | That's a common feature of the C language, not specific to CCS C. |
_________________ Read the label, before opening a can of worms. |
|
|
|