Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Numeric types

Numeric types determine how values are represented and calculations behave. Learn the division into integral and floating-point types, the difference between signed and unsigned, and how conversions affect division and information loss.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

int count = 7;
unsigned int size = 3u;
double ratio = 7.0 / 2.0;

Built-in “numeric types” are called arithmetic types in standard terminology: integral and floating-point types. Integral types also include bool and character types. int is signed, whereas unsigned int is unsigned. The types float, double, and long double provide successively at least as much precision; do not assume each is more precise or that int always has 32 bits.

Arithmetic in an unsigned type of width N operates modulo 2^N. Thus, subtracting 1u from 0u produces the largest unsigned int value. Signed integer arithmetic overflow is undefined behavior, not guaranteed wraparound.

Operand types affect the result: 7 / 2 produces 3 because integer division discards the fractional part toward zero. static_cast<double>(7) / 2 produces 3.5; the conversion must happen before division. The usual arithmetic conversions determine a common operand type; mixing signed and unsigned types can select an unsigned type.

Converting a floating-point value to an integral type other than bool also discards the fractional part toward zero. If the truncated result does not fit the destination type, behavior is undefined. Converting a large integer to a floating-point type can lose precision.

The example demonstrates division, explicit conversion, and unsigned arithmetic without assuming a particular width for unsigned int. This entry targets C++23; the supplied reference is a developing draft, not a frozen C++23 publication. A repository compiler flag does not attest operation in a production runtime.

Examples

#include <iostream>

int main() {
    int a = 7;
    int b = 2;
    unsigned int u = 0u;
    u -= 1u;

    std::cout << a / b << '\n';
    std::cout << static_cast<double>(a) / b << '\n';
    std::cout << static_cast<int>(-3.75) << '\n';
    std::cout << std::boolalpha << (u > 0u) << '\n';
}

Result

3
3.5
-3
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources