Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 364526 - System_printf float conversion differs from the standard C library
Summary: System_printf float conversion differs from the standard C library
Status: CLOSED FIXED
Alias: None
Product: RTSC
Classification: Technology
Component: Runtime (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Vikram Adiga CLA
QA Contact:
URL:
Whiteboard: targets: 3.25.00
Keywords:
Depends on:
Blocks:
 
Reported: 2011-11-22 17:23 EST by Sasha Slijepcevic CLA
Modified: 2013-04-04 17:07 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sasha Slijepcevic CLA 2011-11-22 17:23:17 EST
The following code:
#include <xdc/std.h>
#include <xdc/runtime/System.h>

Void main() {
    System_printf("%5.2f\n", 0.78125 * 0.34);
    System_printf("%7.2f\n", 0.78125 * 0.34);
}

should print
 0.27
   0.27

but it prints
00.2656
00.2656
instead.

It doesn't matter if SysMin or SysStd is used. The bug is initially reported here: http://e2e.ti.com/support/embedded/f/355/p/142976/532090.aspx#532090.
Comment 1 Vikram Adiga CLA 2012-01-17 13:50:02 EST
fixed by adding changes to the System.c and System.xdt. But the fractional part is fixed at 4 in System_printf to make it fast. 

#include <xdc/std.h>
#include <xdc/runtime/System.h>

Void main() {
    System_printf("%5.2f\n", 0.78125 * 0.34);
    System_printf("%7.2f\n", 0.78125 * 0.34);
}

now prints 

0.2656
 0.2656
Comment 2 Vikram Adiga CLA 2012-01-24 14:13:47 EST
fixed in xdc-z04
Comment 3 Sasha Slijepcevic CLA 2013-02-22 16:28:51 EST
- verified with XDCtools 3.25.00.40
The original example uses the field width of 5, which is a minimum width. Since we always print four digits after the decimal point, the output is already wider than five characters. The test stdout.precision in the xdctest tree already verifies the case with 5.2f and then 7.2f format strings.
I tried
    System_printf("%9.2f\n", 0.78125 * 0.34);
    System_printf("%7.2f\n", 0.78125 * 0.34);
and the result was
   0.2656
 0.2656
as expected.

Also, adding '0' as a padding character worked fine:
    System_printf("%09.2f\n", 0.78125 * 0.34);
    System_printf("%07.2f\n", 0.78125 * 0.34);
output:
0000.2656
00.2656