0

I'm implementing a DSL with a compiler that translates it into C. Our code is self-contained: we provide users with just one main entry point (i.e., void step()), all inputs are given in global variables with simple types (bool, signed/unsigned ints, double, float, structs, and arrays; but no pointers, and no functions as inputs).

Our main target is C99, but some people are using the arduino compiler for the code we produce, so maximum portability across standards and compilers is preferred.

I see that C99's math.h defines multiple variants of functions based on the type of the arguments (e.g., sin, sinf, sinl).

To avoid losing precision, our translator can, based on the types of the arguments, pick one of those variants. However, all functions we support are also defined by tgmath.h, so a simpler way would be to use the standard name for each (e.g., sin) and let tgmath.h pick the appropriate variant based on the types of the arguments.

I've read online that opinions on tgmath.h, and support, are mixed. Some people say it is ugly, and that not all compilers support it.

Is relying on tgmath.h a bad idea? Is this going to give us headaches down the line?

Ivan Perez
  • 582
  • 2
  • 17
  • 1
    *Our main target is C99...* Isn't `tgmath.h` a C **11** feature? – Andrew Henle Apr 21 '22 at 22:00
  • 4
    @AndrewHenle [`_Generic`](https://en.cppreference.com/w/c/language/generic) was added in C11, but _"The type-generic math macros from [``](https://en.cppreference.com/w/c/numeric/tgmath), introduced in **C99**, were implemented in compiler-specific manner. Generic selections, introduced in C11, gave the programmers the ability to write similar type-dependent code."_ – Ted Lyngmo Apr 21 '22 at 22:03
  • Ivan Perez ,Note that sometimes one wants `double_math_function(called_with_float_arg)`. Does your approach still allow users to do that? – chux - Reinstate Monica Apr 21 '22 at 22:59
  • Our DSL doesn't really allow that. You can cast the float to a double first, or cast the return type from float to double. All math functions we support are in math.h or stdlib.h (or can be desuraged into functions that are), so I'm not sure we need to allow for that. Users cannot call arbitrary C functions in our language. – Ivan Perez Apr 21 '22 at 23:01
  • 2
    "Some people say it is ugly, and that not all compilers support it." *Some people* is not a convincing citation. Given pretty well any utterance, however malicious or idiotic, it's almost certain that someone has said it, and these days, that you can find an online discussion in which it was said. Repeating an unsubstantiated claim without identifying the source lends unwarranted credibility to what may well be a prejudice or untruth. It's one of the mechanisms by which social networks magnify drama over veracity, although, to be fair, its history is much longer than the internet's. – rici Apr 22 '22 at 00:58
  • https://www.pixelstech.net/article/1324906407-The-ugliest-C-feature%3A-%3Ctgmath-h%3E – Ivan Perez Apr 22 '22 at 09:53
  • That's one source, but I've had this discussion with others (via other channels) and it seems to be a spread perception. I agree that that's no credible argument. I am acknowledging that there may be drawbacks that an experienced C programmer might have an opinion on, while making it clear I am not one myself and that it is not my opinion because I don't have one. – Ivan Perez Apr 22 '22 at 09:55
  • https://www.reddit.com/r/programming/comments/1maa65/the_ugliest_c_feature_tgmathh/ – Ivan Perez Apr 22 '22 at 09:57
  • 1
    Thanks for adding the citations. Few would disagree that `tgmath.h` was an ugly special case in C99, but it's really not a viable argument that a code generator should not use a required language feature because the feature offends the aesthetic sensibilities of some programmer. I find `goto` unaesthetic but I'm not going to toss out a code generator which finds `goto` to be the simplest implementation. That would be absurd. Anyway, as of C11, a general facility was added, which not only allows portable implementation of `tgmath.h` but also demonstrates a commitment to the concept... – rici Apr 23 '22 at 03:06
  • 1
    ... clearly, some programmers find `_Generic` ugly, too --imho, reddit is full of superficial aesthetic prejudices masquerading as informed opinion-- but it's equally absurd to refuse to use a feature because you don't like the way its keyword is spelled. The only viable argument would be inconsistency of implementation, but afaics no-one is actually claiming that. And tg math functions are trivially available in C++, so Arduino shouldn't be a problem either (although I think avr-gcc implements C11, too.) – rici Apr 23 '22 at 03:13
  • 1
    I get the need for due diligence. But it's very rare for a feature to be added to C, and then removed. The only one I can think of is VLAs, added in C99 and made optional in C11. (C23 is likely to make some aspects obligatory again.) But even though the requirement was removed in C11, I don't think that any C compiler which supported VLAs withdrew support. So no functioning program became non-functional as a result of the optionality of VLAs. In short, using a feature which has been part of C for 23 years is not risky behaviour. – rici Apr 23 '22 at 03:20

1 Answers1

1

A C99 (page 165 and 335 in the standard) compliant compiler will include tgmath.h so relying on tgmath.h is ok. It's a standard header since C99.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108