6

std::numeric_limits provides 2 constants that are mutually exclusive:

  • is_integer : "true for all integer arithmetic types T"

  • is_exact: "true for all arithmetic types T that use exact representation"

Is there the possibility of a non-exact integral type? What is trying to be allowed for here?

In all my templates where I to know if I am dealing with precise numbers, I used is_integer, do I need to go add a check for is_exact as well now?

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • Fixed-point maybe? Or fractions? There is actually a comment on encppreference regarding this. You are allowed to specialize such template for your own types. – Holt Jan 12 '18 at 13:31
  • 1
    Looking at [this question](https://stackoverflow.com/questions/14203654/stdnumeric-limitsis-exact-what-is-a-usable-definition) and the C++ standard, it looks like the C++ committee did not provide a good definition of `is_exact`. In integers, `7/3` does not give an exact result. It is in fact worse than floating-point `7./3.`. So integers can be said to be exact only by “defining” the `/` operation to be a truncated division, not normal mathematical division. But, if you are going to do that, then floating-point is also exact in the same way; its operations give exactly their defined results. – Eric Postpischil Jan 12 '18 at 16:23
  • 1
    Perhaps a meaning of `is_exact` is “many programmers understand the arithmetic of this type.” That seems to be a major distinguishing factor between integer arithmetic and floating-point arithmetic. :-) – Eric Postpischil Jan 12 '18 at 16:25
  • @EricPostpischil I guess... This is clearly left in place to provide for a fixed point future type. I've started parsing through proposals. I love the idea of getting a new primitive type... things definitely look interesting! – Jonathan Mee Jan 12 '18 at 18:22
  • 1
    Hmm, `is_integer` has a nice _exact_ definition. `is_exact` definition has far more room for interpretation - not so _exact_. – chux - Reinstate Monica Jan 18 '18 at 16:55

1 Answers1

13

From is_exact cppreference page:

Notes

While all fundamental types T for which std::numeric_limits<T>::is_exact==true are integer types, a library may define exact types that aren't integers, e.g. a rational arithmetics type representing fractions.


And, as @Holt has mentioned, the standard describes it as well:

21.3.4.1 numeric_limits members [numeric.limits.members]

static constexpr bool is_exact;

true if the type uses an exact representation. All integer types are exact, but not all exact types are integer. For example, rational and fixed-exponent representations are exact but not integer.

Community
  • 1
  • 1
Edgar Rokjān
  • 17,245
  • 4
  • 40
  • 67
  • 4
    Standard quote for this: *"All integer types are exact, but not all exact types are integer. For example, rational and fixed-exponent representations are exact but not integer."* – Holt Jan 12 '18 at 13:36
  • @Holt And now I'm scared of all the templates I've just depended on `is_integer` in :( – Jonathan Mee Jan 12 '18 at 13:40
  • 1
    @JonathanMee It completely depends on what you want to check for your templates. `is_integer` is stronger than `is_exact`. – Holt Jan 12 '18 at 13:44
  • @Holt And I guess I should only be scared if I start trying to write my own types... Or if the standard implements a fixed point type... Are there any plans to implement fixed point in the standard? – Jonathan Mee Jan 12 '18 at 13:50