c42f / tinyformat

Minimal, type safe printf replacement library for C++
537 stars 75 forks source link

Use std::hexfloat for %a where available #17

Closed c42f closed 5 years ago

c42f commented 10 years ago

C++11 has an equivalent for %a - should use that where available. Need to figure out which compiler versions support it.

c42f commented 10 years ago

This still appears to be missing as of g++-4.8.2 and clang++-3.5, but actually present as of VS 2012 (or so? hard to find on the web). Will need to wait for compiler support which isn't much of an issue as it's a pretty obscure feature.

According to http://www.cplusplus.com/reference/ios/hexfloat, the hexfloat flag is actually equal to the odd combination std::ios::scientific | std::ios::fixed, so maybe that could be used without breaking the compile on non conforming compilers. Would still want to error at runtime if someone used it on those compilers though...

willwray commented 5 years ago

Time to revisit this, on the twentieth anniversary of C99's %a / %A?

C++11 hexfloat modifier will have been fully supported at least since hexadecimal floating-point literals were added in C++17.

cppreference lists compiler support for hex float literal as:

C++17 feature Paper(s) Version GCC Clang MSVC EDG eccp Intel C++ Portland Group (PGI)  
Hexadecimal floating-point literals P0245R1 c++17-lang 3.0 Yes 19.11
VS2017 15.3
4.13 18.0 17.7

(Support unknown for IBM XLC++, Sun/Oracle C++, Embarcadero C++ Builder, Cray)

c42f commented 5 years ago

I'm happy to accept a patch for this if you want the feature. I think the simplest way to not break anything would be to use the combination of scientific | fixed and just assume that's recognized.

By the way, I don't think having hexfloat literal support in a compiler frontend implies support for hexfloat output in the iostreams standard library. Yes, it would "kind of crazy", but I also wouldn't be completely surprised if some compiler / std library combinations were inconsistent in this.

willwray commented 5 years ago

Testing GCC & Clang support for the scientific | fixed combo, on wandbox online compiler:

Supported since GCC 5.1 -std=c++98

Supported since at least Clang 3.1 -std=c++98 (as far back as wandbox goes).

For earlier GCC releases the combo has no effect (std::hexfloat is accepted with -std=c++11 in GCC 5.1 but gives a compile fail in earlier releases).

I believe that MSVC has had good support for a long time, but have no direct evidence.

So, yes, this is the way to go - using the combo should cause no breakage. I'll open a PR when ready.

FYI, my use case is for textual encoding of floating-point constants in a language backend target. Lossless conversion is needed double -> text -> double.

c42f commented 5 years ago

Sounds great. Is the language backend OSL by any chance?

c42f commented 5 years ago

Fixed in #53

willwray commented 5 years ago

Thanks for the merge.

The project is Hilti, a backend language for parsing protocols and binary formats. OSL - Open Shading Language, right. Used to do some graphics, would like to do more.

I'll bump our version now and use the %a hexfloat spec.

c42f commented 5 years ago

Hah, nice. I assume it's this hilti http://www.icir.org/robin/papers/imc14-hilti.pdf (or successor).

(Correct, that OSL. An easy (if wrong) guess because OSL uses OpenImageIO as infrastructure and tinyformat was originally written out of frustration while trying to support MSVC in OpenImageIO's printf wrapper.)