简体   繁体   中英

Is there any way to create a floating point number without using ldexp?

我正在尝试使用符号,指数和尾数创建一个IEEE754浮点数,但我似乎无法让ldexp()函数在我的计算机上工作,所以我想知道是否有办法通过以下方式创建该数字:直接操纵位的值。

One standard-ish idiom for messing with value representations is to work with your bits as part of an int or char array, and then memcpy() that into your intended type.

Note that doing what you ask through writing one field of a union and reading another, or through type-punning (casting and dereferencing pointers from one type to another, other than char* ) is technically undefined behavior under the C++ standard, and so should be avoided. Compilers are known to apply optimizations resulting from assumptions that that programs don't execute these behaviors, and lead to unexpected behavior when they do.

For the exact instance of pointer casting considered here, the Clang/LLVM developers have published in a blog post that this is undefined behavior that they may optimize in unexpected ways.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM