简体   繁体   English

将十进制数转换为64位浮点双精度二进制数?

[英]a decimal number into 64 bit floating point double precision binary?

So Ive got a code (from another thread) to convert decimal into floating point binary, problem is its still in 32-bit. 所以我有一个代码(从另一个线程)将十进制转换为浮点二进制,问题是它仍然在32位。

#include <iostream>
#include <bitset>
using namespace std;
int main() {
    union {
        float input;   
        int output;
        }  data;
    float x;
    cin>>x;
    data.input = x;

    bitset<sizeof(float) * CHAR_BIT>   bits(data.output); 
    cout << bits << endl;
  }

how do i change it into a 64-bit floating point binary? 如何将其更改为64位浮点二进制文件?

template< std::size_t N > template <std :: size_t N>
class bitset; 类位集;

This is how bitset Defined in header file. 这是在头文件中定义位集的方式。 So if you want 64 bit. 因此,如果您想要64位。 Just type 64 inside the bitset. 只需在位集中输入64。 bitset<64> will give you 64 bit output for your desired input. bitset <64>将为您所需的输入提供64位输出。

Here is the modified code: 这是修改后的代码:

#include <iostream>
#include <bits/stdc++.h>
#include <bitset>
using namespace std;
int main() {
    union {
        float input;   
        int output;
        }  data;
    float x;
    cin>>x;
    data.input = x;

    bitset<64>   bits(data.output); 
    cout << bits << endl;
}

Input 输入

2.25125

Output: 输出:

0000000000000000000000000000000001000000000100000001010001111011

Online compiler link : http://cpp.sh/832cr 在线编译器链接http : //cpp.sh/832cr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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