Java で浮動小数点値を処理する場合、このtoString()
メソッドを呼び出すと、浮動小数点の有効数字の数が正しい値が出力されます。ただし、C++ では、float ビアを出力stringstream
すると、値が 5 桁以下で丸められます。C++ の浮動小数点数を (想定される) 有効数字の正しい数に "きれいに印刷" する方法はありますか?
編集:誤解されていると思います。出力を固定精度ではなく、動的な長さにしたい。私はsetprecisionに精通しています。Double の Java ソースを見ると、何らかの方法で有効桁数が計算されます。それがどのように機能するか、および/またはこれを C++ で簡単に複製することがどれほど実現可能かを理解したいと思います。
/*
* FIRST IMPORTANT CONSTRUCTOR: DOUBLE
*/
public FloatingDecimal( double d )
{
long dBits = Double.doubleToLongBits( d );
long fractBits;
int binExp;
int nSignificantBits;
// discover and delete sign
if ( (dBits&signMask) != 0 ){
isNegative = true;
dBits ^= signMask;
} else {
isNegative = false;
}
// Begin to unpack
// Discover obvious special cases of NaN and Infinity.
binExp = (int)( (dBits&expMask) >> expShift );
fractBits = dBits&fractMask;
if ( binExp == (int)(expMask>>expShift) ) {
isExceptional = true;
if ( fractBits == 0L ){
digits = infinity;
} else {
digits = notANumber;
isNegative = false; // NaN has no sign!
}
nDigits = digits.length;
return;
}
isExceptional = false;
// Finish unpacking
// Normalize denormalized numbers.
// Insert assumed high-order bit for normalized numbers.
// Subtract exponent bias.
if ( binExp == 0 ){
if ( fractBits == 0L ){
// not a denorm, just a 0!
decExponent = 0;
digits = zero;
nDigits = 1;
return;
}
while ( (fractBits&fractHOB) == 0L ){
fractBits <<= 1;
binExp -= 1;
}
nSignificantBits = expShift + binExp +1; // recall binExp is - shift count.
binExp += 1;
} else {
fractBits |= fractHOB;
nSignificantBits = expShift+1;
}
binExp -= expBias;
// call the routine that actually does all the hard work.
dtoa( binExp, fractBits, nSignificantBits );
}
この関数の後、dtoa( binExp, fractBits, nSignificantBits );
一連のケースを処理する呼び出しが行われます - これは OpenJDK6 からのものです
より明確にするために、例: Java:
double test1 = 1.2593;
double test2 = 0.004963;
double test3 = 1.55558742563;
System.out.println(test1);
System.out.println(test2);
System.out.println(test3);
出力:
1.2593
0.004963
1.55558742563
C++:
std::cout << test1 << "\n";
std::cout << test2 << "\n";
std::cout << test3 << "\n";
出力:
1.2593
0.004963
1.55559