Mixing doubles and unsigned things

Mike McNally m5 at lynx.uucp
Thu Jun 8 10:28:56 AEST 1989


What is appropriate behavior in the following instance:

    double d;
    unsigned long u;

    u = BIGNUM;
    d = u;

Assuming that BIGNUM is between the biggest long and the biggest
unsigned long, shouldn't the double be the same value and not a
negative number?  Conversely,  if "d" already contained the
floating-point version of BIGNUM, shouldn't one be able to store the
value into an unsigned long and get the right value?

As an example, try this at home:
    
    main()
    {
        double d;
        unsigned long u;

        d = 3000000000.0 /* That's 3 billion. */
        u = d;
        printf("%lu\n", u);
    }

Under Microport, this generates a floating point exception, by the way.

Why do I ask?  Well, under Microport System V on a 386 machine and
under 4.3BSD on my 68020 machine (Greenhills compiler), it doesn't
work.  The root of the problem is the underlying floating point
processor architecture.  Neither the Intel 387 nor the Moto 68881 know
about unsigned integers.  The 387 does know about 64-bit integers, but
of course support is pretty limited, and the compiler would have to
generate nasty slow code to cope with the problem.  The situation is
much worse on the 881.

>From K&RII, A6.3:

    When a value of floating type is converted to integral type,
    the fractional part is discarded; if the resulting value cannot
    be represented in the integral type, the behavior is
    undefined.

OK, but of course BIGNUM *can* be represented in the integral type!
(From section A4.2, second to the last paragraph, we are assured that
unsigned long is an integral type.)

Now, to be honest, I personally don't give two hoots about all this;
one of my customers complained, however, and I wanted to know how bad I
should feel about saying "tough crap".  (I'm not really that mean to
customers.)  Does *anyone* know of a compiler for a 386 or a 68K that
generates correct code?

-- 
Mike McNally                                    Lynx Real-Time Systems
uucp: {voder,athsys}!lynx!m5                    phone: 408 370 2233

            Where equal mind and contest equal, go.



More information about the Comp.lang.c mailing list