digitalmars.D.learn - Two questions about "%a"
- Magnus Lie Hetland <magnus hetland.org> Mar 02 2011
- "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> Mar 02 2011
- "Nick Sabalausky" <a a.a> Mar 03 2011
- Magnus Lie Hetland <magnus hetland.org> Mar 04 2011
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?
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?)
--
Magnus Lie Hetland
http://hetland.org
Mar 02 2011
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. I've created a bug report for this, and will look into fixing it shortly: http://d.puremagic.com/issues/show_bug.cgi?id=5687Second 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. -Lars
Mar 02 2011
"Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> wrote in message news:iknfsh$13ga$1 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);
Mar 03 2011
On 2011-03-04 03:16:50 +0100, Nick Sabalausky said: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);
That's also an option, certainly. Then I could just write it as a decimal integer, I guess. But the point of hex-literals for floats is precicely that they can represent floats exactly (even though it may not *look* like it, as in this case, because lots of digits are required to approximate the decimal value 1.2). So using those shold be safe -- and perhaps more easily read on other platforms, for example (byte order), or into floats with other precisions or the like. Thanks for the suggestion, though :) -- Magnus Lie Hetland http://hetland.org
Mar 04 2011








Magnus Lie Hetland <magnus hetland.org>