在Java中處理浮點值時,調用toString()方法會給出一個具有正確浮點有效數字的打印值。但是,在C++中,通過stringstream打印浮點數將在5位或更少位數後四捨五入。有沒有辦法將C++中的float漂亮地打印到(假定的)有效數字的正確數目上?如何計算C++ double的重要小數位數?
編輯:我想我被誤解了。我希望輸出具有動態長度,而不是固定的精度。我熟悉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
的'toString'方法並沒有給出「正確的數字」有效數字;它無法知道哪些數字是重要的。 IEEE浮點數不代表那些信息。 – 2012-07-27 20:41:14
僅供參考,以下是[*自定義格式*](http://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html)的常用Java方法。 – trashgod 2012-07-27 20:44:15
Java在打印時對double值做了一系列的計算 - 我只是不夠了解它在做什麼。見編輯 – dave 2012-07-27 21:26:56