コンパイラ: Windows 8.1 で Nuwen ディストリビューションの 64 ビット MinGW G++ 4.9.1。
コード:
#ifdef INCLUDE_IOSTREAM
# include <iostream>
#endif
#include <stdio.h> // ::snprintf
#include <stdlib.h> // EXIT_SUCCESS, EXIT_FAILURE
#include <stdexcept> // std::exception
#ifdef snprintf
# error snprintf defined as macro
#endif
#ifdef _MSC_VER
auto const snprintf = _snprintf;
#endif
void test( double const value, int const precision)
{
char buffer[34];
snprintf( buffer, sizeof( buffer ), "%.*a", precision, value );
printf( "Hex of %.3f with %2d digits: %s\n", value, precision, buffer );
}
auto main() -> int
{
using namespace std;
try
{
for( int precision = 6; precision <= 8; ++precision )
{
test( 5.0, precision );
}
test( 0.0, 14 );
return EXIT_SUCCESS;
}
catch( exception const& x )
{
fprintf( stderr, "!%s\n", x.what() );
}
return EXIT_FAILURE;
}
Visual C++ で問題なく動作します (ただし、Visual C++ には逆の変換がないようです)。
H:\dev\test\so\0187> cl /nologo- /? 2>&1 | find /i "ler ver" Microsoft (R) C/C++ 最適化コンパイラ バージョン 18.00.30723 for x86 H:\dev\test\so\0187> cl barx.cpp -D INCLUDE_IOSTREAM /Feb barx.cpp H:\dev\test\so\0187> b 6 桁の Hex 5.000: 0x1.400000p+2 7 桁の Hex 5.000: 0x1.4000000p+2 8 桁の Hex 5.000: 0x1.40000000p+2 14 桁の 16 進数 0.000: 0x0.00000000000000p+0 H:\dev\test\so\0187>_
が含まれて<iostream>
いない場合、g++ でも正常に動作します。
H:\dev\test\so\0187> g++ --バージョン | 「++」を見つける g++ (GCC) 4.9.1 H:\dev\test\so\0187> g++ -std=c++11 barx.cpp H:\dev\test\so\0187> a 5.000 の 6 桁の 16 進数: 0x1.400000p+2 5.000 の 7 桁の 16 進数: 0x1.4000000p+2 5.000 の 8 桁の 16 進数: 0x1.40000000p+2 14 桁の 0.000 の 16 進数: 0x0.00000000000000p+0 H:\dev\test\so\0187>_
が含まれている場合にハングする奇妙な結果<iostream>
:
H:\dev\test\so\0187> g++ -std=c++11 -D INCLUDE_IOSTREAM barx.cpp H:\dev\test\so\0187> a 6 桁の 5.000 の 16 進数: 0xa.000000p-1 7 桁の 5.000 の 16 進数: 0xa.0000000p-1 5.000 の 8 桁の 16 進数: 0x0.00000000p-33 ← 変。 ^C ← ハング、Ctrl+C H:\dev\test\so\0187>_
修正または回避策を求めています。