为什么除法的结果是零而不是小数? [英] Why does division result in zero instead of a decimal?

查看:16
本文介绍了为什么除法的结果是零而不是小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)) will work but tempC=((5/9)*(tempF-32)) won't work.

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))

The 5 / 9 will get truncated to zero.

To fix that, you need to make one of them a floating-point type:

tempC=((5./9.)*(tempF-32))

这篇关于为什么除法的结果是零而不是小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆