Main Content

AUTOSAR C++14 Rule A13-6-1

Digit sequences separators ' shall only be used as follows: (1) for decimal, every 3 digits, (2) for hexadecimal, every 2 digits, (3) for binary, every 4 digits

Since R2021a

Description

Rule Definition

Digit sequences separators ' shall only be used as follows: (1) for decimal, every 3 digits, (2) for hexadecimal, every 2 digits, (3) for binary, every 4 digits.

Rationale

Since C++14, you can introduce a separator ' to separate digits in a digit sequence for better readability. For consistency across your code, follow this convention when entering the digit sequence separator:

  • In decimal values, starting from the right, place the separator after every three digits, for instance, 3'000'000.

  • In hexadecimal values, starting from the right, place the separator after every two digits, for instance, 0xF'FF'0F.

  • In binary values, starting from the right, place the separator after every four digits, for instance, 0b1001'0011'0100.

If you are consistent across your code, a developer or code reviewer can follow your code more easily and possibly estimate the order of magnitude of a value from the digit sequence separators.

Polyspace Implementation

This checker follows the specifications of the AUTOSAR C++14 rule.

For integers, the checker starts checking from the right. For instance, the checker raises a violation on the value 45'30'00, because starting from the right, the digit sequence separator appears after two digits instead of the expected three.

For floating-point numbers, the checker begins the check from the decimal point and proceeds outwards. The checker checks:

  • The part before the decimal starting from the right.

  • The part after the decimal starting from the left.

For floating-point numbers with a mantissa and exponent, the same rule as integers applies to the exponent. For instance, in the decimal notation, the checker checks exponents starting from the right and raises a violation if the digit sequence separators are placed, for instance, after every two digits instead of three.

Troubleshooting

If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include <cstdint>

std::uint32_t largeNum = 3'0000'0000; //Noncompliant
std::uint32_t smallerNum = 3'000'000;  //Compliant
std::uint32_t evenSmallerNum = 3'00'00; //Noncompliant

std::uint32_t largeHexNum = 0xFF'FF'FF'FF; //Compliant
std::uint32_t smallerHexNum = 0xFFF'FFF; //Noncompliant

In this example, the placement of digit sequence separators is compliant if the separators follow the expected convention:

  • In decimal numbers, starting from the right, the separator is placed after every three digits.

  • In hexadecimal numbers, starting from the right, the separator is placed after every two digits.

#include <cstdint>

float PI = 3.1'415'926'53; //Noncompliant
float pi = 3.141'592'653; //Compliant;

float one_LB_to_KG = 0.45'359'237; //Noncompliant
float one_lb_to_kg = 0.453'592'37; //Compliant

In this example, the same floating-point number is assigned to two different variables but the placement of the digit sequence separators is different. The placement is compliant if the separators follow the expected convention:

  • For digits after the decimal, starting from the left, the separator is placed after every three digits.

  • For digits before the decimal, starting from the right, the separator is placed after every three digits.

    For floating-point numbers, the need for a digit sequence separator before a decimal is typically a rare occurrence. For instance, if you store a floating point-number in normalized form, the mantissa has only one digit before the decimal.

Check Information

Group: Overloading
Category: Required, Automated

Version History

Introduced in R2021a