>> Can substitutes in gnulib assume that the 'double' format is IEEE 754 >> (it is on all modern platforms; VAX was the last one with other formats) ? >> Or should they be written in a slower, but more portable way? > > This question is actually vital. If we can assume IEEE 754, then > I can use implementations from glibc. If we can't, then it is > more difficult, and I will have to see how clever I can be. > > Do we have any opinions on this?
For other math.h functions it's a vital question, but you should be able to implement trunc and round portably using ceil and floor, and this should work correctly even for IEEE 754 hosts. Something like this: double rpl_trunc (double x) { return x < 0 ? ceil (x) : 0 < x ? floor (x) : x; } double rpl_round (double x) { if (x < 0) { double t = ceil (x); if (0.5 <= t - x) t--; return t; } else if (0 < x) { double t = floor (x); if (0.5 <= x - t) t++; return t; } else return x; } This is written so that it works correctly on +-zero, infinities, and nans, assuming ceil and floor work. Admittedly there are a lot of tricky little bits here.