BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

I've got this recollection of there being a hex floating format specifier for s/printf; which I've never had a use for; but now I have, I cannot find any reference to it.

Did I imagine it? Is it a recollection from some other language?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Replies are listed 'Best First'.
Re: Hex floating point format specifier?
by afoken (Chancellor) on Jun 17, 2015 at 19:12 UTC

    I've got this recollection of there being a hex floating format specifier for s/printf; which I've never had a use for; but now I have, I cannot find any reference to it.

    Did I imagine it? Is it a recollection from some other language?

    perlfunc from 5.22 documents %a as "hexadecimal floating point", and %A as "like %a, but using upper-case letters". 5.18 does not know about %a and %A.

    Update: Listed as new feature in perldelta from 5.22.0.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Thanks afoken (and anonymonk). I'd begun to think I'd imagined it. I didn't think to look there (5.22) because it seems like a much older memory.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re: Hex floating point format specifier?
by syphilis (Archbishop) on Jun 18, 2015 at 01:03 UTC
    Not a s/printf specifier but, for pre-5.22, there's Data::Float's float_hex() function:
    C:\>perl -MData::Float="float_hex" -le "print float_hex(1 / 9);" +0x1.c71c71c71c71cp-4
    Cheers,
    Rob

      Thanks for that. I won't be using it directly as I don't need its flexibility and I'll need to extend it for my extended precision numbers; but it made a nice reference.

      This is my version for standard doubles:

      sub fmt{ my $bin = unpack 'Q', pack 'd', $_[0]; my $sign = ( $bin & 0x8000000000000000 ); my $exp = ( $bin & 0x7FF0000000000000 ) >> 52; $exp -= 1023; my $mant = ( $bin & 0x000FFFFFFFFFFFFF ); sprintf "%s0x1.%xp%d", ( $sign ? '-' : '' ), $mant, $exp; }

      Some outputs:

      C:\test>fmt -- -1/9 -0x1.c71c71c71c71cp-4 C:\test>fmt -- 1/9 0x1.c71c71c71c71cp-4 C:\test>fmt -- 1/3 0x1.5555555555555p-2 C:\test>fmt -- 1/11 0x1.745d1745d1746p-4 C:\test>fmt -- "( 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + 1/64 + 1/128 + 1/256 + + 1/512 + 1/1024 + 1/2048 + 1/4096 + 1/8192 + 1/16384 + 1/32768 + 1/ +65536 + 1/131072 + 1/262144 + 1/524288 )" 0x1.ffffc00000000p-1

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re: Hex floating point format specifier?
by ikegami (Patriarch) on Jun 17, 2015 at 19:35 UTC

    An example for the curious:

    $ perl -E'say sprintf "%a", 1/9' 0x1.c71c71c71c71cp-4
Re: Hex floating point format specifier?
by Anonymous Monk on Jun 17, 2015 at 19:20 UTC
    See "Perl v5.22 adds hexadecimal floating point literals" article on "Effective Perl Programming" website (could not post direct link as "Permission Denied". WTF?) for issues with other functions.
Re: Hex floating point format? (Another intimately related question!)
by BrowserUk (Patriarch) on Jun 18, 2015 at 10:01 UTC

    Never mind: I found a non-Perl reference that gave the information I required: "The suffix ‘p-4’, which represents the power of two, written in decimal: 2-4."

    Look as I might, I cannot find anything in the docs or articles linked below that says whether the exponent in the hex floats is also in hex?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re: Hex floating point format specifier?
by stevieb (Canon) on Jun 17, 2015 at 19:12 UTC

    EDIT: I'm sure I misunderstood the OPs question. This can be reaped.

    Are you going from int to hex? If not, can you clarify?

    perldoc -f sprintf

    perl -E '$x="200";$y=sprintf("%.02x", $x); say $y;' c8

    -stevieb