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?
Code:
private float frequency = 1/2;
Could this be todo with using Float.toString(frequency)?
This is an example of integer division. Try:
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.