我是靠谱客的博主 自由魔镜,最近开发中收集的这篇文章主要介绍@今天的c语言你学废了吗?!!----浮点数认识(20210206,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

@今天又学废了呢----float 与 double 篇(C语言)

2021-02-06

就在小five还是小小five的时候,他从王Tao老师口中得知:

float 单精度
double 双精度

但这件事,不简单!

区别 … 字节数 … 有效字位数 … 数值的取值范围 … 处理速度
float … …4 … … … 8 … … … … … ??? … … … … 较快
double … 8 … … …16 … … … … … ??? … … … …较慢

输入输出的时候:
… … … …float … … … … double
scanf… … %f … … … … … %lf
printf … %e %f … … … … %e %f

运算的时候形如:
float y ;
y = 3.0 / 2.0;
y = 3 / 1.0 / 2;
才能得到准确的值!

!!形如3 / 2 / 1.0 并不能得到准确的结果
: 按照优先级,先计算3 / 2 ,但此时是整型的运算,得到的值是 1
: 在将 1 / 1.0 得到的是1.00000 并非1.50000!

例题

A/B Problem
Write a program which reads two integers a and b, and calculates the following values:
a ÷ b: d (in integer)
remainder of a ÷ b: r (in integer)
a ÷ b: f (in real number)

Input
Two integers a and b are given in a line.
Output
Print d, r and f separated by a space in a line. For f, the output should not contain an absolute error greater than 10-5.
Constraints
1 ≤ a, b ≤ 109
Sample Input 1
3 2
Sample Output 1
1 1 1.50000

!!!精度要求:不包含大于10-5的绝对错误 ???

#include <stdio.h>
int main(){
	int a,b;
	int d,r;
	double f;
	scanf("%d %d",&a,&b);
	d = a/b;
	r = a%b;
	f = a/1.0/b;
	//f2 = a / b / 1.0;
	printf("%d %d %lfn",d,r,f);
	return 0;	
}

感觉还有些高深的东西没探索到。下次一定!

最后

以上就是自由魔镜为你收集整理的@今天的c语言你学废了吗?!!----浮点数认识(20210206的全部内容,希望文章能够帮你解决@今天的c语言你学废了吗?!!----浮点数认识(20210206所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部