2

C++17 allows defining floating point constants in hexadecimal format like

double d1 = 0x1.2p3;    // 9.0
double d2 = 0x1.86Ap+16 // 100000.0

In hexadecimal floating point constants the mantissa and exponent is separated using the letter p|P.

Why C++ standard committee decided to use the letter p|P instead of any other letter?

They could not have used the normal e|E notation in hex floating point constant because letter e|E is part of hexadecimal digit. But they could have used letter g|G which is usually used in printf format specifier for floating point value, or any other letter. Is there any specific reason for using the letter p?

Aamir
  • 1,974
  • 1
  • 14
  • 18
  • I have voted to close this as "opinion based" ... but there are other reasons why this could be closed. What is it that you want, as an answer? – Adrian Mole Jun 08 '22 at 20:46
  • 4
    I think it's part of IEEE 754 see __Hexadecimal literals__ here https://en.wikipedia.org/wiki/IEEE_754 _"...the standard recommends providing conversions to and from external hexadecimal-significand character sequences, based on C99's hexadecimal floating point literals....an exponent indicator "p" ... "_ – Richard Critten Jun 08 '22 at 20:47
  • What is the reasoning for using letter `p` in hexadecimal floating point constant? – Aamir Jun 08 '22 at 20:47
  • `p` for power I'd guess – Alan Birtles Jun 08 '22 at 20:48
  • 3
    @RichardCritten so like most things weird in C++, the answer is "blame C" :) – Taekahn Jun 08 '22 at 20:49
  • Does this answer your question? [hexadecimal floating constant in C](https://stackoverflow.com/questions/4825824/hexadecimal-floating-constant-in-c) – Orace Jun 08 '22 at 20:52
  • @Orace That doesn't explain why `p` was chosen over any other letter, which is what this question is asking. But it does seem more appropriate as a C question, considering that C++ just chose to be consistent with C. – user17732522 Jun 08 '22 at 20:54
  • 1
    `e` wouldn't work because it's a hexadecimal digit. – Richard Hodges Jun 08 '22 at 20:55
  • Could also be the first letter of exponent that was usable with `e` and `x` not being suitable – Alan Birtles Jun 08 '22 at 20:55
  • @RichardHodges The question already acknowledges that, but asks why `g`, which was already used in `printf`/`scanf`, wasn't chosen. – user17732522 Jun 08 '22 at 20:58
  • [The proposal](https://isocpp.org/files/papers/p0245r0.html) says "There is a base-2 scientific notation for floating point values: 0x3.ABCp-10" but I'm having a hard time finding the origin of the notation. – Ryan Haining Jun 08 '22 at 21:00
  • @RyanHaining How is that base-2? Base 2 would be `0b11.1010'1011'1100p-10` – Goswin von Brederlow Jun 08 '22 at 21:15
  • 1
    @GoswinvonBrederlow Quoting the linked proposal: *" The significand is given in hexadecimal, and the exponent is given in decimal and interpreted with respect to base 2"* – Bob__ Jun 08 '22 at 21:18
  • @Bob__ Still odd then, shouldn't it be 0x1.D5Ep-9? Normalized floating point only has 1 bit before the decimal point. – Goswin von Brederlow Jun 08 '22 at 21:24
  • @GoswinvonBrederlow like how 1.5e3 = 1.5 * 10^3, 0x5p3 = 0x5 * 2^3. [run this](https://wandbox.org/permlink/t5MALcrLH15ARF32) which shows 0x1p10 = 1024 – Ryan Haining Jun 08 '22 at 21:34
  • @GoswinvonBrederlow I think that's just an example of valid syntax. I'm not sure whether the choice between different possible representations is somehow specified for [outputted](https://godbolt.org/z/Evb9obY74) values. – Bob__ Jun 08 '22 at 21:50
  • @RyanHaining Yes, it is neat. The hexadecimal part is exactly the mantisse (+leading 1 bit) and the exponent is exactly the exponent in the float/double. It makes it real easy to correctly printf or scanf those numbers. None of the rounding horrors the other formats have to find the nearest number. The format is exact. – Goswin von Brederlow Jun 08 '22 at 21:57

1 Answers1

2

From https://en.wikipedia.org/wiki/Hexadecimal#Hexadecimal_exponential_notation :

By convention, the letter P (or p, for "power") represents times two raised to the power of, [...]

Why C++ standard committee decided to use the letter p|P instead of any other letter?

Because it was so in C. It wasn't in B, so must have been something added to C.

Is there any specific reason for using the letter p?

Not really.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111