浮点数

包含头文件

#include <stdio.h>
#include <stdlib.h>
#include <math.h> // 包含数学函数库

浮点数类型大小

打印不同浮点数类型的大小。

    // 各种浮点数类型的占用空间大小
    // 4 byte
    printf("float类型的大小:%d\n", sizeof(float));
    // 8 byte
    printf("double类型的大小:%d\n", sizeof(double));
    // 16 byte
    printf("long double类型的大小:%d\n", sizeof(long double));

浮点数占位符

定义不同类型的浮点数并打印。

    // 各种类型浮点数占位符
    float f1 = 1.2;
    double f2 = 2.2;
    long double f3 = 3.2L;
    printf("单精度浮点数:%f\n", f1);
    printf("双精度浮点数:%lf\n", f2);
    printf("控制输出两位小数双精度浮点数:%.2lf\n", f2);
    printf("长双精度浮点数:%Lf\n", f3);
    printf("科学计数法输出浮点数:%e\n", f2);

浮点数常用函数

使用标准库函数进行字符串转换、绝对值计算、指数运算、四舍五入、上取整、下取整和求余数。

    // 浮点数常用函数
    char *str = "-11.05";
    // 将字符串转为double
    double res = atof(str);
    printf("字符串%s转为浮点数的结果:%lf\n", str, res);
    // 求浮点数的绝对值
    printf("%lf的绝对值:%lf\n", res, fabs(res));
    res = fabs(res);
    // 指数运算
    res = pow(2.0, 1.5);
    printf("2的1.5次方等于:%lf\n", res);
    // 浮点数的四舍五入
    res = round(4.5);
    printf("4.5四舍五入的结果:%lf\n", res);
    // 上取整
    printf("4.5上取整的结果:%lf\n", ceil(4.5));
    res = ceil(4.5);
    // 下取整
    res = floor(4.5);
    printf("4.5下取整的结果:%lf\n", res);
    // 求x/y的双精度余数
    double x = 2.2, y = 3.3;
    res = fmod(x, y);
    printf("%lf除%lf的双精度余数:%lf\n", x, y, res);
    // modf()函数将浮点数分割为整数部分和小数部分,整数部分保存在 ip指针变量中,返回小数
    double val = 4.5, ip = 0;
    res = modf(val, &ip);
    printf("浮点数%lf的整数部分是%lf,小数部分是:%lf\n", val, ip, res);

函数原型

  • double atof(const char *str);:将字符串 str 转换为 double 类型。
  • double fabs(double x);:返回 x 的绝对值。
  • double pow(double base, double exponent);:返回 baseexponent 次幂。
  • double round(double x);:将 x 四舍五入到最接近的整数。
  • double ceil(double x);:将 x 向上取整。
  • double floor(double x);:将 x 向下取整。
  • double fmod(double x, double y);:返回 x 除以 y 的余数。
  • double modf(double value, double *iptr);:将 value 分解为整数部分和小数部分,整数部分存储在 iptr 指向的位置,返回小数部分。

完整实例代码

#include <stdio.h>
#include <stdlib.h>
#include <math.h> // 包含数学函数库

int main() {
    // 各种浮点数类型的占用空间大小
    // 4 byte
    printf("float类型的大小:%d\n", sizeof(float));
    // 8 byte
    printf("double类型的大小:%d\n", sizeof(double));
    // 16 byte
    printf("long double类型的大小:%d\n", sizeof(long double));

    // 各种类型浮点数占位符
    float f1 = 1.2;
    double f2 = 2.2;
    long double f3 = 3.2L;
    printf("单精度浮点数:%f\n", f1);
    printf("双精度浮点数:%lf\n", f2);
    printf("控制输出两位小数双精度浮点数:%.2lf\n", f2);
    printf("长双精度浮点数:%Lf\n", f3);
    printf("科学计数法输出浮点数:%e\n", f2);

    // 浮点数常用函数
    char *str = "-11.05";
    // 将字符串转为double
    double res = atof(str);
    printf("字符串%s转为浮点数的结果:%lf\n", str, res);
    // 求浮点数的绝对值
    printf("%lf的绝对值:%lf\n", res, fabs(res));
    res = fabs(res);
    // 指数运算
    res = pow(2.0, 1.5);
    printf("2的1.5次方等于:%lf\n", res);
    // 浮点数的四舍五入
    res = round(4.5);
    printf("4.5四舍五入的结果:%lf\n", res);
    // 上取整
    printf("4.5上取整的结果:%lf\n", ceil(4.5));
    res = ceil(4.5);
    // 下取整
    res = floor(4.5);
    printf("4.5下取整的结果:%lf\n", res);
    // 求x/y的双精度余数
    double x = 2.2, y = 3.3;
    res = fmod(x, y);
    printf("%lf除%lf的双精度余数:%lf\n", x, y, res);
    // modf()函数将浮点数分割为整数部分和小数部分,整数部分保存在 ip指针变量中,返回小数
    double val = 4.5, ip = 0;
    res = modf(val, &ip);
    printf("浮点数%lf的整数部分是%lf,小数部分是:%lf\n", val, ip, res);

    return 0;
}