CKTIS / C++
float — standard floating-point type
float is a built-in floating-point type for storing approximate numeric values. Literals with an f or F suffix have type float, whereas unsuffixed floating-point literals have type double. C++ does not guarantee that float uses IEEE 754 or occupies 32 bits.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
float name{};
// Alternatively, use this declaration instead:
// float name{1.0f};float is a built-in standard floating-point type. double provides at least as much precision as float, and long double at least as much as double; they are distinct types. Representation and operation accuracy depend on the implementation: do not assume that float always occupies 32 bits or uses IEEE 754.
The declaration float value{1.0f}; creates a float object. The suffix f or F gives a literal this type; unsuffixed 1.0 has type double. In C++23, conversion from double to float preserves the value if it is exactly representable as a float. If the source value is between two adjacent float values, the result is one of those values, with the choice defined by the implementation. Otherwise, the behavior is undefined. An explicit cast does not restore lost digits.
Not every decimal value is exactly representable. On binary implementations, 0.1f is one example, so do not assume that comparing computed results with == will establish their mathematical equality. Inspect implementation properties using std::numeric_limits<float> from <limits>, including digits10, max(), and lowest().
The example checks the variable's type and the unsuffixed literal's type at compile time, then prints 1. This entry targets C++23; the referenced site is an evolving working draft, not a frozen C++23 publication. The repository compiler flag does not attest the production runtime.
Examples
#include <iostream>
#include <type_traits>
int main() {
float value{1.0f};
static_assert(std::is_same_v<decltype(value), float>);
static_assert(std::is_same_v<decltype(1.0), double>);
std::cout << value << '\n';
}
Result
1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.