This question already has answers here:
How do you test to see if a double is equal to NaN?
(7 answers)
Closed 8 years ago.
I am collecting some data from a database and adding them together to get some statistics, but since I backdate some of my data then the calculated sum will sometime come up as NaN (not a number) I want to create an if sentence that says if(not a number) then exclude this data from my table.
How do I test if the data (in this case double) is NaN?
There are static methods Float.isNaN(float) and Double.isNaN(double) that you can use.
double x = ... // whatever calculation you do
if (Double.isNaN(x)) {
...
}
You can test for NaN two ways. You can use the built in function
Double.isNaN(x)
or perform the check this does which is
if (x != x)
provided x is a double or a float
This would work for you.
if(number == Float.NaN)
Related
This question already has answers here:
Variable used in lambda expression should be final or effectively final
(9 answers)
How to sum a list of integers with java streams?
(12 answers)
Closed 4 years ago.
The following snippet does not compile. How to find the sum using forEach as shown below?
private int Sum(ArrayList<Integer> inputs) {
int sum = 0;
inputs.stream().forEach(x -> sum += x);
return sum;
}
This should do the trick:
private int Sum(ArrayList<Integer> inputs) {
return inputs.stream().mapToInt(Integer::intValue).sum();
}
EDIT :
The problem with using for-each is that it is a terminal operation, which means that it doesn't produce another intermediate stream for us to work on. The better approach would be to use mapToInt which produces an IntStream on which we can easily find the sum.
This answer is just to provide a bit more context as to why your code doesn't work and therefore allowing you to decipher the problem if it were to happen in the future.
It seems like you're a .NET user which makes it completely understandable for one to expect the code you've written to work. As in .NET the equivalent would be:
private int Sum(List<int> inputs) {
int sum = 0;
inputs.ForEach(x => sum += x);
return sum;
}
However, in java variables used in a lambda expression must be final or effectively final, for that reason the statement inputs.stream().forEach(x -> sum += x); will not compile.
Nevertheless, simply because one would expect the aforementioned code to work in C# doesn't necessarily mean it should work in Java as there are different rules.
There are solutions available to find the sum of a set of numbers using the forEach method but it's not the idiomatic approach and so should be avoided unless necessary.
The idiomatic approach is as #Nicholas K has shown.
On another note:
even in .NET the idiomatic approach would be return inputs.Sum();
as opposed to using the ForEach extension method to sum the elements of a given list.
whenever you seem to see yourself use inputs.stream().forEach(...); in java you should instead do inputs.forEach(...) as all lists have a forEach method.
method names in Java should be camelCase as opposed to PascalCasing as in C#.
This question already has answers here:
how to use NOT operator for integers in JAVA
(2 answers)
Closed 5 years ago.
I was working on a program in java where I was creating a truth table of a full subtractor using a 2 dimensional array. While doing so, I wanted to compute the borrow out with the following formula:
B(out) = !(X).Y + (!(X ^ Y))B(in)
So, I wrote it like:
table[i][4] = ((!(table[i][2]))&table[i][1])+((!(table[i][2]^table[i][1]))*table[i][0]);
(here: table[i][4] = cell to store borrow out;
table[i][2] = cell storing X;
table[i][1] = cell storing Y;
table[i][0] = cell storing borrow in B(in) )
Whereas XOR(^) and AND(&) and or(|) worked fine, I got the following error for NOT(!):
operator ! cannot be applied to int
How can I correct this? Is there any other way to write this formula as a JAVA code? Please help.
The bitwise complement operator that can be applied to integer values is the ~, not !
This question already has answers here:
What is float in Java?
(4 answers)
Closed 7 years ago.
case 1
float a=033.0 //shows compilation problem
case 2
double a=033.0 //works fine
Why case 1 is showing error but not case 2 or vice-versa?
By default java uses double type to store floating values. since you are storing double in float (down casting) java will throw an error. it can be resolved by two ways
float a=033.0f
float a= (float)033.0
case 1----float a=033.0 //shows compilation problem
case 2----double a=033.0 //works fine
In Java, decimal number is interpreted as a double ,so converting from double to float cannot be performed automatically ,so you need to give like this :
float a= 033.0f;
Its simply the understanding of Java Syntax.
You can read the Primitive data types of java.
You'll get it anywhere...
Link : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Direct to this link, do ctrl+F, & paste this "Floating-Point Literals".
You wont't waste time wandering...
For your own convenience now, you can prefer this.
float fractionNumber = 25.24F;
fractionNumber = 25.24f;
double biggerFractionNum = 56.65555D;
biggerFractionNum = 56.65555;
but generally its like this all over... later you'll get used to it.
float foo = 34.4F;
double doo = 34.4;
IMPLEMENT it right away ! ...experience it, understand it...& you'll never forget it. :)
This question already has answers here:
Assigning variables with dynamic names in Java
(7 answers)
Closed 8 years ago.
An example:
int x1 = 1;
int x2 = 2;
int x3 = 3;
...
int xn = n;
As you can see the name and value of the variables are generated - in this case by a counter. Is this possible?How?
I do not know how, but this is probably possible with some crazy Reflection hacking - do not try to do it, as it will produce code that is hard to maintain, hard to understand and vulnerable to hard-to-trace bugs.
Instead use Collections (do not use arrays if not absolutely needed), for example ArrayList.
Yes, it is valid Java code, but in this particular case you would just use the numbers :D
This question already has answers here:
Difference between double and Double in comparison
(5 answers)
Closed 8 years ago.
I am comparing two Doubles:
37.4238777160645
and
37.4238777160645
But Java does not consider them to be equal. I am comparing them in the following manner
if(object1.getLatitude()!=object2.getLatitude()){
fail("objects are not equal "+object1.getLatitude()+":"+object2.getLatitude());
}
resulting in the following fail:
junit.framework.AssertionFailedError: objects are not equal 37.4238777160645:37.4238777160645
I don't understand why - Please advise.
The issue has already been pointed out. But if you are using junit, it would be simpler to use the appropriate method:
assertEquals(object1.getLatitude(), object2.getLatitude());
or
assertEquals(object1.getLatitude(), object2.getLatitude(), 0.001d);
instead of using fail. That would also solve your issue.
Objects should be compared with .equals and not ==. By == you're comparing the references, which are not the same since you return a different object each time.
Use Double#equals to compare the values.
You should use java.lang.Double.compare() :
Double.compare(object1.getLatitude(), object2.getLatitude())
so you wil have :
if(Double.compare(object1.getLatitude(), object2.getLatitude()) != 0){
fail("objects are not equal "+object1.getLatitude()+":"+object2.getLatitude());
}
When applied to objects, the == operator returns true only when both operands are the same object.
Your method returns Double objects, so each call will produce a new object, and comparing them using == will always be false.
Use .equals(), which compares values of the Doubles:
if (!object1.getLatitude().equals(object2.getLatitude()))
Alternatively, change you methods to return double instead of Double, and your current code will work.
Floating point comparison in may programming languages should always take the form of
if(a-b op c)
where a and b are the numbers being compared and c being the threshold and op is either > or
<
.
This is because floating point representation in binary to not map directly to what is printed.