计算没有库的浮点值的小数位数? [英] Calculate number of decimal places for a float value WITHOUT libraries?

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

问题描述

我需要计算浮点值的小数位数,例如

I need to calculate the number of decimal places for a float value, e.g.

1234。 567 -> 3

2. 1233 -> 4

4. 2432 -> 4

1234.567 -> 3
2.1233 -> 4
4.2432 -> 4

我最初的想法是:

number = 1234.567;  
...  
while (number - (int)number > 0.0)  
{  
  // Count decimal places  
  ...  
  number *= 10;  
}

但是,这会导致while条件下的float精度问题。唯一安全的解决方法是从float到string的转换,然后对字符串进行小数点后的基于字符串的计数。

However, this causes problems with float precision in the while-condition. The only safe workaround would be a conversion from float to string and then do string-based counting of decimal places.

问题是:我一定不能使用任何库,第三方或C ++标准库(由于环境限制)。我知道以后如何对char *进行操作,但是如何在不使用C ++库的情况下将浮点值转换为字符串(即char *)?

The problem is: I must NOT use any libraries, neither third party nor C++ standard libraries (due to environmental restrictions). I know how to operate on a char* later on, but how can I convert my float value to a string (i.e. char*) without using C++ libraries?

任何帮助

//编辑:这是我目前的方法,仍然无法使用(例如适用于2.55555) 。如何选择合适的阈值?

// This is my current approach, which still does not work (e.g. for 2.55555). How do I choose a proper threshold?

float abs(float number)  
{  
  return (number > 0.0 ? number : number * -1);  
}  

int round(float number)  
{  
  return (int)(number + 0.5);  
}  

void splitFloat(float* number, int* mantissa, int* exponent)  
{  
  while (abs(*number - round(*number)) > 0.00001)  
  {  
    // *number -= (int)*number; // ???  
    *number *= 10.0;  

    *mantissa = *number;  
    *exponent += 1;  

    cout << "Number: " << *number << ", Mantisse: " << *mantissa << ", Exponent: " << *exponent << endl;  
  }  
}


推荐答案

您的最初的想法很接近,问题在于浮点会进行舍入,从而使结果不准确。您需要使用阈值而不是精确地与0.0进行比较,并且需要允许(int)操作可能截断不正确,而应取整。您可以在截断前加0.5来取整。

Your initial idea is pretty close, the problem is that floating point does rounding that keeps the result from being exact. You need to use a threshold instead of comparing to exactly 0.0, and you need to allow that the (int) operation might truncate incorrectly and you should round instead. You can round by adding 0.5 before truncating.

当数字位数不适合 int时,也会遇到问题。 。您可以通过在每一步中减去数字的整数部分来提供帮助。

You'll also run into a problem when the number of digits doesn't fit into an int anymore. You can help by subtracting off the integer part of the number at each step.

编辑:要选择适当的阈值,请选择最大数量您要处理的小数位数。如果是4,则您要输出的最小数字是 0.0001 。将阈值设为该阈值的一半,即 0.00005 。现在,每次将数字乘以10时,阈值也乘以10!

To choose a proper threshold, pick the maximum number of decimals you want to process. If that's 4, then the smallest number you want to output is 0.0001. Make your threshold half of that, or 0.00005. Now, every time you multiply your number by 10, multiply the threshold by 10 too!

float threshold = 0.00005;
while (abs(*number - round(*number)) > threshold)  
{  
  *number *= 10.0;
  threshold *= 10.0;
  // ...
}

如果您的float和int都32位,您不必担心会减去int。

If your float and int are both 32 bits, you shouldn't have to worry about subtracting out the int. It would make it harder to return the mantissa as you're doing.

此外,我之前想警告您但忘记了:这仅适用于正数。

Also, a warning I meant to give you before but forgot: this only works for positive numbers.

另一个警告是, float 的值范围非常有限。例如,您可能无法准确表示 1234.5670 ,并且最后得到一个多余的数字。更改为 double 可以解决此问题。

One more warning, the range of values for a float is quite limited. You might not be able to exactly represent 1234.5670 for example, and you'll get an extraneous digit on the end. Changing to double would fix that.

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

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