Printf Formatting Bug

Questions about the BASICtools and MakeItC
Post Reply
joshklod
Posts: 7
Joined: Tue Feb 27, 2018 7:52 pm

Printf Formatting Bug

Post by joshklod »

When using the `%f` specifier of `printf`, if the value is rounded up, the last trailing 0 is dropped.

Tested with:
  • ARMbasic[9.37m]
  • BASICtools[6.06]
  • ARMbasic Kernel[8.36a]
  • SuperPRO
Example:

Code: Select all

dim a as single
a = -1
printf("%%1.4f: %1.4f   %%1.2f: %1.2f   %%1.1f: %1.1f", a, a, a)
while a < 2
	a += 1/256
	printf("%%1.4f: %1.4f  %%1.2f: %1.2f  %%1.1f: %1.1f", a, a, a)
loop
Output:

Code: Select all

%1.4f: -1.0000   %1.2f: -1.00   %1.1f: -1.0
%1.4f: -0.9961  %1.2f: -1.0  %1.1f: -1.
%1.4f: -0.9922  %1.2f: -0.99  %1.1f: -1.
%1.4f: -0.9883  %1.2f: -0.99  %1.1f: -1.
%1.4f: -0.9844  %1.2f: -0.98  %1.1f: -1.
%1.4f: -0.9805  %1.2f: -0.98  %1.1f: -1.
%1.4f: -0.9766  %1.2f: -0.98  %1.1f: -1.
%1.4f: -0.9727  %1.2f: -0.97  %1.1f: -1.
%1.4f: -0.9688  %1.2f: -0.97  %1.1f: -1.
%1.4f: -0.9648  %1.2f: -0.96  %1.1f: -1.
%1.4f: -0.9609  %1.2f: -0.96  %1.1f: -1.
%1.4f: -0.9570  %1.2f: -0.96  %1.1f: -1.
%1.4f: -0.9531  %1.2f: -0.95  %1.1f: -1.
%1.4f: -0.9492  %1.2f: -0.95  %1.1f: -0.9
%1.4f: -0.9453  %1.2f: -0.95  %1.1f: -0.9
[Omitted correct lines]
%1.4f: 0.9453  %1.2f: 0.95  %1.1f: 0.9
%1.4f: 0.9492  %1.2f: 0.95  %1.1f: 0.9
%1.4f: 0.9531  %1.2f: 0.95  %1.1f: 1.
%1.4f: 0.9570  %1.2f: 0.96  %1.1f: 1.
%1.4f: 0.9609  %1.2f: 0.96  %1.1f: 1.
%1.4f: 0.9648  %1.2f: 0.96  %1.1f: 1.
%1.4f: 0.9688  %1.2f: 0.97  %1.1f: 1.
%1.4f: 0.9727  %1.2f: 0.97  %1.1f: 1.
%1.4f: 0.9766  %1.2f: 0.98  %1.1f: 1.
%1.4f: 0.9805  %1.2f: 0.98  %1.1f: 1.
%1.4f: 0.9844  %1.2f: 0.98  %1.1f: 1.
%1.4f: 0.9883  %1.2f: 0.99  %1.1f: 1.
%1.4f: 0.9922  %1.2f: 0.99  %1.1f: 1.
%1.4f: 0.9961  %1.2f: 1.0  %1.1f: 1.
%1.4f: 1.0000  %1.2f: 1.00  %1.1f: 1.0
%1.4f: 1.0039  %1.2f: 1.00  %1.1f: 1.0
[Omitted correct lines]
%1.4f: 1.9453  %1.2f: 1.95  %1.1f: 1.9
%1.4f: 1.9492  %1.2f: 1.95  %1.1f: 1.9
%1.4f: 1.9531  %1.2f: 1.95  %1.1f: 2.
%1.4f: 1.9570  %1.2f: 1.96  %1.1f: 2.
%1.4f: 1.9609  %1.2f: 1.96  %1.1f: 2.
%1.4f: 1.9648  %1.2f: 1.96  %1.1f: 2.
%1.4f: 1.9688  %1.2f: 1.97  %1.1f: 2.
%1.4f: 1.9727  %1.2f: 1.97  %1.1f: 2.
%1.4f: 1.9766  %1.2f: 1.98  %1.1f: 2.
%1.4f: 1.9805  %1.2f: 1.98  %1.1f: 2.
%1.4f: 1.9844  %1.2f: 1.98  %1.1f: 2.
%1.4f: 1.9883  %1.2f: 1.99  %1.1f: 2.
%1.4f: 1.9922  %1.2f: 1.99  %1.1f: 2.
%1.4f: 1.9961  %1.2f: 2.0  %1.1f: 2.
%1.4f: 2.0000  %1.2f: 2.00  %1.1f: 2.0



basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: Printf Formatting Bug

Post by basicchip »

printf is part of the firmware, and I duplicated your test.

the printf code is open source code we found the web and have been using for some time, without noticing this error. We publish the source of that code in the C tools. Also a link here

https://coridium.us/files/printf_breakpoint.zip

We will look into it, but don't know when we will have a fix, which would require a firmware change.

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: Printf Formatting Bug

Post by basicchip »

I found the issue, not sure why that special case was handled incorrectly in the code. We did not write that originally and I can only assume rounding was disabled for most cases. We enable rounding in BASIC as that seems to be the standard.

We will post a firmware update here, and start rolling that into production products. Here is the test case I used for the fixed version --

Code: Select all

dim x as single
x=0.9996
print x
printf("%1.3f",x)
x=9.9996
print x
printf("%1.3f",x)
printf("%1.3E  %1.4E",x,x)
printf("%1.3G %1.4G",x,x)
Output --

Code: Select all

0.9996000
1.000
9.999600
10.000
1.000E+01  9.9996E+00
10.000 9.9996

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: Printf Formatting Bug

Post by basicchip »

I have fixed the rollover rounding bug.

You need to copy TclTerm.tcl and LPC1756.hex files attached into the directory where BASICtools.exe is run from (typically /Program Files (x86)/Coridium)
The attachment TclTerm.tcl is no longer available
LPC1756..hex
(71.2 KiB) Downloaded 1529 times
Attachments
TclTerm.tcl
(73.35 KiB) Downloaded 1079 times

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: Printf Formatting Bug

Post by basicchip »

the latest phpBB3 seems to have a bug when trying to attach multiple files, so here is TclTerm.tcl
TclTerm.tcl
(73.35 KiB) Downloaded 1547 times

joshklod
Posts: 7
Joined: Tue Feb 27, 2018 7:52 pm

Re: Printf Formatting Bug

Post by joshklod »

The new firmware works perfectly. All my issues have been resolved.

Thank you very much for the fast fix!

Post Reply