C++ 如何将float或double转换为以非标准基表示的字符串 #包括 #包括 使用名称空间std; main() { 浮动输入=FLT_最大值; std::ostringstream buff; 抛光精度(39); buff

C++ 如何将float或double转换为以非标准基表示的字符串 #包括 #包括 使用名称空间std; main() { 浮动输入=FLT_最大值; std::ostringstream buff; 抛光精度(39); buff,c++,string,numbers,C++,String,Numbers,我没有测试程序,但我认为这应该可以: #include <iostream> #include <math.h> using namespace std; main() { float input = FLT_MAX; cout.precision(39); while (floor(input) != 0) { cout << input << endl; input /= 10; } } #包括

我没有测试程序,但我认为这应该可以:

#include <iostream>
#include <math.h>

using namespace std; 
main()
{
  float input = FLT_MAX;
  cout.precision(39); 

  while (floor(input) != 0)
  {
    cout << input << endl;
    input /= 10;
  }

}
#包括
#包括
char GetChar(int num)
{
如果(num>base;
双d;
标准:cin>>d;
自动i=(长)d;
d-=i;
std::string res;
而(i)
res+=GetChar(i%base),i/=base;
std::reverse(res.begin(),res.end());
res+=';
//在出现重复分数时停止。
int最大精度=30;
做
{
d*=基准;
i=(长)d;
d-=i;
res+=GetChar(i);
}而(d>0&&MaximumPrecision--);

std::cout标准流不会对浮点数字IIRC执行此操作。您必须自己将它们转换为字符串。以什么形式打印?符号、分数和指数?十进制,可能还有指数,例如0.333̅₍₁₀₎ = 0.1₍₃₎, 或1e-1₍₃₎ (想不出二进制或列出的其他系统中的示例)做好这件事真的很难。人们获得博士学位学习这类东西。做一份半途而废的工作相当容易,如果这足够好,你在这里得到的答案可能会有用。谢谢,但这对d=DBL_MAX不起作用,这对我来说很重要,因为我正在编写一个程序,该程序应该显示每个基本数据的限制a型。
#include <iostream>
#include <math.h>

using namespace std; 
main()
{
  float input = FLT_MAX;
  cout.precision(39); 

  while (floor(input) != 0)
  {
    cout << input << endl;
    input /= 10;
  }

}
#include <iostream>
#include <algorithm>

char GetChar(int num)
{
    if (num <= 9) return '0' + num;
    return 'A' + num - 10;
}

int main()
{
    int base; std::cin >> base;
    double d;
    std::cin >> d;

    auto i = (long long)d;
    d -= i;

    std::string res;

    while (i)
        res += GetChar(i % base), i /= base;

    std::reverse(res.begin(), res.end());
    res += '.';

    // to stop in case of a repeating fraction.
    int MaximumPrecision = 30;

    do
    {
        d *= base;
        i = (long long)d;
        d -= i;
        res += GetChar(i);

    } while (d > 0 && MaximumPrecision--);

    std::cout << res;
}