You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言避免浮点数舍入问题:直角三角形判定代码优化求助

如何修正C语言直角三角形判断函数的浮点数精度问题?

我编写了C语言的IsRightTriangle函数用于判定直角三角形,最初直接通过浮点数相等判断直角关系,之后修改为判断差值处于0.999到1之间,但仍存在小数精度问题,例如测试用例IsRightTriangle(edge1=15.26, edge2=8.00, edge3=13.00)执行失败。请问该如何修改代码以避免浮点数舍入问题?

现有代码

最初的代码

int IsRightTriangle(float x, float y, float z)
{
    int result;
    
    if((x*x) + (y*y) == (z*z) || (x*x) + (z*z) == (y*y) || (y*y) + (z*z) == (x*x)) {
        result = 1; 
        return result ;   
    }
    else { 
        result =0 ;
        return result;
    }
}

修改后的代码

#include <stdio.h>
int IsRightTriangle(float x,float y,float z )
{
    
    int result;
    
    if( ((x*x)+(y*y)-(z*z)>0.999 && (x*x)+(y*y)-(z*z)<1)  || ((x*x)+(z*z)-(y*y)>0.999 && (x*x)+(z*z)-(y*y)<1) || ((y*y)+(z*z)-(x*x)>0.999 &&(y*y)+(z*z)-(x*x)<1)) {
      
        result = 1; 
        return  result ;      
    } else { 
        result =0 ;
        return result;
    }
}

解决方案

你的核心问题是用了固定绝对阈值判断浮点数近似相等,这在浮点数运算中非常不可靠——浮点数的精度误差会随数值大小变化,固定范围只适用于特定量级的数值。正确的做法是使用相对误差判断,同时优化逻辑减少冗余。

优化思路

  1. 先确定最长边:直角三角形的斜边一定是最长边,只需验证另外两边的平方和是否近似等于最长边的平方,无需判断三种情况。
  2. 使用相对误差判断:计算两边平方和与最长边平方的差值绝对值,除以最长边的平方(相对误差),若小于设定阈值(如1e-3,可按需调整),则认为近似相等。
  3. 避免大数平方溢出:对于大数值,直接计算平方可能超出float范围,改用边长比值的平方和判断,避免溢出。

修改后的代码示例

#include <stdio.h>
#include <math.h>

int IsRightTriangle(float x, float y, float z) {
    // 找出最长边,确定斜边候选
    float max_edge = x;
    float side_a = y, side_b = z;
    
    if (y > max_edge) {
        max_edge = y;
        side_a = x;
        side_b = z;
    }
    if (z > max_edge) {
        max_edge = z;
        side_a = x;
        side_b = y;
    }

    // 处理边长为0的非法情况
    if (max_edge == 0.0f) {
        return 0;
    }

    // 计算比值平方和,避免大数平方溢出
    float ratio_a = side_a / max_edge;
    float ratio_b = side_b / max_edge;
    float sum_ratio_sq = ratio_a * ratio_a + ratio_b * ratio_b;

    // 相对误差阈值,可根据精度需求调整(如1e-3对应千分之一误差)
    return fabs(sum_ratio_sq - 1.0f) < 1e-3 ? 1 : 0;
}

代码说明

  • 最长边判断:减少了2/3的判断逻辑,同时确保只验证勾股定理的唯一可能情况。
  • 比值计算:将边长转换为与最长边的比值,平方后不会超出float的表示范围,避免溢出问题。
  • 相对误差阈值:1e-3的阈值允许千分之一的误差,足以覆盖浮点数存储和计算带来的精度损失,你的测试用例(15.26, 8.00, 13.00)会返回正确结果(计算得sum_ratio_sq≈0.99943,与1的差值约5.7e-4,小于1e-3)。

内容的提问来源于stack exchange,提问作者proran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 09:05:19