cplusplus / cplusplus/draft

[numeric.limits.members] The example for `is_modulo` is not quite correct for small signed integer types

Open
#8,438 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

lwg not-editorial
Dominant language
TeX
Stars
221
Forks
813
Avg merge
16h 4m
Merged PRs (30d)
36

Description

[numeric.limits.members] currently states:

  1. true if the type is modulo.182) A type is modulo if, for any operation involving +, -, or * on values of that type whose result would fall outside the range [min(), max()], the value returned differs from the true value by an integer multiple of max() - min() + 1.
  2. [Example 1: is_modulo is false for signed integer types ([basic.fundamental]) unless an implementation, as an extension to this document, defines signed integer overflow to wrap. — end example]

On most modern platforms, signed char is 8-bit and int is 32-bit. For any operation involving +, -, or * on two signed char values, the operands are converted to int first, and the behavior is always well-defined and the result (of type int) is always mathematically correct.

There seems to be different interpretations for this case.

  1. On these platforms, signed char simply satisfies the condition for is_modulo because the value returned always differs from the true value by zero.
  2. Even if we require converting back returned value to singed char, it would always differ from an integer multiple of 256.
  3. Given signed char values are converted due to usual arithmetic conversion first, we may said that signed char values don't actually participate in arithmetic operations (see also LWG554). As a result, is_modulo should be vacuously true for signed char (and other non-promoted integer types).

So IIUC std::numeric_limits<signed char>::is_modulo should be true on these platforms and the example is incorrect.

Also, it's not clear why std::numeric_limits<bool>::is_modulo is false. This doesn't seem correct unless the returned value is required to be converted back to bool.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the [numeric.limits.members] wording and Example 1, then read the issue's discussion of integral promotions and LWG554. Compare the treatment of signed small integer types and bool against the definition of is_modulo. Done means the standard wording and example clearly resolve both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.