#1172 printf_fast_f, problem with large float

closed
5
2007-01-25
2006-07-19
No

printf_fast_f seems to have a problem with large
floating point numbers.
The code below outputs numbers up to 2147483648
correctly and then seems to fall into another endless
loop (outputing unrelated stuff).

(some lines copied from bug #1520966)

---------8<-------------------------------------
#include <stdio.h>
#include <c8051f120.h>

#define printf printf_fast_f

void putchar(char c) {
while(!TI)
;
SBUF = c;
TI = 0;
}

void my_init() {
WDTCN = 0xde;
WDTCN = 0xad;
TI = 1;
P3_3 = 1;
P3_6 = 1;
}

void main() {

float fpnum=1.0;

my_init();
printf("\r\nHello world!\r\n");
while (1) {
printf("0x%08lx -", *(unsigned long*)&fpnum);
printf(" %f\r\n", fpnum);
fpnum *= 2;
}
}
--------->8-------------------------------------

output:
---------8<-------------------------------------
Hello world!
0x3F800000 - 1
0x40000000 - 2
0x40800000 - 4
0x41000000 - 8
0x41800000 - 16

...

0x4E800000 - 1073741824
0x4F000000 - 2147483648
0x4F800000 -
(Garbage follows)

--------->8-------------------------------------

Unfortunately the alternate printf_large.c with
floating point support does not work for the small
memory model (it declares a 128 byte floating point
buffer (line 221) in the default memory space and thus
wont work. Could this be xdata or idata or smaller?)

Discussion

  • Paul Stoffregen

    Paul Stoffregen - 2006-07-19

    Logged In: YES
    user_id=104682

    printf_fast_f (which I wrote) is never going to handle
    printing floats larger than a 32 bit integer. It's so small
    and fast because it simply converts the mantissa to an
    integer and uses the integer code to print it, and does
    something similar for the fractional part.

    It could probably be reworked to be a little nicer, though.
    Sadly, I don't have any time to work on SDCC until after
    mid-September (2006). Bug me again around then if you want
    printf_fast_f modified for more graceful failure.

     
  • Paul Stoffregen

    Paul Stoffregen - 2007-01-25
    • assigned_to: nobody --> pjs
    • status: open --> closed
     
  • Paul Stoffregen

    Paul Stoffregen - 2007-01-25

    Logged In: YES
    user_id=104682
    Originator: NO

    Fixed crash on float overflow.

    You still can't print floats with a magnitude greater than 4294967040 with printf_fast_f, but at least it will not crash.

     

Log in to post a comment.