Android - 1除以2 = 0 [英] Android - 1 divided by 2 = 0

查看:441
本文介绍了Android - 1除以2 = 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用我的新IOIO for android,并且需要找到信号的频率。

所以我将信号转换为二进制,然后将1除以1之间的时间。虽然当我这样做时,我发现我的输出为0。然后我决定看看1/2给了我什么,令我惊讶的是它也给了0!任何人都知道为什么会这样吗?

I have been trying to use my new IOIO for android, and needed to find a frequency of a signal.
So I converted the signal to binary and then did 1 divided by the time between 1's. Although when I did this I found that I got 0 as my output. I then decided to see what 1 / 2 gave me, and to my surprise it also gave 0! Anyone have any idea why this is the case?

代码:
私人浮动频率= 1/2;

这可能是使用 Float.toString(频率)吗?

推荐答案

这是整数除法的一个例子。尝试:

This is an example of integer division. Try:

private float frequency = 1.0/2.0;

Java将执行1/2,产生0.5。但是,由于Java认为这是对整数的操作(并且0.5不是整数),它会截断小数并只留下整数部分0。通过告诉Java使用浮点数(1.0对1),你告诉它保留中间计算的小数部分。

Java will execute 1/2, which yields 0.5. However, since Java sees this as operations on integers (and 0.5 isn't an integer), it'll truncate the decimal and leave just the integer part, 0. By telling Java to work with floats (1.0 vs. 1), you tell it to keep the decimal part of the intermediate computation.

这篇关于Android - 1除以2 = 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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