Hexadecimal formatted I/O for floating point numbers

In C/C++, this is a,A format.

Are the plans to implement this in Chapel, at least for output?

If not, could I be pointed at the appropriate routine to enhance to achieve this please?

Hi Damian -

This functionality already exists and should be stable.

Here is an example to get you started:

use IO;

config const num: real = 1.25;

// use an x prefix character in the format string to request hexadecimal
writef("%xr\n", num);

// works also with .format 
var str: string = "%xr".format(num);
writeln(str);

// this part demonstrates it can be read back in and gets the same number
var f = openTempFile();
f.writer().writef("%xr", num);

var num2: real;
f.reader().readf("%xr", num2);

assert(num == num2);

See also the docs at FormattedIO — Chapel Documentation 1.32 .

Best,

-michael

1 Like

Sorry, silly me. Thanks.

What is the difference between "%xer" and "%xr" please?

That table to help C programmers should have an example where '%a" maps to "%xr" or something similar. Sorry for making work.

That table to help C programmers should have an example where '%a" maps to "%xr" or something similar. Sorry for making work.

Yeah, there are many things that seem like they should be in that table when one looks for it; but we also don't want that table to be overwhelmingly long. Anyway, feel free to create a PR with the proposed change or an issue if you just want us to put it on the TODO list.

What is the difference between "%xer" and "%xr" please?

For non-hexadecimal, %er asks for it to always print out with an exponent, where %r might not print an exponent. I'm not aware of e making any difference at all when combined with x though. AFAIK %xer and %xr do the same thing.

Thanks. That's what I was observed too.