Clive D.W. Feather
clive at demon.net
Wed Aug 4 19:00:34 UTC 2004
Paul Eggert said:
>> My inclination is to say "don't do that".
> No can do. POSIX requires the od command to "do that". Here's the spec:
> The only way to support (say) "od -t xL" is to use a %lx format,
> selected at run-time.
Accepted, though your specific method isn't required.
[Incidentally, if I read it correctly you don't need to handle types larger
than 16 bytes anyway.]
> sarcasm off. hope you didn't mind...)
>> I'm still dubious about "a lot".
> What can I say? I gave you one example, from code I was working on
> the minute I received your email (no lie!).
I don't doubt you.
> As it happens this code
> is quite widely used, and widely portable, and it has safely made the
> sizeof(int)<=sizeof(long) assumption since before C89 came out. I
> could give you other examples but I'm afraid it sounds like your mind
> was made up before I started.
No. It's just that I am having difficulty figuring out why someone would do
that at all. You've given me one example.
>> What's difftime.c doing that needs that assumption?
> difftime's problem is slightly different. It's trying to subtract two
> POSIX time_t values and return a floating-point answer that is exactly
> correct, when possible. It can't simply subtract the time_t values,
> because they are typically integers and we might have integer
> overflow. And it can't simply convert to floating point and subtract
> the results, because that will lose information in some cases (e.g.,
> if time_t is 64 bits and "double" is IEEE 64-bit double). So it uses
> a heuristic, based on the size of time_t, to decide what to do. This
> heuristic is that if sizeof (time_t) < sizeof (double), then time_t
> can be converted to double without losing information; and similarly
> for long double. This heuristic is not guaranteed by C but is true on
> all platforms that we know of. (If you know of any counterexamples,
> please let us know.)
I'm not familiar enough with architectures to be able to answer that.
An implementation could put double in 80 bit storage but still make it an
IEEE 64 bit double; that would break you. I can't see why it would, though
(but that argument isn't one you like).
> As far as I know, there is no portable way in C89 or C99 to implement
> POSIX difftime; the heuristic is the best we have come up with so far.
I'll do some thinking.
Clive D.W. Feather | Work: <clive at demon.net> | Tel: +44 20 8495 6138
Internet Expert | Home: <clive at davros.org> | Fax: +44 870 051 9937
Demon Internet | WWW: http://www.davros.org | Mobile: +44 7973 377646
Thus plc | |
More information about the tz