Java Assert Double is NaN [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am trying to assert that my double is NaN.
Here is a code snippet:
private Double calculateIt(String input){...}
assertEquals(Double.NaN, calculateIt("input text"));
The code does not compile, Double.NaN is defined as primitive
public static final double NaN = 0.0d / 0.0;
To make the assertion work I wrap NaN with a Double object.
assertEquals(new Double(Double.NaN), calculateIt("input text"));
Is there a shorter way to do this?

You could use:
boolean isNan = Double.isNaN(calculateIt("input text"));
assertTrue(isNan);
Double.NaN values cannot be compared with == (Double.NaN == Double.NaN will return false), because NaN is considered as a special one.
More info:
IEEE floating point

You could try the following:
assertTrue(Double.compare(Double.NaN, calculateIt("input text")));
Hope this helped you.

assertEquals(Double.NaN, calculateIt(...), 0.0) with assertEquals(double, double, double)
or
assertThat(calculateIt(...), isNan()) with Hamcrest.
better way of doing this
What best shows the intent? Which can you skim read and easily see what the method is testing?

Shorter is Double.valueOf("NaN") or with a static import valueOf("NaN"), but it's basically the same as you already have.

Related

Comparing values with zero [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 9 years ago.
Improve this question
How to compare floating point values with zero, I may get integers like 0,1,2 or floating point numbers 0.0 ,41.2 etc. How to compare these values against 0
I tried this way
String x="<some value either 0 or 1.2"
If(Integer.parseInt(x)==0)
System.out.println("parsed");
with input 0 it is working, with 1.2 throwing format exception.
Regards,
Raj
If your values can be decimal numbers, use this for parsing the string:
Double.parseDouble(x)
The comparison remains the same, though:
if (Double.parseDouble(x) == 0)

How do I fix these incompatible data types? [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 9 years ago.
Improve this question
I'm adding two random numbers (generated by my program). The operation variable is a String that the user inputs what type of math problem they wish to have(+,-, or *). This is only one section of my full program.
public static double getCorrectAnswer(int operand1, int operand2, String operation){
double correctResponse;
correctResponse =(operand1 + (operation) + operand2);
return correctResponse;
}
Use if:
if (operand.equals("+")) {
...
}
else if (operand.equals("*")) {
...
}
...
You can also use a switch.
Note that you should return an int, since adding and multiplying integers will return an integer.
This may be too much for you but you could have a look at a formal language parser such as ANTLR

Using float and long data types in java [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Why do most programmers avoid using the float and long data types in their video tutorials?
Is it only to avoid the "0000f" and "0000L" notation?
I avoid float has it has poor precision. I would rather use double (or long with fixed precision or if I have to BigDecimal).
I suspect long is not often used as int is usually enough and many Java APIs only accept int values. e.g. array sizes and Collection/Map size() must be int.
And why should they use them? Perhaps the one reason is to avoid writing a letter indicating that the float/long type is used.
However if you don't need any special precision, why use double instead of float or long instead of int?

What is the difference between Float and float in java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Can someone explain to me the difference between Float and float in java? Manythanks.
Float is an object; float is a primitive. Same relationship as Integer and int, Double and double, Long and long.
float can be converted to Float by autoboxing, e.g.
float f=1.0f;
Float floatObject = f;
or explicitly
Float floatObject = new Float(f);
Initially primitives were retained alongside the object versions for speed. Autoboxing/unboxing was added with java 5 to facilitate conversion.
Float is a class which wraps the primitive float. In newer versions of Java, a feature called autoboxing makes it hard to tell that they are different but generally speaking, use float when you using the number to do calculations and Float when you need to store it in Object collections.

Java error "possible loss of precision" [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 9 years ago.
Improve this question
I am in a computer science class (11th) for hw i have to do some code following a picture
--------------------Configuration: --------------------
C:\Users\I_SLAY_NOOBS\Desktop\Variables.java:26: error: possible loss of precision
float floatOne = 58.5678;
^
required: float
found: double
1 error
Process completed.
You assign a double which is more precise than a float to a variable declared as float. Float values end with an f in Java. Plain floating point numbers are automatically considered to be doubles...
Either do:
float f = 58.5678f;
or:
double d = 58.5678;
If you truly want a float (single precision IEEE754), you can use:
float floatOne = 58.5678f;
Me, I'd simply go with the double type everywhere since it provides more precision than float:
double doubleOne = 58.5678;
The default for floating point constants in Java (and C and C++, among others) is double precision and you're trying to shoehorn that into a single-precision variable, hence the message. Appending f to a floating point constant tells the compiler that you want it to be single precision.
Unless you have vast arrays of them (and limited space to store them), doubles are generally preferred.

Categories

Resources