#include <stdio.h>
#include <stdint.h>
double custom_pow(double base, int exponent)
{
double result = 1.0;
if(exponent >= 0) {
for(int i = 0; i < exponent; i++) {
result *= base;
}
} else {
for(int i = 0; i < -exponent; i++) {
result /= base;
}
}
return result;
}
/**
* @brief 向下取整
*
* @param bit 精确到第几位
* @note
* custom_floor(1.234, 2) = 1.23
* custom_floor(-1.234, 2) = -1.24
*/
double custom_floor(double num, uint8_t bit) {
int times = custom_pow(10, bit);
int int_part = (int)(num * times);
// 检查要求精确位数的后一位是否为0,不为0负数要减一
if (num < 0 && (int)(num*times*10)/10.0 != int_part) {
return (double)(int_part - 1) / times;
} else {
return (double)int_part / times;
}
}
/**
* @brief 向上取整函数
*
* @param bit 精确到第几位
*/
double custom_ceil(double num, uint8_t bit)
{
int times = custom_pow(10, bit);
int int_part = (int)(num * times);
// 检查要求精确位数的后一位是否为0,不为0正数要加一
if (num > 0 && (int)(num*times*10)/10.0 != int_part) {
return (double)(int_part + 1) / times;
} else {
return (double)int_part / times;
}
}
/**
* @brief 四舍五入
*
* @param bit 精确到第几位
*/
double custom_round(double num, uint8_t bit)
{
return num < 0 ? custom_ceil(num - 0.5/custom_pow(10, bit), bit) : custom_floor(num + 0.5/custom_pow(10, bit), bit);
}
void test(double num, uint8_t bit)
{
double result_floor = custom_floor(num, bit);
printf("自定义向下取整的结果为: %lf\n", result_floor);
double result_ceil = custom_ceil(num, bit);
printf("自定义向上取整的结果为: %lf\n", result_ceil);
double result_round = custom_round(num, bit);
printf("自定义四舍五入的结果为: %lf\n", result_round);
printf("%f -------------------------------------------------\n", num);
}
/**
* @brief 向下取整,精确到整数
*
* @param
* @param
*/
double custom_floor0(double num)
{
int int_part = (int)num;
if (num < 0 && num != int_part) {
return int_part - 1;
} else {
return int_part;
}
}
/**
* @brief 向上取整,精确到整数
*
* @param
* @param
*/
double custom_ceil0(double num)
{
int int_part = (int)num;
if (num > 0 && num != int_part) {
return int_part + 1;
} else {
return int_part;
}
}
/**
* @brief 四舍五入,精确到整数
*
* @param
* @param
*/
double custom_round0(double num)
{
return num < 0 ? custom_ceil0(num - 0.5) : custom_floor0(num + 0.5);
}
void test0(double num)
{
double result_floor = custom_floor0(num);
printf("自定义向下取整的结果为: %lf\n", result_floor);
double result_ceil = custom_ceil0(num);
printf("自定义向上取整的结果为: %lf\n", result_ceil);
double result_round = custom_round0(num);
printf("自定义四舍五入的结果为: %lf\n", result_round);
printf("%f -------------------------------------------------\n", num);
}
int main(void)
{
uint8_t bit = 0;
double num = 6.0;
test(num, bit);
num = -6.0;
test(num, bit);
num = 6.0001;
test0(num);
test(num, 3);
num = -6.0001;
test0(num);
test(num, 3);
num = 6.111;
test(num, bit);
num = -6.111;
test(num, bit);
num = 6.888;
test(num, bit);
num = -6.888;
test(num, bit);
num = 6.555;
test(num, bit);
num = -6.555;
test(num, bit);
num = 6.405;
test(num, bit);
num = -6.405;
test(num, bit);
num = 6.415;
test(num, bit);
num = -6.415;
test(num, bit);
num = 6.455;
test(num, bit);
num = -6.455;
test(num, bit);
num = 6.465;
test(num, bit);
num = -6.465;
test(num, bit);
return 0;
}
执行结果
自定义向下取整的结果为: 6.000000
自定义向上取整的结果为: 6.000000
自定义四舍五入的结果为: 6.000000
6.000000 -------------------------------------------------
自定义向下取整的结果为: -6.000000
自定义向上取整的结果为: -6.000000
自定义四舍五入的结果为: -6.000000
-6.000000 -------------------------------------------------
自定义向下取整的结果为: 6.000000
自定义向上取整的结果为: 7.000000
自定义四舍五入的结果为: 6.000000
6.000100 -------------------------------------------------
自定义向下取整的结果为: 6.000000
自定义向上取整的结果为: 6.000000
自定义四舍五入的结果为: 6.000000
6.000100 -------------------------------------------------
自定义向下取整的结果为: -7.000000
自定义向上取整的结果为: -6.000000
自定义四舍五入的结果为: -6.000000
-6.000100 -------------------------------------------------
自定义向下取整的结果为: -6.000000
自定义向上取整的结果为: -6.000000
自定义四舍五入的结果为: -6.000000
-6.000100 -------------------------------------------------
自定义向下取整的结果为: 6.000000
自定义向上取整的结果为: 7.000000
自定义四舍五入的结果为: 6.000000
6.111000 -------------------------------------------------
自定义向下取整的结果为: -7.000000
自定义向上取整的结果为: -6.000000
自定义四舍五入的结果为: -6.000000
-6.111000 -------------------------------------------------
自定义向下取整的结果为: 6.000000
自定义向上取整的结果为: 7.000000
自定义四舍五入的结果为: 7.000000
6.888000 -------------------------------------------------
自定义向下取整的结果为: -7.000000
自定义向上取整的结果为: -6.000000
自定义四舍五入的结果为: -7.000000
-6.888000 -------------------------------------------------
自定义向下取整的结果为: 6.000000
自定义向上取整的结果为: 7.000000
自定义四舍五入的结果为: 7.000000
6.555000 -------------------------------------------------
自定义向下取整的结果为: -7.000000
自定义向上取整的结果为: -6.000000
自定义四舍五入的结果为: -7.000000
-6.555000 -------------------------------------------------
自定义向下取整的结果为: 6.000000
自定义向上取整的结果为: 7.000000
自定义四舍五入的结果为: 6.000000
6.405000 -------------------------------------------------
自定义向下取整的结果为: -7.000000
自定义向上取整的结果为: -6.000000
自定义四舍五入的结果为: -6.000000
-6.405000 -------------------------------------------------
自定义向下取整的结果为: 6.000000
自定义向上取整的结果为: 7.000000
自定义四舍五入的结果为: 6.000000
6.415000 -------------------------------------------------
自定义向下取整的结果为: -7.000000
自定义向上取整的结果为: -6.000000
自定义四舍五入的结果为: -6.000000
-6.415000 -------------------------------------------------
自定义向下取整的结果为: 6.000000
自定义向上取整的结果为: 7.000000
自定义四舍五入的结果为: 6.000000
6.455000 -------------------------------------------------
自定义向下取整的结果为: -7.000000
自定义向上取整的结果为: -6.000000
自定义四舍五入的结果为: -6.000000
-6.455000 -------------------------------------------------
自定义向下取整的结果为: 6.000000
自定义向上取整的结果为: 7.000000
自定义四舍五入的结果为: 6.000000
6.465000 -------------------------------------------------
自定义向下取整的结果为: -7.000000
自定义向上取整的结果为: -6.000000
自定义四舍五入的结果为: -6.000000
-6.465000 -------------------------------------------------
标签:四舍五入,6.000000,自定义,int,C++,num,取整,bit From: https://www.cnblogs.com/skullboyer/p/18061812