Java/Pawn Maths formula returns 0 - java

could someone explain me what am I doing wrong here, and how can I make this formula to be working? right now it returns 0 in Java and Pawn. But it works in PHP, which isnt making much sense for me.
int test = (65 / 65 / (1 + 25) * 10000);

In Java, integer division will truncate the results. For example, 5/2 will truncate 2.5 to 2.
To ensure you're using float with numeric constants, add a .0 on the end, as in:
int test = (65.0 / 65.0 / (1.0 + 25.0) * 10000.0);

Java operations for multiplication and division is left to right, so your expression is really:
(((65 / 65) / 26) * 1000) // Clarifying parenthesis
((1 / 26) * 1000)
(0 * 1000) // integer division!
0
To avoid this, you just need to ensure the operations are casted to a double.
The simple way would be just changing the first value to a double, either by:
int test = (65D / 65 / (1 + 25) * 10000);
Or
int test = (65.0 / 65 / (1 + 25) * 10000);
However, if you are refactoring your code later, you might want to change more than just the first value.

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

Can anyone explain the output process in java

int p=15, q=12, s;
s=p/q*++p%(++p%--q);
System.out.print(s);
I am trying it according to the precedence of operators and associativity but I think it should give me the output 2 but it is giving me 4 as output.
Precedence
p/q*++p%(++p%--q)
is computed as
((p / q) * (++p)) % ((++p) % (--q))
using the operator precendece rules.
Note that ++x/--x increment/decrement the variable by one and then return the result after the operation. Since they change the variable itself, the effect obviously carries over to other usages of the variable.
Evaluation
It is then evaluated left to right, the math is
((15 / 12) * (16)) % ((17) % (11))
which results in 4:
((15 / 12) * (16)) % ((17) % (11))
= (1 * 16) % 6
= 16 % 6
= 4

Rounding to tens in Java formula doesn't make sense

I found this formula on SO but I don't get how it works:
(n + 5) / 10 * 10
This rounds a number to the multiple of ten but when I calculate it it just divides and multiplies back to n+5. How does this actually work?
Example (in Java):
(24 + 5) / 10 * 10 = 20
While it should actually be 29 as far as I know.
You're doing these operations on int. That's why, every operation result is also an int. What happens is:
(24 + 5) = 29
29 / 10 = 2 (because the fraction part is always dropped in ints)
2 * 10 = 20
This rounding method works fine only for float and double. You can try to cast the int to double.

Percentage of a Number

So I'm a little frustrated with how many times I've tried this.
I'm using # / 100 = % * Balance = Fine here, yet for some reason always results to 0. Note that this uses an external API.
double fine = (getConfig().getBoolean("fineispercentage") == true ? getConfig().getInt("fine") / 100 * count * econ.getBalance(event.getPlayer().getName()) : econ.getBalance(event.getPlayer().getName()) - (econ.getBalance(event.getPlayer().getName()) - count * getConfig().getInt("fine")));
When true, always results to 0. When false, it goes the way it's supposed to, which is subtract an amount depending on the count.
Try this (not tested):
double fine = (getConfig().getBoolean("fineispercentage") == true ? getConfig().getInt("fine") / 100.0 * count * econ.getBalance(event.getPlayer().getName()) : econ.getBalance(event.getPlayer().getName()) - (econ.getBalance(event.getPlayer().getName()) - count * getConfig().getInt("fine")));
The trick is to divide by 100.0 (double), not 100 (int)
Assuming that getConfig().getInt() returns an int, this java division:
getConfig().getInt("fine) / 100
Will do an integer division, and produce an int result, which will truncate any decimal part, so any value of "fine" 0-99 will result in a zero. The fix is this:
getConfig().getInt("fine) / 100d
Adding the "d" to 100 makes the 100 a double value, which will perform a double division, keeping the decimal part.
Assuming getInt does actually return an integer, it looks like you're doing integer division, giving you 0.
Divide getConfig().getInt("fine") by 100d instead of 100:
double fine = (getConfig().getBoolean("fineispercentage") == true ? getConfig().getInt("fine") / 100.0 * count * econ.getBalance(event.getPlayer().getName()) : econ.getBalance(event.getPlayer().getName()) - (econ.getBalance(event.getPlayer().getName()) - count * getConfig().getInt("fine")));

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