Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C syntax
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Floating-point types==== A floating-point form is used to represent numbers with a fractional component. They do not, however, represent most rational numbers exactly; they are instead a close approximation. There are three standard types of real values, denoted by their specifiers (and since [[C23 (C standard revision)|C23]] three more decimal types): single precision ({{code|float}}), double precision ({{code|double}}), and double extended precision ({{code|long double}}). Each of these may represent values in a different form, often one of the [[IEEE floating-point]] formats. {| class="wikitable" width="80%" |+ Floating-point types |- ! rowspan="2" | Type specifiers ! colspan="2" | Precision (decimal digits) ! colspan="2" | Exponent range |- ! Minimum ! IEEE 754 ! Minimum ! IEEE 754 |- | {{code|float}} | align="center" | 6 | align="center" | 7.2 (24 bits) | align="center" | Β±37 | align="center" | Β±38 (8 bits) |- | {{code|double}} | align="center" | 10 | align="center" | 15.9 (53 bits) | align="center" | Β±37 | align="center" | Β±307 (11 bits) |- | {{code|long double}} | align="center" | 10 | align="center" | 34.0 (113 bits) | align="center" | Β±37 | align="center" | Β±4931 (15 bits) |} Floating-point constants may be written in [[decimal notation]], e.g. {{code|1.23}}. [[Decimal scientific notation]] may be used by adding {{code|e}} or {{code|E}} followed by a decimal exponent, also known as [[E notation]], e.g. {{code|1.23e2}} (which has the value 1.23 Γ 10<sup>2</sup> = 123.0). Either a decimal point or an exponent is required (otherwise, the number is parsed as an integer constant). [[Hexadecimal floating-point constant]]s follow similar rules, except that they must be prefixed by {{code|0x}} and use {{code|p}} or {{code|P}} to specify a binary exponent, e.g. {{code|0xAp-2}} (which has the value 2.5, since A<sub>h</sub> Γ 2<sup>β2</sup> = 10 Γ 2<sup>β2</sup> = 10 Γ· 4). Both decimal and hexadecimal floating-point constants may be suffixed by {{code|f}} or {{code|F}} to indicate a constant of type {{code|float}}, by {{code|l}} (letter {{code|l}}) or {{code|L}} to indicate type {{code|long double}}, or left unsuffixed for a {{code|double}} constant. The standard header file [[float.h|{{code|float.h}}]] defines the minimum and maximum values of the implementation's floating-point types {{code|float}}, {{code|double}}, and {{code|long double}}. It also defines other limits that are relevant to the processing of floating-point numbers. [[C23 (C standard revision)|C23]] introduces three additional ''decimal'' (as opposed to binary) real floating-point types: _Decimal32, _Decimal64, and _Decimal128. <!-- It's unclear if decimal is optional as keywords, or just in library: Support for the ISO/IEC 60559:2020, the current version of the [[IEEE 754|IEEE 754 standard]] for floating-point arithmetic, with extended binary floating-point arithmetic and (optional) decimal floating-point arithmetic.<ref name="N2341">{{cite web |title=WG14-N2341: ISO/IEC TS 18661-2 - Floating-point extensions for C - Part 2: Decimal floating-point arithmetic |url=https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2341.pdf |website=open-std.org |archive-url=https://web.archive.org/web/20221121122559/https://open-std.org/JTC1/SC22/WG14/www/docs/n2341.pdf |archive-date=November 21, 2022 |date=February 26, 2019 |url-status=live}}</ref><ref name="N2601">{{cite web |title=WG14-N2601: Annex X - IEC 60559 interchange and extended types |url=https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2601.pdf |website=open-std.org |archive-url=https://web.archive.org/web/20221014221322/https://open-std.org/JTC1/SC22/WG14/www/docs/n2601.pdf |archive-date=October 14, 2022 |date=October 15, 2020 |url-status=live}}</ref> --> : NOTE C does not specify a radix for '''float''', '''double''', and '''long double'''. An implementation can choose the representation of '''float''', '''double''', and '''long double''' to be the same as the decimal floating types.<ref name="N2341">{{cite web |title=WG14-N2341: ISO/IEC TS 18661-2 - Floating-point extensions for C - Part 2: Decimal floating-point arithmetic |url=https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2341.pdf |website=open-std.org |archive-url=https://web.archive.org/web/20221121122559/https://open-std.org/JTC1/SC22/WG14/www/docs/n2341.pdf |archive-date=November 21, 2022 |date=February 26, 2019 |url-status=live}}</ref> Despite that, the radix has historically been binary (base 2), meaning numbers like 1/2 or 1/4 are exact, but not 1/10, 1/100 or 1/3. With decimal floating point all the same numbers are exact plus numbers like 1/10 and 1/100, but still not e.g. 1/3. No known implementation does opt into the decimal radix for the previously known to be binary types. Since most computers do not even have the hardware for the decimal types, and those few that do (e.g. IBM mainframes since [[IBM System z10]]), can use the explicitly decimal types. <!-- Lots more keywords added in C23 (also for the preprocessor), see at https://en.cppreference.com/w/c/keyword such as _BitInt, typeof and thread_local as opposed to older _Thread_local. NOT mentioned there, only at https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2601.pdf (and note C23 is not yet finalized): X.5.1 Keywords _Float32x _Float64x _Float128x .. X.5.2 Constants [1] This subclause specifies constants of interchange and extended floating types. [2] This subclause expands floating-suffix (6.4.4.2) to also include: fN FN fNx FNx dN DN dNx DNx .. The type specifiers _FloatN (where N is 16, 32, 64, or β₯ 128 and a multiple of 32), _Float32x, _Float64x, _Float128x, _DecimalN (where N is 96 or > 128 and a multiple of 32), _Decimal64x, and _Decimal128x shall not be used if the implementation does not support the corresponding types (see 6.10.8.3 and X.2). .. β _DecimalN, where N is 96 or > 128 and a multiple of 32 β _Decimal64x β _Decimal128x β _FloatN _Complex, where N is 16, 32, 64, or β₯ 128 and a multiple of 32 -->
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)