为什么分裂导致零而不是十进制? [英] 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.
为什么呢?结果
根据政府的Primer Plus应该为我用两个tempC和tempF彩车工作。
Why?
According to 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屋!
查看全文