ver 1.0
目前只能处理小浮点数(abs{Value}≈1018)
我认为这一版本不稳定,并且效率和准确性都不是很好,而且代码极长,正在改进中…
C++Fast_Ostream& operator<<(double x) {
if (x < 0)
return *this << '-' << -x;
flush();
*this << int(x);
if (_SetP) {
char* _ = new char[_precision];
unsigned pew = 1, Npre = _precision;
for (int i = 0; i < _precision; ++i)
_[i] = int(x * (pew *= 10)) % 10 ^ 48;
_[_precision - 1] += (int(x * _Ppow * 10) % 10 > 4);
for (int i = _precision - 1; _[i] == 58 && i; --i) {
_[i] = 0;
++_[i - 1];
}
if (*_ == 58) {
delete[] _;
len = 0;
return *this << unsigned(x + 1);
}
*this << '.';
for (int i = 0; i < _precision; ++i)
*this << _[i];
delete[] _;
return *this;
}
if (int(x * _Ppow) % 10) {
char* _ = new char[_precision];
unsigned pew = 1, Npre = _precision;
for (int i = 0; i < _precision; ++i)
_[i] = int(x * (pew *= 10)) % 10 ^ 48;
_[_precision - 1] += (int(x * _Ppow * 10) % 10 > 4);
for (int i = _precision - 1; _[i] == 58 && i; --i) {
_[i] = 0;
++_[i - 1];
}
if (*_ == 58) {
delete[] _;
len = 0;
return *this << unsigned(x + 1);
}
*this << '.';
for (int i = _precision - 1; i && !_[i]; --i)
--Npre;
for (int i = 0; i < Npre; ++i)
*this << _[i];
delete[] _;
return *this;
}
for (int y = _Ppow / 10; y; y /= 10)
if (int(x * y) % 10) {
*this << '.';
for (int i = 10; i < y; i *= 10)
*this << char(int(x * i) % 10 ^ 48);
return *this << char(int(x * y) % 10 ^ 48);
}
return *this;
}