2022-02-12

WARNING: This article may be obsolete
This post was published in 2022-02-12. Obviously, expired content is less useful to users if it has already pasted its expiration date.
This article is categorized as "Garbage" . It should NEVER be appeared in your search engine's results.

继续复习C语言

继续复习基本数据类型:double和float

* 之前复习的基本数据类型见:🔗 [2022-01-23 - Truxton's blog] https://truxton2blog.com/2022-01-23/

C语言程序设计 现代方法 P210

注:下面这张表需要遵循IEEE标准

C语言程序设计 现代方法 P210

Float和double的默认类型与强制转换

Any numeric constant in a C program that contains a decimal point is treated as a double by default. You can also use e or E to add a base-10 exponent (see the table for some examples of this.) If you want to insist that a constant value is a float for some reason, you can append F on the end, as in 1.0F.

https://www.cs.yale.edu/homes/aspnes/pinewiki/C(2f)FloatingPoint.html
#include<stdio.h>

int main(int argc, char const *argv[]) {

    printf("sizeof(float) = %lu\n", sizeof(float));
    printf("sizeof(double) = %lu\n", sizeof(double));
    printf("sizeof(1.1) = %lu\n", sizeof(1.1));
    printf("sizeof(1.1F) = %lu\n", sizeof(1.1F));
    printf("sizeof((float)1.1) = %lu\n", sizeof((float) 1.1));
    
    return 0;
}

得到结果:

sizeof(float) = 4
sizeof(double) = 8
sizeof(1.1) = 8
sizeof(1.1F) = 4
sizeof((float)1.1) = 4

OCR辅助查找工具

float:单精度浮点数。 double:双精度浮点数。 1ong double:扩展精度浮点数。 当精度要求不严格时(例如,计算带一位小数的温度),float 类型 是很适合的类型。double 提供更高的精度,对绝大多数程序来说都 够用了。long double 支持极高精度的要求,很少会用到。 C标准没有说明float 、double 和1ong double 类型提供的精度 到底是多少,因为不同的计算机可以用不同方法存储浮点数。大多数 现代计算机都遵循IEEE 754标准(即IEC 60559) 的规范,所以这里也 用它作为一个示例。类型 最小正值 最大值 精度float 1.175 49 X 10-38 3.402 821038 16个数字double 2.225 0710- 808 1.797 691030S 15个数字


 Last Modified in 2022-02-28 

Leave a Comment Anonymous comment is allowed / 允许匿名评论