Android Math Calculation Not Giving Expected Value - java

I'm trying to run a calculation but I'm not getting the correct result and struggling to understand why.
Calculation
float Signal = ((20 - 0) / (20 - 4)) * (F5 - 4) + 0;
F5 = 12 and is declared as a "Float" type
When run through a calculator, you end up with the following:
((20 - 0) / (20 - 4)) * (12 - 4) + 0
((20) / (16)) * (12 - 4) + 0
(1.25) * (8) + 0
Result = 10
However, when this is run through the Android code, I get the result of 8.
Why is this, I'd like to understand what's going on

The first term ((20 - 0) / (20 - 4)) is calculated using integer arithmetic, giving a value of 1. This makes the final result 8 regardless of the type of F5. If you want it to happen in floating point, use floating point constants
float Signal = ((20f - 0f) / (20f - 4f)) * (F5 - 4f) + 0f;
Technically you don't need all the constants to be floats due to numeric promotion, but it is much clearer to those reading your code that you intended for everything to be a float.

Related

Why are the outcomes of the two calculations different? [duplicate]

This question already has answers here:
The accuracy of a double in general programming and Java
(2 answers)
Division of integers in Java [duplicate]
(7 answers)
Closed 6 years ago.
I have the following two calculation using Math.round(...):
double x = 0.57145732;
x = x * 100;
x = Math.round(x * 10);
x = x / 10;
If I now print the value of x it will show me: 57.1.
double x = 0.57145732;
x = (Math.round((x * 100) * 10)) / 10;
// x = (Math.round(x * 1000)) / 10; //Also gives me 57.0.
If I now print the value of x it will show me: 57.0.
Why is there this difference in the outcome?
The Math.round() method returns an integer (of type long - as pointed out by Ole V.V). It's usually thought to return a float or double which gives rise to confusions as these.
In the second calculation,
Math.round((x * 100) * 10)
returns 571. Now, this value and 10 both are integers (571 is long, 10 is int). So when the calculation takes the form
x = 571 / 10
where x is double, 571/10 returns 57 instead of 57.1 since it is int. Then, 57 is converted to double and it becomes 57.0
If you do
x = (double)Math.round((x * 100) * 10) / 10.0;
its value becomes 57.1.
Edit: There are two versions of the Math.round() function. The one you used accepts a double (since x is double) and returns long. In your case, implicit type casting spares you the trouble of considering the precise little details.
The reason of the difference is that in the second formula you're making a division of two integer. in order to have the same result you have to add a cast to double:
double x = 0.57145732;
x = (double)(Math.round((x * 100) * 10)) / 10;
The difference is between
x = Math.round(571.45732) / 10;
and
x = Math.round(571.45732);
x = x / 10;
Since round(double) returns a long, in the first case you divide a long by an int, giving the long 57. Converting back to double leads to 57.0. The second case is equivalent to
x = ((double)Math.round(571.45732)) / 10;
where a double is divided by an int, resulting in 57.1.
This because Math.round() returns an int. If you do this step-by-step (as in the first example), you assign the result of Math.round() to a float value. The following calculation uses then a float division.
In the second example, you let the JVM decide which types to use (and it uses an integer division as the intermediate step). This is why the precision gets lost.

Math.random() in Java, how is this possible?

Currently in class, learning about math in Java
I got speechless when my teacher showed me this formula?
if ((int)(Math.random() * 15) == 1) {
How can something * 15 be 1?
Suppose Math.random returns 0.08907633950002491
Now according to your formula
0.08907633950002491*15 it returns 1.3361450925003737
after int type cast it will be 1
(int)(Math.random() * 15) == 1 returns true
This is because of the cast to integer
(int) Math.random()
You see Math.random() gives a random number between 0.0 and 1.0
when you cast to an integer you are effectively saying - " Throw out all the decimal places"
So if you had 0.5 doing (int) Math.random() would give you 0.
Now looking at your code, you'd have something like this
Assuming Math.random() gives 0.3, you'd have
((int)(0.3 * 15) == 1) which will be ((int)(4.5) == 1) now remember what casting to int does? throw out the decimal places. So we are left with if (4 == 1).
In this case the condition will be false so the code in the if won't run but there is a chance that you would have a number like 1.xx after multiplying.
The java.lang.Math.random() returns a double value with a positive
sign, greater than or equal to 0.0 and less than 1.0.
x * 15 = 1
x = 1/15
So X is a double value from interval 0-1. Everything is correct.

How do math equations work in Java?

When I do something like this
int test = 5 + 3 * (4 - 1) / 2;
I get 9. I suspected this was because int rounds down. However, when I do this
float test = 5 + 3 * (4 - 1) / 2;
I also get 9. However, when I do this
float test1 = 5;
float test2 = 4.5;
float test = test1 + test2;
Test finally outputs 9.5. Could someone explain the logic behind this? Why don't I get 9.5 in the second example? Thanks.
In your second example, although you are assigning the result to a variable of type float, the calculation itself is still performed exactly the same way as the first example. Java does not look at the destination variable type to determine how to calculate the right hand side. In particular, the subexpression 3 * (4 - 1) / 2 results in 4.
To fix this, you can use floating point literals instead of all integers:
float test = 5 + 3 * (4 - 1) / 2.0f;
Using 2.0f triggers floating point calculations for the arithmetic expression.
Although you represent the result of 5 + 3 * (4 - 1) / 2 in a float, the actual evaluation is done with the precision of an integer, meaning that the division of 9 / 2 returns 4 rather than the 4.5 you would receive if they were evaluated as floats.
Expressions have their own type. So start with:
5 + 3 * (4 - 1) / 2
Each value has its own type. This type happens to be int, so this is the same as:
((int)5) + ((int)3) * (((int)4) - ((int)1)) / ((int)2)
Making it clearer that we're dealing with ints. Only after this is evaluated as 9 does it get assigned to a float.
The short answer is that integer types operate on modular arithmetic, with modulus 1, and discard the remainder.
Since you cast test as an integer, modular arithmetic is employed with modulus 1 (e.g. 9.5 mod 1),
int test = 5 + 3 * (4 - 1) / 2;
With a 32-or-64 bit float this would give 9.5; however, because you have cast test as an int, the remainder is discarded, and the value referenced by test is 9.

Operator precedence in Java

In one example from http://leepoint.net/notes-java/data/expressions/precedence.html
The following expression
1 + 2 - 3 * 4 / 5
Is evaluated as
1 + 2 - 3 * 4 / 5
= (1 + 2) - ((3 * 4) / 5)
= 3 - (12/5)
= 3 - 2 The result of the integer division, 12/5, is 2 .
= 1
Then i saw another example from http://www.roseindia.net/java/master-java/operator-precedence.shtml
The following expression
4 + 5 * 6 / 3
is evaluated as
4 + (5 * (6 / 3))
I am slightly confused as to how it is decided which will be evaluated first when * and / are involved. In the examples above, both seem to be difference.
The first example is evaluating 3*5/5 as ((3*4)/5)
Whereas the second example is evaluating 5*6/3 as (5*(6/3))
I know that * and / have precedence over + and - but what about when the expression includes both * and /. And also why are the above two examples showing different approaches? Is one of them wrong?
Edit
public class ZiggyTest {
public static void main(String[] args) {
System.out.println(4 + (5 * (6 / 3)));
System.out.println(4 + ((5 * 6) / 3));
System.out.println(1 + 2 - (3 * (4 / 5)));
System.out.println(1 + 2 - ((3 * 4) / 5));
}
}
The above program produces the output
14
14
3
1
Why are the last two outputs not the same if the first produced the same output.
I am slightly confused as to how it is decided which will be evaluated first when * and / are involved
That's why we have specifications :)
Section 15.7 is the section of the Java Language Specification which deals with evaluation order, and section 15.17 states:
The operators *, /, and % are called the multiplicative operators. They have the same precedence and are syntactically left-associative (they group left-to-right).
So whenever there is A op1 B op2 C and both op1 and op2 are *, / or % it's equivalent to
(A op1 B) op2 C
Or to put it another way - the second linked article is plain wrong in their example. Here's an example to prove it:
int x = Integer.MAX_VALUE / 2;
System.out.println(x * 3 / 3);
System.out.println((x * 3) / 3);
System.out.println(x * (3 / 3));
Output:
-357913942
-357913942
1073741823
That shows the multiplication happening first (leading to integer overflow) rather than the division (which would end up with a multiplication of 1).
Are you sure?
4 + (5 * 6) / 3
4 + 30 / 3
4 + 10
14
4 + 5 * (6 / 3)
4 + 5 * 2
4 + 10
14
They produce the same output because adding the parentheses don't happen to change the result. For your other equation, the parentheses actually do change the result. By removing the parentheses in the equations I solved, the correct path to the result is the first one.
The second one is wrong. See Jon Skeet's answer. Multiplicative operators evaluate left to right. The grouping for:
4 + 5 * 6 / 3
should be
4 + ((5 * 6) / 3).
In this case, though, the wrong grouping
4 + (5 * (6 / 3))
yields the same answer.

3 / 2 = 1.0? really? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java Integer Division, How do you produce a double?
double wang = 3 / 2;
Log.v("TEST", "Wang: " + Double.toString(wang));
Logcat output...
07-04 09:01:03.908: VERBOSE/TEST(28432): Wang: 1.0
I'm sure there's an obvious answer to this and probably I'm just tired from coding all night but this has me stumped.
In many languages, Java being one of them, the way you write a number in an expression decides what type it gets. In Java, a few of the common number types behave like this1:
// In these cases the specs are obviously redundant, since all values will be
// cast correctly anyway, but it was the easiest way to show how to get to the
// different data types :P
int i = 1;
long l = 1L;
float f = 1.0f; // I believe the f and d for float and double are optional, but
double d = 1.0d; // I wouldn't bet on what the default is if they're omitted...
Thus, when you declare 3 / 2, you're really saying (the integer 3) / (the integer 2). Java performs the division, and finds the result to be 1 (i.e. the integer 1...) since that's the result of dividing 3 and 2 as integers. Finally, the integer 1 is cast to the double 1.0d which is stored in your variable.
To work around this, you should (as many others have suggested) instead calculate the quotient of
(the double 3) / (the double 2)
or, in Java syntax,
double wang = 3.0 / 2.0;
1 Source: The Java Tutorial from Oracle
Integer division of 3 by 2 is equal to 1 with residue of 1. Casting to double gives 1.0
3 and 2 are integer constants and therefore 3 / 2 is an integer division which results in 1 which is then cast into a double. You want 3.0 / 2.0
Try: double wang = 3.0 / 2.0;
That's the expected behaviour. "3" and "2" are both int values, and when you perform 3 / 2 the result will also be an int value which gets rounded down to 1. if you cast both to double before you perform the division then you'll get the result that you expect:
double wang = (double)3 / (double)2;

Categories