On Sun, Sep 17, 2000 at 11:44:01PM +0200, Albert Faber wrote:
>
Lets take:

  float   x = 1.5;
  long    y = 1234567890;
  double  z = x * y;

  printf ("%30.12f\n", z);

> 1) one type is long double, the other will be casted to long double
Not fulfilled.

> 2) one type is double, the other will be casted to double
Not fulfilled.

> 3) one type is float, the other will be casted to float
Fulfilled:
Convert (long)1234567890 to (float)1234567936.
  

> 4) one type is char, short int, enum or bitfield, the other will
> be converted to int, if int can represent all values of the original
> type; if not, the other type will be converted to unsigned int
Not fulfilled.

> 5) one type is unsigned long, the other will be unsigned long
Not fulfilled.

> 6) one type is long int and the other is unsigned int, then the unsigned
> int type will be converted to long int, if long int can
> represent all values of unsigned int. If this is not the case,
> then both are converted to unsigned long int
Not fulfilled.

> 7) one type is long, the other will be converted to long
Not fulfilled.

> 8) one type is unsigned, the other will get unsigned
Not fulfilled.

> 9) is none of the above cases true, then both are of type int
Not fulfilled.

So the code should be equivalent to:

  double  z = (float)1.5 * (float)1234567936;

  printf ("%30.12f\n", z);

which not only looks like nonsense.
But also gcc makes a lot of very strange things:

#include <stdio.h>

float   x       = 1.5;
long    y       = 1234567890;
float   y_float = 1234567890.0;
float   a       = 1.e32;

int main (void)
{
    double  z = x * y;

    printf ( "%30.12f\n", z );
    printf ( "%30.12f\n", (float)x * (float)y );    // this two lines
    printf ( "%30.12f\n", (float)x * y_float );     // give a different result on gcc
    printf ( "%30.12f\n", (double)x * (double)y );
    printf ( "a*a is %g, sizeof(a*a) is %u\n", a*a, sizeof(a*a) );  // ;-)
    return 0;
}

Maybe Richie made an arrangement with the FORTRAN compiler builder to save their jobs
for the next 50 years ;-)

-- 
Frank Klemm

Note: The background is that a float can't store all values of a signed long and an
unsigned long. The integer range of a float is only -16777216...+16777216.
For the same reason 64 bit ints should be auto casted to long double, not to double.

--
MP3 ENCODER mailing list ( http://geek.rcc.se/mp3encoder/ )

Reply via email to