Java – Android – 1 divided by 2 = 0
I've been trying to use my new ioio for Android and need to find the frequency of the signal. So I convert the signal into binary and divide 1 by the time between 1. Although when I did this, I found that my output was 0. Then I decided to see what 1 / 2 gave me. To my surprise, it also gave 0! Anyone knows why?
Code: private floating frequency = 1 / 2;
Could this be using float.tostring (frequency)?
resolvent:
This is an example of integer division. Try:
private float frequency = 1.0/2.0;
Java will execute 1 / 2 and generate 0.5. However, since Java thinks this is an operation on integers (and 0.5 is not an integer), it will truncate the decimal and leave only the integer part 0. By telling java to use floating-point numbers (1.0 to 1), you tell it to keep the decimal part of the intermediate calculation