2010-04-02

floating pointの仮数部を取る

C++では、floating pointの内部表現は、実装依存である。ExponentやFractionを得る、ポータブルな方法はない。

ただし、numeric_limitsに、is_iec559というメンバーがある。これがtrueの場合、IEC 559 Standard準拠であることを意味する。

ということは、もし、is_iec559がtrueであれば、float、double、long doubleのうち、サイズが32bitか、64bitである型は、ビット演算を使うことによって、ポータブルに内部表現を調べられる。

しかし、1 byte = 8 bitである保証はないので、やはり、ポータブルなコードは書けない。

最も、exponentやfractionを取得することを前提としたコードは、もとよりポータブルには書けない。どうでもいいといえば、どうでもいいのかもしれないが。

No comments:

Post a Comment

You can use some HTML elements, such as <b>, <i>, <a>, also, some characters need to be entity referenced such as <, > and & Your comment may need to be confirmed by blog author. Your comment will be published under GFDL 1.3 or later license with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.