Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#1019 printf("%f"...) sets fraction to zero

closed-fixed
Bernhard Held
7
2013-05-25
2005-11-16
No

The code below was compiled using

C:\sdcc\tests>sdcc -v
SDCC :
mcs51/gbz80/z80/avr/ds390/pic16/pic14/TININative/xa51/d
s400/hc08 2.5.4 #1153 (Nov 15 2005) (MSVC)

C:\sdcc\tests>sdcc --model-large badpf.c

When run, the output is:

x=10.000000, y=3.000000, x/y=3.000000
x*10=101.000000, y*10=31.000000, (x/y)*1000=3258.000000

While the output should have been:

x=10.100000, y=3.100000, x/y=3.258065
x*10=101.000004, y*10=30.999999, (x/y)*1000=3258.064747

(print_large.c is compiled with USE_FLOATS enabled!)

/**** badpf.c ****/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#ifdef SDCC_mcs51
#include <8051.h>
void dummyint3 (void) interrupt 3 _naked {}
#endif

void main (void)
{
volatile float x, y, z;
x=10.1;
y=3.1;
z=x/y;
printf("x=%f, y=%f, x/y=%f\n", x, y, z);
printf("x*10=%f, y*10=%f, (x/y)*1000=%f\n", x*10,
y*10, z*1000);
}

#ifdef SDCC_mcs51
unsigned char _sdcc_external_startup(void)
{
TR1=0;
TMOD=(TMOD&0x0f)|0x20;
PCON|=0x80;
TH1=TL1=0xff; //112000 baud with 22MHz crystal
TR1=1;
SCON=0x52;

return 0;
}

char getchar(void)
{
char c;
while (!RI);
RI=0;
c=SBUF;
return c;
}

void putchar(char c)
{
while (!TI);
TI=0;
SBUF=c;
if (c=='\n') putchar('\r');
}
#endif

Discussion

  • Logged In: YES
    user_id=603650

    The las CVS commit that worked was the one in August 7,
    2005. The commits on August 10, 2005 aparently introduced
    the problem.

    Jesus

     
  • Logged In: YES
    user_id=603650

    Typecasting an integer to float in printf_large.c solves
    the problem:

    Line 241: decimalPart=f-(float)integerPart;

    and

    Line 238: decimalPart-=(float)integerPart;

    The question is: shouldn't this be the default behaviour?

    Jesus

     
  • Bernhard Held
    Bernhard Held
    2005-11-18

    • assigned_to: nobody --> bernhardheld
     
  • Bernhard Held
    Bernhard Held
    2005-11-18

    Logged In: YES
    user_id=203539

    Fixed in src/SDCCsymt.c 1.205, build 1157

     
  • Bernhard Held
    Bernhard Held
    2005-11-18

    • labels: --> C-Front End
    • milestone: --> fixed
    • status: open --> closed-fixed