double 需要多长时间才能在 12 个字节中容纳这么多字符?
How long double fits so many characters in just 12 bytes?
double 需要多长时间才能在 12 个字节中容纳这么多字符?
我做了一个例子,一个C++阶乘
当输入一个大数字时,例如 1754,它计算的数字显然不适合 long double 类型。
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
unsigned int n;
long double fatorial = 1;
cout << "Enter number: ";
cin >> n;
for(int i = 1; i <=n; ++i)
{
fatorial *= i;
}
string s = to_string(fatorial);
cout << "Factorial of " << n << " = " <<fatorial << " = " << s;
return 0;
}
重要提示:
Windows 上的 GCC 编译器,由 visual Studio long double 表现得像 double
问题是怎么存储的还是to_string函数?
它放不下那么多字符。相反,to_string
从数据中生成那么多字符。
这是一个玩具程序:
std::string my_to_string( bool b ) {
if (b)
return "This is a string that never ends, it goes on and on my friend, some people started typing it not knowing what it was, and now they still are typing it it just because this is the string that never ends, it goes on and on my friend, some people started typing it not knowing what it was, and now they still are typing it just because...";
else
return "no it isn't, I can see the end right ^ there";
}
bool
正好存储 1 位数据。但是它通过调用 my_to_string
生成的字符串可以任意长。
double
的to_string
就是这样。它生成的字符比 double
.
中的 "information" 多得多
这是因为它在输出时被编码为以 10 为基数的数字。在 double
内部,它被编码为无符号数、符号位和指数部分的组合。
然后 "value" 大致是“1+number/2^常数”,乘以符号的 +/- 1,再乘以“2^指数部分”。
基数2只有一定数量的"bits of precision";如果你以 2 为基数(或十六进制,或任何 power-of-2 基数)打印它,双精度数将有几个 non-zero 数字,然后是一堆 0(或者,如果小,它会有0.0000...000 然后是少数 non-zero 数字)。
但是当转换为10进制时,里面没有一堆零数字。
取 0b10000000——又名 2^8。这是以 10 为基数的 256 -- 它根本没有尾随 0!
std::to_string(factorial)
将 return 包含与 std::sprintf(buf, "%Lf", value)
.
相同结果的字符串
反过来,%Lf
打印 long double
的整个整数部分、句点和小数部分的 6 位小数。
由于 factorial
是一个非常大的数字,所以您最终会得到一个很长的字符串。
不过请注意,这与long double
无关。一个更简单的例子,例如double
是:
#include <iostream>
#include <string>
int main()
{
std::cout << std::to_string(1e300) << '\n';
return 0;
}
这将打印:
10000000000000000525047602 [...300 decimal digits...] 540160.000000
十进制数字不完全为零,因为该数字不完全为 1e300
,而是最接近它的可以用 floating-point 类型表示的数字。
这是因为浮点数只存储实际值的近似值。如果您查看 the actual exact value of 1754!
,您会发现在前 ~18 位数字之后您的结果变得完全不同。后面的数字就是在十进制中写出2的大次方(的倍数)的结果。
double 需要多长时间才能在 12 个字节中容纳这么多字符?
我做了一个例子,一个C++阶乘 当输入一个大数字时,例如 1754,它计算的数字显然不适合 long double 类型。
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
unsigned int n;
long double fatorial = 1;
cout << "Enter number: ";
cin >> n;
for(int i = 1; i <=n; ++i)
{
fatorial *= i;
}
string s = to_string(fatorial);
cout << "Factorial of " << n << " = " <<fatorial << " = " << s;
return 0;
}
重要提示: Windows 上的 GCC 编译器,由 visual Studio long double 表现得像 double
问题是怎么存储的还是to_string函数?
它放不下那么多字符。相反,to_string
从数据中生成那么多字符。
这是一个玩具程序:
std::string my_to_string( bool b ) {
if (b)
return "This is a string that never ends, it goes on and on my friend, some people started typing it not knowing what it was, and now they still are typing it it just because this is the string that never ends, it goes on and on my friend, some people started typing it not knowing what it was, and now they still are typing it just because...";
else
return "no it isn't, I can see the end right ^ there";
}
bool
正好存储 1 位数据。但是它通过调用 my_to_string
生成的字符串可以任意长。
double
的to_string
就是这样。它生成的字符比 double
.
这是因为它在输出时被编码为以 10 为基数的数字。在 double
内部,它被编码为无符号数、符号位和指数部分的组合。
然后 "value" 大致是“1+number/2^常数”,乘以符号的 +/- 1,再乘以“2^指数部分”。
基数2只有一定数量的"bits of precision";如果你以 2 为基数(或十六进制,或任何 power-of-2 基数)打印它,双精度数将有几个 non-zero 数字,然后是一堆 0(或者,如果小,它会有0.0000...000 然后是少数 non-zero 数字)。
但是当转换为10进制时,里面没有一堆零数字。
取 0b10000000——又名 2^8。这是以 10 为基数的 256 -- 它根本没有尾随 0!
std::to_string(factorial)
将 return 包含与 std::sprintf(buf, "%Lf", value)
.
反过来,%Lf
打印 long double
的整个整数部分、句点和小数部分的 6 位小数。
由于 factorial
是一个非常大的数字,所以您最终会得到一个很长的字符串。
不过请注意,这与long double
无关。一个更简单的例子,例如double
是:
#include <iostream>
#include <string>
int main()
{
std::cout << std::to_string(1e300) << '\n';
return 0;
}
这将打印:
10000000000000000525047602 [...300 decimal digits...] 540160.000000
十进制数字不完全为零,因为该数字不完全为 1e300
,而是最接近它的可以用 floating-point 类型表示的数字。
这是因为浮点数只存储实际值的近似值。如果您查看 the actual exact value of 1754!
,您会发现在前 ~18 位数字之后您的结果变得完全不同。后面的数字就是在十进制中写出2的大次方(的倍数)的结果。