[英]Rounding function in C
所以我試圖寫一個代碼,可以讓我將任何數字四舍五入到小數點后三位。 我的四舍五入代碼如下:
for (rowIndex = 0; rowIndex < MAX_ROWS; rowIndex++)
{
for (columnIndex = 0; columnIndex < MAX_COLUMNS; columnIndex++)
{
printf("%.3f ", ceil(rawData[rowIndex][columnIndex] * 1000.0) / 1000.0);
}
}
但是昨天我的老師告訴我們使用具有如下結構的代碼:
float roundValue(float value, int decimalPlaces)
{
// Place rounding code here
return value;
}
我不太確定如何以這種格式編寫代碼! 我是編碼的初學者,所以這可能太傻了。
更新:所以我只是閱讀下面的所有評論,並嘗試編寫代碼,但仍然有問題。 我的代碼是:
double roundValue(double value, int decimalPlaces)
{
value = roundf( value * pow(10, decimalPlaces)) / pow(10, decimalPlaces);
return value;
}
int main(void)
{
int rowIndex = 0;
int columnIndex = 0;
double rawData[MAX_ROWS][MAX_COLUMNS]; // 2-dimensional array to store our
raw data
double value = rawData[MAX_ROWS][MAX_COLUMNS];
int decimalPlaces = 3;
// Print out the roundup data array
printf(" --- ROUNDED DATA ---\n");
for (rowIndex = 0; rowIndex < MAX_ROWS; rowIndex++)
{
for (columnIndex = 0; columnIndex < MAX_COLUMNS; columnIndex++)
{
printf("%.3f ", roundValue(value, 3));
}
printf("\n");
}
return 0;
}
它給我所有數字僅0。
基於此答案 ,您可以使用math.h
的roundf
函數:
#include <stdio.h>
#include <math.h>
/* function that rounds a float to the specified number of decimals */
float roundValue(float value, int decimalPlaces)
{
value = roundf(value * pow(10, decimalPlaces)) / pow(10, decimalPlaces);
return value;
}
/*to see the results: */
int main()
{
float value = 12.34567;
printf("%f", roundValue(value, 3));
return 0;
}
編譯/運行:
$ gcc -lm main.c
$ ./a.out
12.346000
他只是告訴您在可以在main()
函數中調用的函數中編寫代碼。
因此,不必每次都需要舍入值時都重寫代碼,而是可以使用函數,而是給它提供要為其計算舍入值的數字,它將為您提供結果,因此您的代碼不會重復
從本質上講,這是不可能完成的。 問題是不能精確地以浮點格式表示0.1或0.001。 因此,您只能四舍五入為最接近的表示形式,四舍五入= floor(x * 1000 + 0.5)/1000.0。 最好使用雙精度的全部精度,然后在最后一刻進行舍入顯示。
printf("%.3g", x);
將為您實現這一目標。 與strtod結合使用時,它也是另一種舍入技術。
..將任何數字四舍五入到小數點后三位。
我的老師告訴我們使用代碼...例如float roundValue(float value, int decimalPlaces)
如果不追求更高的精度, 就很難以所有value
的最佳答案來達到OP的目標。
通常將步長將a或b)的浮動指針值四舍五入到最接近的可表示0.001(或10 -n )。
1)乘以10 n
2)將a)向上或b)四舍五入
3)除以10 n
float roundValue(float value, int decimalPlaces) {
// form the power of 10
assert(decimalPlaces >= 0 && decimalPlaces <= 9);
int power_of_10 = 1;
while (decimalPlaces-- > 0) power_of_10 *= 10;
double fpower_of_10 = power_of_10; // or just use `pow(10, decimalPlaces);
以10的冪進行縮放會導致不精確。 在舍入步驟中,此輕微誤差會放大。 一個簡單的解決方法是使用高精度數學。 幸運的是,編碼目標始於float
值,而double
通常具有更高的精度。
乘以10的冪可以導致溢出,但是當value
float
且乘積是double
范圍更廣時,這不太可能發生。
double y = value * fpower_of_10;
// round
double rounded_y = ceil(y); // round up
// or
double rounded_y = round(y); // round to nearest
商很少提供0.001(或10的冪)的精確倍數,而提供的浮點值接近 0.001的倍數。
y = rounded_y / fpower_of_10;
return y;
}
用法如下。 回想一下,除非您的浮點類型使用FLT_RADIX == 10
(最近很少見,通常為2),否則結果僅接近所需的“數字到n
個小數位”。 如果做得好,結果將是最接近的float/double
。
printf("%f\n", roundValue(123.456789, 3));
printf("%.10f\n", roundValue(123.456789, 3)); // to see more
更多:如果無法獲得或使用更高的精度,避免溢出問題的一種簡便方法是認識到大的C浮點值沒有小數部分,也不需要四舍五入。
float roundValue(float value, int decimalPlaces) {
float int_ptr;
float frac_part = modff(value, &int_ptr);
if (frac_part == 0) return value;
double pow10 = pow(10, decimalPlaces);
return round(value * pow10)/pow10; // or ceil()
}
還有其他一些細微的問題,這里沒有提到。 double四舍五入 , NaN , 四舍五入模式 , round()
與rint()
, nearbyint()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.