unexpected output, confusion between float and integer [duplicate] - java

This question already has answers here:
Dividing two integers in Java gives me 0 or 100?
(5 answers)
Closed 2 years ago.
I have two statements in java:
System.out.println(2 * (5 / 2 + 5 / 2 ));
This generate the output 8 but again in next line:
System.out.println(2 * 5 / 2 + 2 * 5 / 2 );
This generate the output 10.
now my confusion is why it generates different output
please someone describe it. thanks

Note, that division between ints generates an int (rounded down).
2 * (5 / 2 + 5 / 2) => 2 * (2 + 2) => 8
2 * 5 / 2 + 2 * 5 / 2 => 10 / 2 + 10 / 2 => 10
To get the exact value, you would have to use floats:
2 * (5.0 / 2 + 5.0 / 2) == 10

Related

How to resolve this 3x3 matrix with equations requirements

I was in an interview today and I got the following exercice :
write a program to find all the 3x3 matrix X for each element x varying from 0 to 100
A B C
D E F
G H I
that meet the following requirements
A + B - C = 4
+ - -
D - E * F = 4
/ * -
G + H + I = 4
= = =
4 4 4
write a program in Java.
It is not really clear what your question is, but this seems quite straightforward to simply bruteforce by trying all choices in a sensible order.
For example, this Python code:
for G in range(1,4+1):
for H in range(4+1-G):
I = 4 - H - G
for A in range(0,4+1):
D = G*(4-A)
if not 0<=D<=100:
continue
for E in range(100+1):
for F in range(100+1):
if D-E*F==4:
for B in range(100+1):
C=A+B-4
if 0<=C<=100:
if B-E*H==4:
if C-F-I==4:
print A,B,C
print D,E,F
print G,H,I
print A+B-C,D-E*F,G+H+I,A+D/G,B-E*H,C-F-I
finds the following 4 solutions:
0 10 6
4 6 0
1 1 2
4 4 4 4 4 4
2 7 5
4 3 0
2 1 1
4 4 4 4 4 4
1 8 5
6 2 1
2 2 0
4 4 4 4 4 4
2 6 4
4 1 0
2 2 0
4 4 4 4 4 4

Using division in recursion [duplicate]

This question already has answers here:
Understanding how recursive functions work
(18 answers)
Closed 7 years ago.
I have the following program
public static int doSomething(int num){
if(Math.abs(num) > 9){
return (1 + doSomething( num / 10));
}
else{
return 1;
}
}
public static void main(String[] args){
System.out.println(doSomething(333));
}
This is how I understand it. If the number is 333.
333 / 10 gives me 33. Since 33 > 9 it runs the recursive loop again giving me 3.
After 3 it enters the else condition.
I don't understand why it prints 3 as answer.
I am new to java so still trying to understand the basics.
I don't believe the question is a duplicate. My question is much simpler being a beginner to java. Also the question I believe is using javascript not java.
Look, what will return doSomething() if Math.abs(num) < 9? 1? yes you are right. Ok lets start.
doSomething(333) => 1 + doSomething(33) // 33 not less than 9
=> 1 + (1 + doSomething(3)) // now 3 is less than 9
=> 1 + (1 + {1})
=> 3
The recursion is not on 1 + 33, but on 33 only, so it's:
doSomething(333)
= 1 + doSomething(33)
= 1 + 1 + doSomething(3)
= 1 + 1 + 1
= 3

Regular expression for arithmetic expression

I,m trying to write a regex to check if the given string is like
a + b, 2 + a + b, 3 + 6 * 9 + 6 * 5 + a * b, etc...
Only + and * operators.
I tried
if (str.matches("(\\d|\\w \\+|\\*){1,} \\d|\\w"))
Unfortunately it only handles cases like 3 * 7 ... (numeric * numeric).
Waiting for your answers, thanks for reading me.
Put * and + inside a character class.
str.matches("\\w(?:\\s[+*]\\s\\w)+");
DEMO
This will handle cases of simple and chained calculations
[0-9A-Za-a]*( ){0,}([+-/*]( ){0,}[0-9A-Za-a]*( ){0,})*
This would match, for example
1+2
1 + 2
1 + a * 14 / 9
(You can change the operators you want by updating [+-/*])

How is sine implemented in Java? [duplicate]

This question already has answers here:
Why does Math.sin() delegate to StrictMath.sin()?
(4 answers)
Closed 8 years ago.
Quick question about the sine function in Java. Does anyone know how the value is computed? I found this question about sin in Java, but that's asking why the sin function isn't wrapped in native code. I'm asking something entirely different. I want to know how the function was implemented. (Since it's wrapped in native code, I can't see it.)
Did they simply implement it from the Taylor series expansion:
sin(x) = x - (x^3)/3! + (x^5)/5! - O(x^7)
I can't look at the code for the Math.sine() function, since it gets wrapped up in native code.
The implementation can be found here(*).
The sin function is approximated by a 13-degree polynomial. That is, a function on the shape
c12x12 + c11x11 + ... + c1x1 + c0x0
on the interval [0,π/4]
The description of the algorithm looks as follows:
33 * Algorithm
34 * 1. Since sin(-x) = -sin(x), we need only to consider positive x.
35 * 2. if x < 2^-27 (hx<0x3e400000 0), return x with inexact if x!=0.
36 * 3. sin(x) is approximated by a polynomial of degree 13 on
37 * [0,pi/4]
38 * 3 13
39 * sin(x) ~ x + S1*x + ... + S6*x
40 * where
41 *
42 * |sin(x) 2 4 6 8 10 12 | -58
43 * |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x +S6*x )| <= 2
44 * | x |
45 *
46 * 4. sin(x+y) = sin(x) + sin'(x')*y
47 * ~ sin(x) + (1-x*x/2)*y
48 * For better accuracy, let
49 * 3 2 2 2 2
50 * r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6))))
51 * then 3 2
52 * sin(x) = x + (S1*x + (x *(r-y/2)+y))
53 */
(*) Disclamer: Talking about OpenJDK here

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.

Categories