编写了个小程序,最后结果不大对,是否float会四舍五入?
是一个例题,求一棵树的高度,通过一个高的人和一个矮的人与树站成一条直线,用相似三角形原理求出树的高度,2人的身高差/矮个子=(树高-矮个子)/树到高个子的距离。以下是程序代码。
程序代码:
#include<stdio.h> int main(void) { /*输入数据阶段*/ float tree_height,tom_height,marry_height,gap; float distance_tm=0,distance_tt=0; float feet1,feet2;/*利用相似三角形原理计算对象高度,定义树、tom、marry两人身高,身高差,两人的距离,tom到树距离*/ printf("Please make a guy to stand between trees and another guy," "they should have a different stature.\n\n");/*说明*/ tom_height=1.0; /*防止scanf的bug*/ printf("And then,please import a stature number of the height man: "); scanf("%f",&tom_height); /*得到tom的身高*/ printf("\nThen,import a stature number of the short man:"); scanf("%f",&marry_height);/*得到marry的身高*/ printf("how many feets between tree and the hight man:");/*得到树到tom步数*/ scanf("%f",&feet1); printf("how many feets between heightman and the shortman:");/*得到两人之间的步数*/ scanf("%f",&feet2); /*化步数和身高为米*/ feet1*=0.5; feet2*=0.5; tom_height/=60.0; marry_height/=60.0; /*开始计算*/ gap=tom_height-marry_height; tree_height=gap/feet2*feet1+marry_height; printf("There is %f meter is it.\n",tree_height); return 0; }
编译并没有错误,但如果输入高个子170 矮个子160 树到高个子步数20 两人之间步数10,得到结果是3.0000,实际计算应该是3.2左右,
如果把矮个子改158,就成了3.002这种,难道计算过程中进行了四舍五入吗?希望解惑