Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#611 Equation result not promoted to float?

closed-fixed
Bernhard Held
None
5
2013-05-25
2003-10-18
No

In the code below it looks like the result of the macro is
not promoted to float before adding 0.5 to it and
asigning to f. If the result of the macro is casted, say
by using (float), the result is correct. This used to work
fine with the sdcc CVS version prior to October 9, 2003
compiled with VC6 under Win XP. The result obtained
with the current sdcc CVS version is:

days_since_2000_Jan_0(y, m, d)=1387
days_since_2000_Jan_0(y, m, d)+0.5=1452447.500000
(float)days_since_2000_Jan_0(y, m, d)+0.5=1387.500000

The code was compiled using:

sdcc --model-large --debug promo.c

Jesus

--- cut here ---
#include <stdio.h>
#ifdef SDCC_mcs51
#include <8051.h>
#endif

#define days_since_2000_Jan_0(y,m,d) \ (367L*(y)-((7*((y)+(((m)+9)/12)))/4) \ +((275*(m))/9)+(d)-730530L)

void main(void)
{
float f;
int y=2003;
char d=18, m=10;

printf("days_since_2000_Jan_0(y, m, d)=%ld\n",
days_since_2000_Jan_0(y, m, d));

/*Here the result of the macro is not promoted to
float*/
f=days_since_2000_Jan_0(y, m, d)+0.5;
printf("days_since_2000_Jan_0(y, m, d)+0.5=%f\n",
f);

/*Here the promotion to float is explicit*/
f=(float)days_since_2000_Jan_0(y, m, d)+0.5;
printf("(float)days_since_2000_Jan_0(y, m, d)+0.5=%
f\n", f);
}

#ifdef SDCC_mcs51
unsigned char _sdcc_external_startup(void)
{
TR1=0;
TMOD=(TMOD&0x0f)|0x20;
PCON|=0x80;
TH1=TL1=0xfd;
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
--- cut here ---

Discussion

  • Bernhard Held
    Bernhard Held
    2003-10-18

    Logged In: YES
    user_id=203539

    Fixed in SDCCval.c 1.78

    I'm sorry Jesus, it was copy & paste error.

    Thanks for the comprehensive bug report!

     
  • Bernhard Held
    Bernhard Held
    2003-10-18

    • milestone: --> fixed
    • assigned_to: nobody --> bernhardheld
    • status: open --> closed-fixed