Division with expected result between 1 and 0 always gives 0 [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-02 13:15:22

You're dividing integer with integer. The result will be an integer, the remainder is omitted:

1 / 2 = 0     The remainder (0.5) is omitted.

Only after the division is done, your result is passed into your float d variable.

EDIT: Right, I forgot to suggest a solution: Make sure at least one of your operands is a float.

1.0 / 2 = 0.5    

1 / 2.0 = 0.5
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!