比较 C 中的浮点值

发布于 2024-12-09 10:32:45 字数 89 浏览 0 评论 0原文

我想比较浮点值以确定列表中的最大数字。浮点的精度是固定的,小数点后为 6。

我应该将它们作为整数进行比较,如果它们相等,那么就去挖掘小数点后的值吗?

I want to compare float values to determine the biggest number in the list.The precision of the float is fixed, 6 after the decimal.

Should I just compare them as integer and if they are equal then go and dig the values after the decimal ?

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

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

发布评论

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

评论(6

皇甫轩 2024-12-16 10:32:45

比较浮点数的最简单方法是使用 < 运算符,如下所示

if(float1 < float2)
     printf("%f < %f\n", float1, float2);

The easiest way to compare floats is with the < operator, like so

if(float1 < float2)
     printf("%f < %f\n", float1, float2);
焚却相思 2024-12-16 10:32:45

使用 DBL_EPSILON 作为需要接近双打的程度的基础。

使用 FLT_EPSILON 存在于浮点数中。

请参阅此答案以获取演示该技术的示例函数。

use DBL_EPSILON as a basis for how close to doubles need to be.

use FLT_EPSILON exists for floats.

see this answer for a sample function that demonstrates the technique.

屋檐 2024-12-16 10:32:45

如果你想找到最大的数字,为什么还需要担心比较是否相等呢?

If you want to find the biggest number, why would you need to worry about comparing for equality?

夜雨飘雪 2024-12-16 10:32:45

这取决于您没有告诉我们的事情。

  • 这些数字是如何提供给您的?作为字符串,或者它们已经在内存中(浮点数组/双精度数组)?
  • 它们有多大?为了达到一定的精度,是否存在最大边界?也就是说,如果数字是 1 亿,那么 6 位小数是否相关?
  • 失去精度可以吗?

等。

当您在浮点数组中拥有值时,我会执行以下操作:

float biggest(float * values, int num)
{
    int i;
    float curmax;
    if (num == 0) return NAN;
    curmax = values[0];
    for (i=1; i < num; i++) {
        if (values[i] > curmax) curmax = values[i];
    }
    return curmax;
}

That depends on things you don't tell us.

  • How are these numbers provided to you? As strings, or are they already in the memory (float array/double array)?
  • How large are they? Is there any greatest boundary, in order to have a certain precision? I.e., are the 6 decimals relevant if the number is, let's say, 100 millions?
  • Is it ok to lose precision?

etc.

As you have the values in a float array, I would do the following:

float biggest(float * values, int num)
{
    int i;
    float curmax;
    if (num == 0) return NAN;
    curmax = values[0];
    for (i=1; i < num; i++) {
        if (values[i] > curmax) curmax = values[i];
    }
    return curmax;
}
像你 2024-12-16 10:32:45

执行此操作的代码如下所示:

float MinFloatArray(float *a, int n)
{
    int i;
    float result = MAX_FLOAT;
    for (i=0; i<n; i++)
        if (a[i]<result)
            result = a[i];
    return result;
}

Code to do this looks like the following:

float MinFloatArray(float *a, int n)
{
    int i;
    float result = MAX_FLOAT;
    for (i=0; i<n; i++)
        if (a[i]<result)
            result = a[i];
    return result;
}
失眠症患者 2024-12-16 10:32:45

以下文章提供了比较浮点值的各种替代方法。
http://randomascii.wordpress.com/ 2012/02/25/comparing-floating-point-numbers-2012-edition/

通常,查找浮点数之间的差异并检查如果差异在精度限制内,有助于比较浮点数。
但正如也提到的,对此没有完美的答案和实现,因为相邻浮点数之间的差异随大小而变化。因此,如果您知道程序将使用的值的范围,那么您可以选择适当的实现进行比较。

Following article provides various alternatives on comparing float values..
http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

Usually, finding the difference between the floats and checking if the difference is within the precision limit, helps in comparing the floats.
But as also mentioned, there is no perfect answer and implementation for this because different between adjacent floats varies with the magnitude. Hence if you know the range of values which your program will use, then you can chose appropriate implementation for comparison.

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