Java math operations shorthand? - java

I've been searching but no luck. I want to know if there is a way to perform shorthand operations with the number 1. I know how to do it with addition and subtraction:
/*
* Addition: variableName++;
* Addition: variableName +=;
* Subtraction: variableName--;
* Subtraction: variableName -=;
* Multiplication: variableName*=
* Multiplication: variableName**; ?
* Division: variableName /=;
* Division: variableName// ? impossible
* Exponent: variableName ^=;
* Exponent: variableName^^; ?
* Modulo: variableName %=;
* Modulo: variableName%%; ?
*/
What about the others? Multiplication, Division, Exponents, Modulo.
Doing division seems almost impossible.
EDIT:
I should have been more specific.
I want to know if there is another version of ++ or - - for the other operators.

The other mathematical shorthand operators which Java provides are the += (a = a + number), *=, and so on.
The complete list of operators can be obtained from here.

Try these:
Int a = 5;
a += 1;
a -= 1;
a *= 2;
a /= 2;
Are these shorthand enough?

The other operators won't actually change the number.
e.g.
x = x * 1;
x is unchanged, why would you want a shorthand that doesn't change the value of x?
There are some languages that allow you to make your own shorthand for operators but this requires a bit of research

I think there is no increment or decrement operator like for multiplication and division.Instead we can use them like below
a *= b (a = a * b)
a /= b (a = a / b) and so on..
also if we want to multiply x by 1 or divide x by 1 then also there will be no effect on the final result..so what's the need

Related

Why does result of Math.pow() and * differ while calculating aswers in java ? and how do java calculate math in run-time memory?

I was trying to execute simple java program to calculate result with expression as: v^2 - u^2 / 2as
that is v*v - u*u / 2*a*s
code is in java 11
int v=16;
int u =5;
int a = 7;
int s = 9;
int res1 = v*v;
int res2 = u*u;
double FunRes1 = Math.pow(v, 2);
double FunRes2 = Math.pow(u, 2);
int part1 = res1 - res2;
int part2 = 2 *a*s;
int result = part1/part2; // = All 4
int AllResult = (v*v-u*u)/2*a*s; // == results
double doubleResult = FunRes1-FunRes2 / 2*a*s; // === have different
double doubleResult2 = (FunRes1-FunRes2) / 2*a*s; // ==== answers (see ss above)
the asnwers of all 4 variable ( result , AllResult , doubleResult1 , doubleResult2 ) are different .
Anyone explain why this happen ?
and What is the correct answers mathematically ?
This is because of operator precedence. If I write something like 2 * 8 / 8 - 6, without further context, it is ambiguous how it should be evaluated. This can lead to different results. For example (2 * 8) / (8 - 6) == 8 but ((2 * 8) / 8) - 6 == -4. To disambiguate, Java uses a list of precedence rules that are common throughout most languages. You can find the complete list here.
For your case the important part is that multiplication and division are applied before addition and subtraction.
Also of note is what happens in the case of operators having equal precedence, which also appears in your example.
When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.
Going back to our example of 2 * 8 / 8 - 6. We can see (from here) that java will evaluate multiplication and division before subtraction. * and / have the same precedence and are both binary operators so we evaluate them left to right. This leaves us with ((2 * 8) / 8) - 6 which is why java evaluates this as -4
System.out.println(2 * 8 / 8 - 6);
-4

What does n*=-1 mean in Java? Particularly n*=

so I'm supposed to read a code and I don't understand what this notation means. I understand that n+= bla, means n+1= bla. But i can't make out the meaning of n*= -1. Can someone tell me real quick. Also does anyone know WHY this would be used instead of a clearer notation? Whoever wrote the code couldn't have possibly saved more than a few letter when using this...
n += bla is equivalent to n = n + bla
n *= -1 is equivalent to n = n * -1
There's no functional reason to choose one notation over the other, it's just a stylistic preference. Java language (and others as well) is full of equivalent statements:
n++ is equivalent to n=n+1
n=m=1 is equivalent to n=1; m=1;
and many many others
It's the compound assignment operator version of the multiplication operator.
The code n *= x is equal to n = n * x
So in your case, n *= -1 would make n equal to itself multiplied by -1.
The only difference between using the compound assignment operator and the normal assignment operator with a multiplication expression is of stylistic preference.
You can read about compound assignment operators here: http://java.about.com/od/c/g/compoundassgnment.htm
n*= -1 does same thing as: n = n* -1
In Java or C++, there are things like:
+=, -=, *=, /=, &=, |=
They are all the same in a sense.
That is, do the operation first, then do the assignment.
It means n = n*(-1). Just changing the sign.
n += bla is the same as n = n + bla
n *= -1 is the same as n = n * -1
All of these operators act the same way : value math-operator assignment-operator other value is the same as value assignment-operator value math-operator other value

Operator in Java like // in python?

Is there a form of the // operator that is used in python that I can use in java, or some sort of workaround?
10 // 3 = 3
In python 3 // act as a floor division by default.
In python 2.2 and later 2.X version we can import it from the __future__
>>> from __future__ import division
>>> 10/3
3.3333333333333335
>>> 10//3
3
In Java:
When dividing floating-point variables or values, the fractional part of the answer is represented in the floating-point variable.
float f = 10.0f / 6.0f; // result is 1.6666
double d = 10.0 / 9.0; // result is 1.1111
But for floor in java:
(int)Math.floor(10/3);
One thing to notice is:
in python 3:
6 // -132 = -1
in java:
6 / -132 = 0
public static int python_like_divisor(int x, int y) {
final remainder = x % y;
if(remainder != 0) {
return (x - remainder) / y;
}
return x / y;
}
Some basic math knowledge is good ;)
With float-point (float, double etc.) values this method will not work properly.
Java's integer division will act in the same way as the // operator in Python. This means that something like this:
(int) 9/4 == 2 is True
The cast here is even unnecessary because both 9 and 4 are integers. If one was a float or a double this cast would be necessary as java would no longer execute this statement as integer division. To be more explicit you could do this
(int)Math.floor(9 / 4);
which divides the numbers first and then floors the results to the nearest integer.
You can use
java.lang.Math#floorDiv(int, int)

Using Math.pow() function

I'm trying to make a program in Java to calculate the formula for the Ricker wavelet:
But the results are not matching to the real ones.
This is what I'm using:
private static double rickerWavelet(double t, double f){
double p = (Math.pow(Math.PI, 2))*(Math.pow(f, 2))*(Math.pow(t, 2));
double lnRicker = 1 - (2 * p) * Math.exp(-p);
return lnRicker;
}
Am I using the Math functions wrongly?
To match the formula,
double lnRicker = 1 - (2 * p) * Math.exp(-p);
needs to be
double lnRicker = (1 - (2 * p)) * Math.exp(-p);
Since * has higher operator precedence than -, in your expression the multiplication of (2 * p) with Math.exp(-p) will be done first, which is not what you want.
I'd just like to add that Math.pow(x, 2) can be written more simply (and possibly more accurately and more efficiently) as x * x ... for any variable or constant x.
Look at your executing equation if you know about BODMAS method:
You should do: (1-(2*p))* Math.exp(-p);
I just changed your equation by inserting round brackets around 1-2*p..

Arithmetic operator confusion

Why I'm getting two different values while using the arithmetic operators for the same value of variables. I've just altered little bit my second program, which is resulted in giving me the different output. Could anyone please tell me why?
int number=113;
int rot=0;
rot=number%10;
rot*=100+number/10;
System.out.println(rot);//333
int number=113;
int rot=0;
rot=number%10;
rot=rot*100+number/10;
System.out.println(rot);//311
In the first part you compute
rot *= 100 + number/10
which is
rot = rot * (100 + number/10)
And in the second part:
rot = rot*100 + number/10
Note that multiplication and division goes before addition and substraction.
the problem is that *= has different (lower) precedence than * and +
rot *= 100 + number/10;
is equavalent to
rot = rot * (100 + number /10);
operator precdence can be found here
It seems like the problem is operator precedence.
What this means is that num * 10 + 13 is treated like (num * 10) + 13, i.e. the () are automatically added according to the rules of the language.
The difference then, in your example, is that the first one means the following:
rot*=100+number/10;
// Is the same as this:
rot = rot * (100 + (number / 10));
Whereas the second one means the following:
rot=rot*100+number/10;
// Is the same as this:
rot = (rot * 100) + (number / 10);
Since the parenthesis are in different places, these probably evaluate to different numbers.
in the second code because of the high precedence of *. rot*100 will be calculated and to that (number/10) will be added so its (300 + 11 = 311).

Categories

Resources