0

The C standard (and C++: [expr.7.1.6], and Fortran), states:

The values of the floating operands and the results of floating expressions may be represented in greater precision and range than that required by the type; the types are not changed thereby.

Some compilers use greater precisions for the results of floating-point expressions by default (GCC, MSVC), and some compilers do not (Clang), but most of them (not all) are standard conforming and provide a way to control the floating-point contraction behavior.

I write programs that do a lot of floating-point arithmetic.

Is there a general recommendation about whether one should enable or disable contraction by default? Or should I decide this on a case-by-case basis? If so, what should I weight as arguments in favor or against enabling contraction by default for a whole application ?


This other answer (Is floating point expression contraction allowed in C++?) explains what contraction is, and what the standard rules are, but offers no advice about how to use it. I have experienced both some advantages (e.g. faster performance) and some disadvantages (e.g. super hard to debug bugs) of contraction, but would like to know if there is community consensus about what's the recommended thing to do by default, or if this is one of those things that should be decided on a case-by-case basis (and if that's the case, how do I decide this beforehand?).

gnzlbg
  • 7,135
  • 5
  • 53
  • 106
  • 1
    Why the fortran tag? – Barmar Apr 18 '19 at 17:38
  • Fortran has the same behavior as C and C++, edited the answer with a link to the Fortran standard, and added Fortran in the title. – gnzlbg Apr 18 '19 at 17:39
  • Even if the standards might seem similar for C and C++, they are still two *very* different languages. And even minute differences in the wordings of the standards could mean very big differences in the language semantics. Therefore please try to keep your questions to one language or the other, unless you make a direct comparison between the languages. And just because two programs in C and C++ seems to behave similarly, doesn't mean they're the same and that the behavior always have to be similar. – Some programmer dude Apr 18 '19 at 17:42
  • @Someprogrammerdude It seems like he's asking a more general question about how to use floating point -- should you allow any language to do expression contraction? – Barmar Apr 18 '19 at 17:43
  • [datascience.se] might be a better place to ask something like that. – Barmar Apr 18 '19 at 17:44
  • @Someprogrammerdude C++17 includes the FP_CONTRACT pragma specified in the C11 standard from the C11 standard text, so... AFAICT it has to have the exact same semantics and behavior as in C. Otherwise the C11 standard would need to have some clause "When included by C++, this pragma does something else", or the C++ standard would need to have a clause modifying the behavior of this pragma, but I couldn't find either - maybe I missed them? – gnzlbg Apr 18 '19 at 17:46
  • @gnzlbg Is the wording *exactly* the same in both specifications? Then a specific question about that (and only that) could possibly be tagged with both languages. But the question seems more generic than that, and also adds the completely unrelated language Fortran (of which there are conflicting versions of itself). Perhaps you should try to make a more generic question and tag it as `language-agnostic` (which adds the risk of it being to broad, which it already is IMHO)? Or ask specifically about behavior in a single language? You can't have it both ways. – Some programmer dude Apr 18 '19 at 17:51
  • 1
    @Barmar I really doubt Data Science is appropriate. Perhaps Computational Science, but I think here it is OK. – Vladimir F Героям слава Apr 18 '19 at 18:17
  • 1
    I think the problem with this question is that there's probably no general answer to it. You need to determine the needs of your specific application to determine how FP contraction impacts it. – Barmar Apr 18 '19 at 19:03
  • @Someprogrammerdude yes, the wording is exactly the same, the C++ standard includes this part of the C standard verbatim as is. The C standard wording is copy pasted as is in the C++ standard. – gnzlbg Apr 18 '19 at 19:03
  • @Barmar that would be a valid answer, but the question is what should you do by default. If you were writing a C implementation, you can choose your default, and this default needs to be the best for most applications. What should it be? If you need to write portable C code, different C compiler choose different defaults for this, so if you want to get similar results, you should be setting them all to the same behavior at least when that does affect your results. – gnzlbg Apr 18 '19 at 19:05
  • If there were a good answer to what the default should be, I think either the standard would have mandated it, or most implementations would do it voluntarily. The fact that GCC and clang differ is a strong indication that both modes are reasonable. – Barmar Apr 18 '19 at 19:07
  • 1
    In general, I think most experienced programmers understand that FP introduces precision errors, and they don't expect portability in cases where this parameter takes effect. – Barmar Apr 18 '19 at 19:10
  • 2
    Fortran does NOT have this feature in the standard. Fortran compilers may do this, and back in the x87 days many did, but there's nothing in the Fortran standard on this topic. The standard specifies the type for every operation, but Fortran leaves a lot up to the individual implementation without giving explicit permission. – Steve Lionel Apr 18 '19 at 23:39
  • [here](https://retrocomputing.stackexchange.com/questions/9751/did-any-compiler-fully-use-80-bit-floating-point) is a somewhat related question. In particular, there's a link to [Bruce Dawson's Intermediate Floating-Point Precision](https://randomascii.wordpress.com/2012/03/21/intermediate-floating-point-precision/) which discusses this issue. – Barmar Apr 19 '19 at 16:58
  • @SteveLionel "Fortran does NOT have this feature in the standard". The standard disagrees: https://www.fortran.com/F77_std/rjcnf0001-sh-6.html#sh-6.6.4 "the processor may evaluate any mathematically equivalent expression, provided that the integrity of parentheses is not violated." The standard actually contains examples. – gnzlbg Apr 20 '19 at 18:16

0 Answers0