解释c++中非规范化双精度对象的输出

Interprete the print out of a denormalized double in C++

本文关键字:对象 输出 双精度 规范化 c++ 非规范 解释      更新时间:2023-10-16

当c++程序打印出以下数字时,这是什么意思?最后的H是什么意思?

-6.38442 e - 86 h

整个系统太大,不能在这里添加,但是这里是打印特定双精度数的代码。

try{
    newLogLikelihoodEM= hmm->learningLogLikelihood(data, Arglist::getDiffLogLikelihood(), fileNumbers, rng);
}
catch (SingularCovarianceMatrixException &scme)
{
    std::cout << scme.what() << ": doing learning, so restarts for this start-point" << std::endl;
    noRestarts++;
    restart = true;
}

和异常类

class SingularCovarianceMatrixException: public std::exception
{
    double det;
public:
    SingularCovarianceMatrixException(double det):det(det){};
    virtual const char* what() const throw()
    {
        std::stringstream msg;
        msg<< "Singular covariance matrix: determinant="<<det;
        return msg.str().c_str();
    }
};

异常由

抛出
if(*detCovarianceMatrix<1e-300)
{
    throw SingularCovarianceMatrixException(*detCovarianceMatrix);
}

H不属于数字。这不是浮点数的有效后缀。在你的代码中应该有别的东西打印它

H不是一个有效的浮点后缀,我找不到一个很好的参考来证明这一点,但我们可以显示它不是一个有效的转换使用这个代码:

#include <iostream>
#include <sstream>
#include <string>
#include <stdexcept>
class BadConversion : public std::runtime_error {
public:
  BadConversion(std::string const& s)
    : std::runtime_error(s)
    { }
};
inline double convertToDouble(std::string const& s,
                              bool failIfLeftoverChars = true)
{
  std::istringstream i(s);
  double x;
  char c;
  if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
    throw BadConversion("convertToDouble("" + s + "")");
  return x;
}
int main()
{
    double d1 = convertToDouble("-6.38442e-86") ;
    std::cout << d1 << std::endl ;
    d1 = convertToDouble("-6.38442e-86H");
    std::cout << d1 << std::endl ;
}

这是我从我以前的一个关于如何检查string是否为integer的答案中的代码。