Two questions about "%a"

Nick Sabalausky a at a.a
Thu Mar 3 18:16:50 PST 2011


"Lars T. Kyllingstad" <public at kyllingen.NOSPAMnet> wrote in message 
news:iknfsh$13ga$1 at digitalmars.com...
> On Wed, 02 Mar 2011 13:35:11 +0100, Magnus Lie Hetland wrote:
>
>> First question: I just noticed that writefln("%a", 1.2) writes
>> 0x1.3333333333333p+0, while writeln(format("%a", 1.2)) (that is, with
>> std.string.format) writes 0x9.9999999999998p-3 ... wouldn't it be nice
>> to be consistent here? (The former is what printf in gcc gives.) Or am I
>> missing a difference in functionality?
>
> Hm, that's weird.  I'm pretty sure writefln() is doing the right thing
> here, since that's what printf() does.  I've had a look at the code for
> format(), and it looks to me like it is using some old formatting code
> that is being phased out.
>

Yea, definitely seems to be the case. Unless it's been fixed recently, 
format doesn't even support the posix-style positional argument specifiers 
that were added to writef ages ago (I know I filed a ticket on this, but I 
don't recall the number offhand).

>> Second question: Just to make sure, this *is* an exact representation of
>> the underlying floating-point number? (I.e., if that'w what I'm after,
>> using %a *is* the way to go?)
>
> Yes, that's right.
>

I'm no floating-point expert, but I would think that the only way to get an 
exact representation would be to output the raw data in hex (or binary, or 
octal, etc):

writef("0x%X", cast(ulong)1.2);





More information about the Digitalmars-d-learn mailing list