Rounding a double to the nearest of two doubles [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Is there a method to round a double the nearest of two doubles?
e.g.:
I want to round numbers to the nearest between 3.3 and 3.7.. so:
3.4 --> 3.3
3.472 --> 3.3
3.5 --> 3.7
3.573 --> 3.7
And so on...
Is there a method to do that?

As far as I know, there is no such method. However, writing your own shouldn't present a great difficulty:
static double nearest(double val, double left, double right) {
return Math.abs(val-left) <= Math.abs(val-right) ? left : right;
}

Related

When does Common subexpression elimination apply? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
for (Pair p : pairs) {
double f = foo(p)
...
}
foo() performs a simple mathematical calculation as follows:
double foo(Pair p) {
return Math.cos(p.x) + Math.sin(p.y);
}
If all the items in pairs are the same, can the Java compiler optimise this using Common subexpression elimination? Or is there another optimisation that occurs? I am asking since I have found a significant time reduction when more values in pairs are the same.

At last lines why he is using typecasting in double? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm new learner in java
This is just a part of the code just i need to know why he is using typecasting here and why int specially?
double anynumber =(int) 32/8.5;
This:.
(int) 32/8.5;
casts 32 to an integer and not the result of the division to integer,
because casting has higher precedence than division.
But it is totally unnecessary since 32 is already an integer.
So this:
double anynumber =(int) 32/8.5;
System.out.println(anynumber);
will print:
3.764705882352941
just like if the casting was not there.
But this:
double anynumber =(int) (32/8.5);
System.out.println(anynumber);
will print:
3.0
because it applies the casting to the result of the division by truncating the decimal digits to create an integer.
So to answer your question, if you found this line of code in a book or online,
it does not do anything different than a simple:
double anynumber = 32/8.5;

Java - Gamma algorithm [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am trying to code an algorithm to implement Gamma Correction but I am unable todo so with below code, because of a high power base. Wondering if anyone could fix below code to raise color values between 0,1 Thanks
The problem is in your math.
int / int will cause a truncation.
If red = 9 and you execute 9 / 255 the result is 0.
Try making all your literal values floating point
(for example 255.0 instead of 255).

python big integer multiplication doesn't calculate most significant digits [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a python program which should multiply 2 big integers.
The problem is I get a different result in java with BigInteger class with same input.
I have tried DecInt library for python but gives the same answer as using pure python.
Here are my variables:
d = 372049305848826709205673800090501485720867768816
r = 5452188953055713107393819158892374332916586527736541041226026450382
Result I get in python from d * r:
2028483115341019294875069650745272851135156323450218238187883716036516369477015140871224045070868977706272670887712
Result I get in java with BigInteger class:
9530687378863294988874153740700860249994095546182028483115341019294875069650745272851135156323450218238187883716036516369477015140871224045070868977706272670887712
Here is my java code:
BigInteger d = new BigInteger("372049305848826709205673800090501485720867768816");
BigInteger r = new BigInteger("5452188953055713107393819158892374332916586527736541041226026450382");
BigInteger tmp1 = d.multiply(r);
System.out.println(tmp1);
As you can see, there are some most significant digits that are missed in python's result.
Is there any solution for that?
Both Java and Python give the same answer:
2028483115341019294875069650745272851135156323450218238187883716036516369477015140871224045070868977706272670887712
The problem must be in your Java code then. Make sure you preform multiplication correctly and verify your input.
Also, check if there is anything that might print digits just before the result. The simplest way I can think of is:
System.out.println("the result is: " + int1.multiply(int2));
That will separate the output you are interested in from everything that is already printed.

How do I sum only the positive numbers in a stream? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have list values in stream while iterating I need to check if that value is negative or not if negative return zero or else retrun same value
Below is my code
{
Inventory .stream().
map.(inventory.value).sum;
}
You can just use Math.min for this. You'll have to map over them as integers, presumably:
Inventory.stream()
.mapToInt(it -> Math.min(0, it.value))
.sum()

Categories