Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trivial CDC.printf program yields unexpected output
09-04-2013, 03:49 AM,
#1
Trivial CDC.printf program yields unexpected output
OS: OS X 10.6.8
Python: v2.6.1
IDE: x.4 rev750
SDCC: v3.2.0 release
gpasm: v0.13.7 beta
gplink: v0.13.7 beta

Code:
float f=0;

void setup()
{
    pinMode(USERLED, OUTPUT);
}

void loop()
{

    if(f > 10)
        {
        f = 1;
        CDC.printf("float=%.1f \r\n", f);  // 1 digit after decimal comma        
        }
    else
        {
        f = f + 0.1;
        CDC.printf("float=%.1f \r\n", f);  // 1 digit after decimal command
        }
        
    toggle(USERLED);
    delay(100);
}

The output is:

Code:
float=9.8
float=9.9
float=10.0
float=1.0
float=1.0 <<<<<<<< ????
float=1.1
float=1.2
float=1.3
float=1.4
float=1.5
float=1.6
float=1.7

Any ideas?
Reply
09-04-2013, 06:35 PM,
#2
RE: Trivial CDC.printf program yields unexpected output
Looks like a compiler bug so ideas:
1. look at code generated (not that easy?)
2. try other settings e.g. 32-bit so other compiler used
also, e.g. turn off all optimisation
3. try other SDCC

Hope to find something that works!

John
Reply
13-04-2013, 02:07 PM,
#3
RE: Trivial CDC.printf program yields unexpected output
I rewrote the program in Pascal and ... the same output. Turns out to be a rounding error.

1.00 -> 1.0
1.09 -> 1.0
1.19 -> 1.1
etc

Should have thought of that first Smile
Reply
13-04-2013, 03:09 PM, (This post was last modified: 13-04-2013, 03:10 PM by pingotg.)
#4
RE: Trivial CDC.printf program yields unexpected output
Something looks wrong. From my ANSI C standard, format ".1f" in C means to round such that one digit after the decimal point is printed. I cannot see how the result you got with SDCC is right.

I used C and gcc and got different output, getting the output I (and looks like you) expected.

(Pascal may not define whether the number is rounded or maybe you didn't use an equivalent format.)

John
Reply
13-04-2013, 11:53 PM,
#5
RE: Trivial CDC.printf program yields unexpected output
Yes, I also used gcc and received the expected output.

In Pascal I used a real number, compiler implementation limited to 5 decimal places, which yielded what the textbooks like to call "representational error".

Looks like sdcc (I also tried the latest versions) suffers from a similar issue.
Reply
14-04-2013, 09:15 AM,
#6
RE: Trivial CDC.printf program yields unexpected output
Maybe the pinguino libs don't use printf but their own which is buggy (doesn't round).

John
Reply
14-04-2013, 10:42 AM,
#7
RE: Trivial CDC.printf program yields unexpected output
That's right. It has been designed to have a smaller footprint than the original.
Code is in p32/include/pinguino/core/printf.c, see pprintfl function.

(14-04-2013, 09:15 AM)pingotg Wrote: Maybe the pinguino libs don't use printf but their own which is buggy (doesn't round).

John
It is easier to complain than it is to do, but it is better to do than it is to complain.
Reply
14-04-2013, 11:24 AM,
#8
RE: Trivial CDC.printf program yields unexpected output
I suspect it's the float type causing the issue. I substituted a double for f instead and all is now good.

Quote:double=9.8
double=9.9
double=10.0
double=1.0
double=1.1
double=1.2
double=1.3
Reply
14-04-2013, 12:26 PM,
#9
RE: Trivial CDC.printf program yields unexpected output
It may not be broken with double but it looks broken (fails to conform with ANSI C) with float.

I expect it won't matter in practice very often. I expect the actual arithmetic is OK, just CDC.printf that's buggy.

John
Reply
15-04-2013, 02:01 AM,
#10
RE: Trivial CDC.printf program yields unexpected output
(14-04-2013, 10:42 AM)regis Wrote: Code is in p32/include/pinguino/core/printf.c, see pprintfl function.

Ahhh...

Code:
FIRST RELEASE:  10 Nov 2010
LAST RELEASE:   16 Jan 2012
----------------------------------------------------------------------------
TODO : floating point support - work in progress
----------------------------------------------------------------------------
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)