[Cfp-interest] freestanding

Stephen Canon scanon at apple.com
Mon Feb 27 06:21:17 PST 2012


On Feb 26, 2012, at 9:33 PM, "Joel C. Salomon" <joelcsalomon at gmail.com> wrote:

> On 02/26/2012 06:20 PM, Jim Thomas wrote:
> 
>> An ideal specification of strfromf would convert float  to a character sequence directly, without first promoting x to double, as would happen with snprintf. The difference can show up in %a formatting. But such a specification would be a fair amount more complicated. I'm inclined to add strfromf, using the same snprintf definition as for the other two strfrom functions and noting the implicit promotion to double. If it becomes troublesome, we could easily remove it later. What do you think?
> 
> Dilemma:  If strfromf(f) is indistinguishable from strfromd((double)f),
> it doesn't make much sense to define it; defining strfromf in the
> "reasonable" way could get complicated -- but my reading of 754 requires
> (or at least strongly suggests) that the function exist.

I don't see any requirement in IEEE-754 that there be a distinct function strfromf( ).  It is required that an implementation provide the operations of converting a single-precision number to both decimal and hex character sequences, but there is no requirement that this be implemented by a function distinct from that for double-precision numbers (or, for that matter, that it be implemented by a function at all).

What I think Jim is getting at is that one might like the %a format specifier to behave differently for single-precision inputs than for double-precision.  Double is 53 bits wide, and the %a format specifier naturally prints normalized double-precision values as 0x1.XXXXXXXXXXXXXpEEEE.  Because 53 = 1 + 13*4, all the bits of the hexadecimal digits have meaning.  However, a single-precision value is only 24 bits wide; converted to a double before printing with %a, you get something of the form 0x1.XXXXXYpEEE, where the low bit of the trailing hexadecimal digit is always zero, so Y can only be 0,2,4,6,8,a,c,e.  Some people would like for strfromf( ) to align the significand against a hexadecimal digit boundary instead, so that values would print as 0xX.XXXXXpEEE.  (Personally, I rather prefer hex floating-point values to start with 0x1., but I can see the other argument).  Of course, if we had strfromf, users could get my preferred behavior by simply calling strfromd instead.

- Steve


More information about the Cfp-interest mailing list