为什么除法结果为零而不是小数? [英] Why does division result in zero instead of a decimal?
本文介绍了为什么除法结果为零而不是小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
自学 C 并发现当我为温度转换做等式时,除非我将分数更改为小数,否则它将不起作用.即,
Teaching myself C and finding that when I do an equation for a temp conversion it won't work unless I change the fraction to a decimal. ie,
tempC=(.555*(tempF-32))
会起作用,但 tempC=((5/9)*(tempF-32))
不会工作.
tempC=(.555*(tempF-32))
will work but tempC=((5/9)*(tempF-32))
won't work.
为什么?
根据C Primer Plus"一书它应该可以工作,因为我对 tempC 和 tempF 都使用浮点数.
Why?
According to the book "C Primer Plus" it should work as I'm using floats for both tempC and tempF.
推荐答案
看起来你在第二种情况下有整数除法:
It looks like you have integer division in the second case:
tempC=((5/9)*(tempF-32))
5/9
将被截断为零.
要解决这个问题,您需要将其中一个设为浮点类型:
To fix that, you need to make one of them a floating-point type:
tempC=((5./9.)*(tempF-32))
这篇关于为什么除法结果为零而不是小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文