C语言没有四舍五入?

把一个double值,作为函数uint32_t返回值,结果直接把小数舍弃了,没有四舍五入,问问GEMINI:

Me:

actual_f_carrier is defined as a double, however when it's returned as the return value which is uint32_t, the decimals are dropped, how can I make the return value as a rounded value of actual_f_carrier, for example, when actual_f_carrier  = 868549987.79, the return value should be 868549988.

GEMINI:

The challenge lies in converting a floating-point value (actual_f_carrier) with decimals to a uint32_t integer while preserving the rounding behavior you described. Here are two approaches you can consider:

1. Rounding and Casting:

  • You can round actual_f_carrier to the nearest integer using a suitable rounding function and then cast the result to uint32_t. C doesn't have a built-in "round to nearest" function, but you can achieve this using a combination of floor and conditional logic:
uint32_t SetOperatingFrequency(double f_carrier) {
    // ... existing code ...

    double actual_f_carrier = f_xosc * freq / pow(2, 16);

    // Round to nearest integer
    int rounded_freq = floor(actual_f_carrier + 0.5);

    // Handle potential overflow (rounded_freq might be negative)
    if (rounded_freq < 0) {
        // Handle error: rounded value overflows uint32_t
        return UINT32_MAX; // Example: return max value to indicate error
    }

    return (uint32_t)rounded_freq;
}

2. Returning a Structure:

  • Instead of returning a uint32_t, you can define a custom structure that holds both the integer and fractional parts of the calculated frequency. This allows you to preserve the full precision of actual_f_carrier while still providing an integer representation for potential hardware limitations.
  • typedef struct {
        uint32_t integer_part;
        double fractional_part;
    } FrequencyInfo;
    
    FrequencyInfo SetOperatingFrequency(double f_carrier) {
        // ... existing code ...
    
        double actual_f_carrier = f_xosc * freq / pow(2, 16);
    
        FrequencyInfo result;
        result.integer_part = (uint32_t)actual_f_carrier; // Extract integer part
        result.fractional_part = actual_f_carrier - result.integer_part; // Calculate fractional part
    
        return result;
    }
    

  • Explanation:
    • We define a FrequencyInfo structure with two members: integer_part (a uint32_t) and fractional_part (a double).
    • The function now returns a FrequencyInfo structure instead of a uint32_t.
    • We extract the integer part by casting actual_f_carrier to uint32_t.
    • We calculate the fractional part by subtracting the integer part from actual_f_carrier.
  • The best approach depends on your specific needs. If you only need the rounded integer for hardware configuration and can handle potential overflow situations, option 1 might be sufficient. However, if you need to retain more precise information about the actual frequency for further calculations, returning a structure in option 2 provides greater flexibility.

  • 也就是说采用+0.5 再用floor函数的方法可以四舍五入,或者用结构体分别保存整数和小数部分。

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值