[Cfp-interest] type-generic math

Jim Thomas jaswthomas at sbcglobal.net
Thu Jan 19 09:32:04 PST 2012


On Jan 19, 2012, at 5:24 AM, Joel C. Salomon wrote:

> On 01/18/2012 08:30 PM, Jim Thomas wrote:
>> Here's a second attempt. I'd forgetting the functions that round result
>> to narrower type:
>> 
>>  16  Type-generic math <tgmath.h>
> <snip>
>> In 7.25 #5, include in the list of type-generic
>> macros: roundeven, nextup, nextdown, fminmag, fmaxmag, llogb, fromfp,
>> ufromfp, fromfpx, ufromfpx, totalorder, totalordermag, and canonicalize.
> 
> Are you satisfied with a call to totalorder with different types being
> defined; e.g., `totalorder(1.0f, 2.0) => totalorder((double)1.0f, 2.0)`?


I agree that calling a total order operation (function or macro) with different type arguments seems like bad programming practice. On the other hand, the existing conversion rules (and type-generic rules) give predictable results (mostly) - except for conversions of NaNs. Note that same-type conversion of a NaN is not guaranteed to preserve the sign. 

There's some advantage in relying on existing mechanisms instead of introducing a new special mechanism, even if the new one is better in some way. Being unaware of any experience with total order, I'm particularly wary of a total order special. 

A different approach that would dodge these concerns would be to define the total order functions with pointer arguments, e.g., 

int totalorder(double * x, double * y);

Then there would be no conversions and no type-generic macros for totalorder.

Mixed type arguments can be problematic for other operations too, e.g., nextafter. Wide evaluation further complicates the picture, because arguments can be in wider format than their type.

-Jim
 
> 
> Note that without totalorder being type-generic, the functions *can* be
> protected against mixed use:
> 
> #define __totalorder(x, y, type, func) \
> 	(_Generic(x, type: _Generic(y, type: func))(x, y))
> #define totalorder(x, y) __totalorder((x), (y), double, totalorder)
> #define totalorderf(x, y) __totalorder((x), (y), float, totalorderf)
> ...
> 
> --Joel
> _______________________________________________
> Cfp-interest mailing list
> Cfp-interest at oakapple.net
> http://mailman.oakapple.net/mailman/listinfo/cfp-interest




More information about the Cfp-interest mailing list