@Sakychu@lemmy.world
link
fedilink
8
edit-2
10M

Because gnu.org says so: “The exponent in a hexadecimal floating constant is an optionally signed decimal integer that specifies a power of 2 (not 10 or 16) to multiply into the number.” https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Floating-Constants.html

I just learned something, thanks!

From the documentation (emphasis mine):

If the floating literal begins with the character sequence 0x or 0X, the floating literal is a hexadecimal floating literal. Otherwise, it is a decimal floating literal.

For a hexadecimal floating literal, the significand is interpreted as a hexadecimal rational number, and the digit-sequence of the exponent is interpreted as the (decimal) integer power of 2 by which the significand has to be scaled.

double d = 0x1.4p3; // hex fraction 1.4 (decimal 1.25) scaled by 2^3, that is 10.0

You can find the full documentation here: cppreference.com

So in your example 0x1P1 means 116 * 2^(110) = 2

@velox_vulnus@lemmy.ml
creator
link
fedilink
1
edit-2
10M

deleted by creator

There is decimal scientific notation for specifying a decimal exponent already (123e4 = 123×10^4).

And I don’t know why the committee preferred base 2 over base 16, I think it really depends on the use case which one is more useful.

xrs
link
fedilink
110M

Presumably related to binary, at least indirectly.

I’ve never seen P used as an operator and find nothing with a quick Google search. What’s it do?

Edit: What’s it supposed to do/where did it come from?

It’s a C++17 feature that allows specifying a power of two (as decimal exponent) by which the fractional part should be multiplied.

So mPn is m*1<<n? (For integer values of n)

Yeah, you could express it like that

1P1 = 1 * 2^1 = 2

@velox_vulnus@lemmy.ml
creator
link
fedilink
1
edit-2
10M

deleted by creator

Sleepless One
link
fedilink
110M

Hex digits are 0-9 and A-F. I don’t think P is a digit.

@velox_vulnus@lemmy.ml
creator
link
fedilink
1
edit-2
10M

deleted by creator

General Programming Discussion
!programming@lemmy.ml

    A general programming discussion community.

    Rules:

    1. Be civil.
    2. Please start discussions that spark conversation

    Other communities

    Systems

    Functional Programming

    Also related

    • 0 users online
    • 5 users / day
    • 14 users / week
    • 44 users / month
    • 138 users / 6 months
    • 1 subscriber
    • 336 Posts
    • 544 Comments
    • Modlog