C++计算 double 的位数

发布于 2024-10-05 06:59:15 字数 234 浏览 0 评论 0原文

我想做这样的标题:

int number1;
cin>>number1;
num1len=log10(number1)+1;
cout<<"num of digits is "<<num1len<<"\n";

但是当位数为 11 或更多时,答案始终是 7(6+1)

有谁知道为什么或我做错了什么?

i want to do what the title says like this:

int number1;
cin>>number1;
num1len=log10(number1)+1;
cout<<"num of digits is "<<num1len<<"\n";

but when the number of digits is 11 and more the answer is always 7(6+1)

Does anyone knows why or what im i doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

在梵高的星空下 2024-10-12 06:59:15

浮点数据类型,包括double,存储近似值。通过调用 log10 可以发现小数点左边的位数,它最多受近似过程影响 1。

你问的问题,如何找到以二进制浮点数存储的数字的十进制位数,是没有意义的。数字 7.1 有两位十进制数字,但其近似浮点表示根本不使用十进制数字。要保留十进制位数,您需要一些十进制表示形式,而不是 C++ double 数据类型。

当然,根据问题标题,所有这些仅适用于 double。您的代码片段实际上并未使用double

Floating-point data types, including double, store approximations. What you're finding by calling log10 is the number of places to the left of the decimal point, which is affected by at most one by the approximation process.

The question you asked, how to find the number of decimal digits in a number stored in binary floating-point, is meaningless. The number 7.1 has two decimal digits, however its approximate floating-point representation doesn't use decimal digits at all. To preserve the number of decimal digits, you'd need some decimal representation, not the C++ double data type.

Of course, all of this is applicable only to double, per the question title. Your code snippet doesn't actually use double.

半﹌身腐败 2024-10-12 06:59:15

“错误”的是可以存储在(有符号)int中的最大值:

#include <iostream>
#include <numeric>

int main()
{
    std::cout << std::numeric_limits<int>::max() << std::endl;
}

给我:

2147483647

What is 'wrong' is the maximum value which can be stored in a (signed) int :

#include <iostream>
#include <numeric>

int main()
{
    std::cout << std::numeric_limits<int>::max() << std::endl;
}

Gives me :

2147483647

旧瑾黎汐 2024-10-12 06:59:15

您正在运行超过无符号 32 位边界...您的 11 位或更多数字超过 0xFFFFFFFF,因此回绕。

您需要为 number1 变量使用 unsigned long longdouble

#include <iostream>
#include <cstdlib>
#include <cmath>

int
main ( int argc, char * argv[] )
{
  unsigned long long num; // or double, but note comments below
  std::cin >> num;
  std::cout << "Number of digits in " << num << " is " << ( (int) std::log10 ( num ) + 1 ) << std::endl;
  return 0;
}

这些大数字在发送时默认会以科学记数法打印如果您选择使用 double 作为数据类型,则为 std::cout ,因此您需要在其中添加一些格式。如果您使用 unsigned long long 代替,它们将按照输入的方式打印,但您必须确保您的平台支持 unsigned long long

编辑:正如其他人提到的,使用浮点值还有其他需要考虑的含义,并且很可能不是您最终想要实现的目标。 AFAIK,产生最大正值的平台上的整数类型是 unsigned long long,因此根据您要使用的值,看看它是否可供您使用。

You are running past the unsigned 32-bit boundary ... your number of 11 digits or more exceeds 0xFFFFFFFF, and so wraps around.

You need to use either unsigned long long or double for your number1 variable:

#include <iostream>
#include <cstdlib>
#include <cmath>

int
main ( int argc, char * argv[] )
{
  unsigned long long num; // or double, but note comments below
  std::cin >> num;
  std::cout << "Number of digits in " << num << " is " << ( (int) std::log10 ( num ) + 1 ) << std::endl;
  return 0;
}

Those large numbers will print in scientific notation by default when you send them to std::cout if you choose to use double as your data type, so you would want to throw some formatting in there. If you use an unsigned long long instead, they will print as they were entered, but you have to be sure that your platform supports unsigned long long.

EDIT: As mentioned by others, use of floating point values has other implications to consider, and is most likely not what you are ultimately trying to achieve. AFAIK, the integral type on a platform that yields the largest positive value is unsigned long long, so depending on the values you are looking to work with, see if that is available to you for use.

唔猫 2024-10-12 06:59:15

其他人指出,浮点数是近似值,因此您无法真正获得准确的位数。

但是......您可以通过将其写入 std::stringstream 对象,然后将其转换为 std::string 并获取所述字符串的长度来获得近似值。当然,您必须处理字符串中可能存在非数字字符的事实(例如减号、小数点、代表指数的 E 等)。此外,以这种方式获得的位数将取决于写入 stringstream 对象时选择的格式选项。但假设您知道要使用哪些格式选项,则可以根据这些选项获取位数。

Others have pointed out that floating point numbers are approximations, so you can't really get an accurate count of digits in it.

But...you can get something approximate, by writing it out to a std::stringstream object, then converting it to a std::string, and getting the lenght of the said string. You'll of course have to deal with the fact that there may be non-digit characters in the string (like minus sign, decimal point, E for exponent etc). Also the number of digits you obtain in this manner would be dependent on formatting options you choose when writing to the stringstream object. But assuming that you know what formatting options you'd like to use, you can get the number of digits subject to these options.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文