计算浮点数中“."之后的位数? [英] Count number of digits after `.` in floating point numbers?

查看:19
本文介绍了计算浮点数中“."之后的位数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一道面试题.你如何计算浮点数中 . 之后的位数.

This is one interview question. How do you compute the number of digit after . in floating point number.

例如如果给定 3.554 输出=3

e.g. if given 3.554 output=3

对于 43.000 输出=0.我的代码片段在这里

for 43.000 output=0. My code snippet is here

double no =3.44;
int count =0;
while(no!=((int)no))
{
    count++;
    no=no*10;
}
printf("%d",count);

有些数字不能用float类型表示.例如float类型中没有73.487,c中float表示的数字为73.486999999999995近似它.

There are some numbers that can not be indicated by float type. for example, there is no 73.487 in float type, the number indicated by float in c is 73.486999999999995 to approximate it.

现在如何解决它,因为它正在无限循环中.

Now how to solve it as it is going in some infinite loop.

注意:在 IEEE 754 规范中,32 位浮点数被划分为 24+7+1 位.7位表示尾数.

Note : In the IEEE 754 Specifications, a 32 bit float is divided as 24+7+1 bits. The 7 bits indicate the mantissa.

推荐答案

问题并没有真正解决,因为浮点通常用二进制而不是十进制表示.正如您所说,许多(实际上是大多数)十进制数不能完全用浮点数表示.

The problem isn't really solvable as stated, since floating-point is typically represented in binary, not in decimal. As you say, many (in fact most) decimal numbers are not exactly representable in floating-point.

另一方面,所有 可以精确表示为二进制浮点数的数字都是具有有限位数的小数 - 但如果您想要 2 的结果,这并不是特别有用代码>3.44.

On the other hand, all numbers that are exactly representable in binary floating-point are decimals with a finite number of digits -- but that's not particularly useful if you want a result of 2 for 3.44.

当我运行你的代码片段时,它说 3.44 在小数点后有 2 位数字 - 因为 3.44 * 10.0 * 10.0 恰好恰好产生 344.0.对于像 3.43 之类的其他数字(我还没有尝试过),这可能不会发生.

When I run your code snippet, it says that 3.44 has 2 digits after the decimal point -- because 3.44 * 10.0 * 10.0 just happens to yield exactly 344.0. That might not happen for another number like, say, 3.43 (I haven't tried it).

当我尝试使用 1.0/3.0 时,它会进入无限循环.添加一些 printf 表明 no 在 17 次迭代后恰好变为 33333333333333324.0 —— 但该数字太大而无法表示为 int (至少在我的系统上),并将其转换为 int 具有未定义的行为.

When I try it with 1.0/3.0, it goes into an infinite loop. Adding some printfs shows that no becomes exactly 33333333333333324.0 after 17 iterations -- but that number is too big to be represented as an int (at least on my system), and converting it to int has undefined behavior.

对于大数,反复乘以 10 将不可避免地给您带来浮点溢出.有一些方法可以避免这种情况,但它们并不能解决其他问题.

And for large numbers, repeatedly multiplying by 10 will inevitably give you a floating-point overflow. There are ways to avoid that, but they don't solve the other problems.

如果您将值 3.44 存储在 double 对象中,则存储的实际值(至少在我的系统上)恰好是 3.439999999999999946709294817992486059665679931640625,它的小数部分有 51 个十进制数字.假设您真的想要计算 3.439999999999999946709294817992486059665679931640625 中的点之后的小数位数.由于 3.443.4399999999999999946709294817992486059665679931640625 实际上是相同的数字,因此任何 C 函数都无法区分它们并知道它应该返回 2 还是51(或 50,如果您的意思是 3.43999999999999994670929481799248605966567993164062,或 ...).

If you store the value 3.44 in a double object, the actual value stored (at least on my system) is exactly 3.439999999999999946709294817992486059665679931640625, which has 51 decimal digits in its fractional part. Suppose you really want to compute the number of decimal digits after the point in 3.439999999999999946709294817992486059665679931640625. Since 3.44 and 3.439999999999999946709294817992486059665679931640625 are effectively the same number, there's no way for any C function to distinguish between them and know whether it should return 2 or 51 (or 50 if you meant 3.43999999999999994670929481799248605966567993164062, or ...).

您可能会检测到存储的值足够接近"3.44,但这使它成为一个更复杂的问题——它失去了确定小数位数的能力3.439999999999999946709294817992486059665679931640625的小数部分.

You could probably detect that the stored value is "close enough" to 3.44, but that makes it a much more complex problem -- and it loses the ability to determine the number of decimal digits in the fractional part of 3.439999999999999946709294817992486059665679931640625.

只有当给定的数字以某种格式存储时才有意义代表.

The question is meaningful only if the number you're given is stored in some format that can actually represent decimal fractions (such as a string), or if you add some complex requirement for determining which decimal fraction a given binary approximation is meant to represent.

可能有一种合理的方法来执行后者,方法是查找在给定浮点类型中最接近的近似值是给定二进制浮点数的唯一小数.

There's probably a reasonable way to do the latter by looking for the unique decimal fraction whose nearest approximation in the given floating-point type is the given binary floating-point number.

这篇关于计算浮点数中“."之后的位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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