对于十位数以上的小数,此程序无法正常工作吗? [英] This program doesn't work properly for decimals more than 10 digits?

查看:63
本文介绍了对于十位数以上的小数,此程序无法正常工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码用于计算给定十进制数的位数。问题是它不会计算超过10的数字。

The below code is used to count number of digit in a given decimal. The problem is that it doesn't count digits more than 10.

int NumDigits(int n) {
  int digits = 0;
  if (n <= 0) {
    n = -n;
    ++digits;
  }

  while (n) {
    n /= 10;
    ++digits;
  }

  return digits;
}


推荐答案

似乎您的工具链上有32位 int 类型。这种类型可表示的最大值为 2 31 -1 或2,147,483,647。如您所见,这是一个10位数字。如果要使用这种算法,则需要使用支持更大数字的其他类型。

It seems like your toolchain has a 32-bit int type. The maximum value representable in such a type is 231-1, or 2,147,483,647. As you can see, that's a 10-digit number. You'll need to use a different type that supports larger numbers if you want to use this kind of an algorithm.

这篇关于对于十位数以上的小数,此程序无法正常工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆