Can you get a number from doing maths with a NaN? [closed] - java

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 7 years ago.
Improve this question
Say I had double x = 0.0/0.0;.
Is there anything I could do with x in order to get an actual number?
Dividing by itself/0/infinity? Subtracting by something? Anything like that.

You can go through each JLS chapter for each of +, -, *, / and % and you'll read
If either operand is NaN, the result is NaN.
Using the value NaN with any of those would always produce NaN.
Is there anything I could do with x in order to get an actual number?
I'm assuming you meant with the operators above.

I think, we should prevent any number divided by zero instead.
EDIT
avg = 0;
count = 0;
for (number in scores) {
avg += number;
count++;
}
if (count != 0){
return avg / count;
} else {
return 0.0;
}

Related

Clarification on math expression using prefix increment operator and bracket precedence [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 4 years ago.
Improve this question
Given this code:
int p,k=8;
p=k*(++k-8);
System.out.println(p);
when ++k is evaluated k=9 and then that becomes k*(9-8) giving 9*1
int p,k=8;
p=(++k-8)*k;
System.out.println(p);
But this gives 9 as output
You have a multiplication with
left side: k
right side: (++k-8)
As you correctly stated, braces have precedence. However, your program still runs "from left to right". So first the left side is evaluated, which is k = 8. Afterwards the right side is evaluated, which is (++k-8) = 1.
Now we have determined both sides and can multiply them together: 8*1 = 8.
this is the class file your code compiled:
int k = 8;
byte var10000 = k;
int k = k + 1;
int p = var10000 * (k - 8);
System.out.println(p);

stuck with this while loop java [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 4 years ago.
Improve this question
I have been working with while loops however I cant seem to understand how the remainder works inside this code.
int a = 10;
while( a <= 1000 && a % 100 != 0){
System.out.println("a = " + a);
a = a + 10;
}
a & 100 != 0
Performs bitwise and , then compares the result to 0. It will be false even in the first iteration, since 10 & 100 = 0

What's the safe way to calculate 0.01 of an Integer in Java? [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 5 years ago.
Improve this question
For instance, I have integers, i.e. 450. I want to get 1/100 of the number N. In this case it should be 4.5, which will rounded to 5.
int i = 450;
int round = Math.round(i/450)?
When the i varies, is this safe always?
i/450 will do an integer division before the result gets passed into Math.round and you won't get what you expected. Even then you got ~1/450 of the value, not 0.01 of it. You need
(int)Math.round(i/100.0);
However you can do rounding with just integer math like either of these
int round = (i + 50)/100; // i + 99 for ceiling
int round = (i - 1)/100 + 1;
int round = i/100 + (i % 100 < 50 ? 0 : 1);
For more information read Rounding integer division (instead of truncating)
See also
How to round up integer division and have int result in Java?
How to Round Up The Result Of Integer Division
Fast ceiling of an integer division in C / C++
These are about ceiling function but you can get the idea
For it to work no matter numerator or denominator, then
BigDecimal.valueOf(450).divide(BigDecimal.valueOf(100),RoundingMode.HALF_UP);
Another way could be like
BigDecimal.valueOf(450,2).setScale(0, RoundingMode.HALF_UP);
You can convert back to int using .intValue()
That would only make sense of course when you are dealing with exact decimal math most of the time until the very end, like would be the case for financial applications.
I "think" this might be what you are looking for:
float i = 450;
int round = Math.round(i/100);
System.out.println(round);//prints 5
Division with int always rounds down so when i is an int, the expression i/100 returns 4 when i is between 400 and 499.
Alternately, you could cast to float:
int i = 450;
int round = Math.round((float)i/100);
System.out.println(round);//prints 5

The devide of NaN plus number does not result the NaN value [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 5 years ago.
Improve this question
I'm using Double.isNaN() to detect NaN value. `
Double nan = Double.NaN;
Double num = 1.5;
Double num2 = 4.5;
Double result = (nan+num)/num2;
System.out.println(result);// the result is NaN
if(Double.isNaN(result))
System.out.println("not NaN");//true
Is there any other way to detect NaN value?
Your condition doesn't correspond to your output - you check if the result is in fact a NaN, but then print that it isn't. Either check that it isn't:
if (!Double.isNaN(result))
System.out.println("not NaN"); // This won't be reached in your case
Or print that it is:
if (Double.isNaN(result))
System.out.println("NaN");

How to add two or more numbers and display the sum (Netbeans) [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 8 years ago.
Improve this question
So i'm a beginner and I am trying to figure out how to add more than 2 numbers and display it through the output of netbeans. Its really confusing for me since i'm still new to programming.
First, you need to define variable.
what is variable?
A variable provides us with named storage that our programs can
manipulate.
There are different type variable in Java like int, double, char, String, and so on.
What do I mean by type variable?
determines the size and layout of the variable's memory
he range of values that can be stored within that memory
the set of operations that can be applied to the variable
How to define Variable in Java
data type variable [ = value][, variable [= value] ...] ;
For example , you can define two variable as int type as follows
int firstNumber = 1;
int secondNumber = 2;
Note if you like you can define more than two variables by following up the rules.
you can do Arithmetic Operators on your variables
Operator Description
+ Additive operator (also used for String concatenation)
- Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator
Here you want to add so you need to use + operator.
read this which is my source : http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html
At the end, you should define a variable that carries the summation of two variables like
int sum = number1 + number2;
and print the value on your console by using System.out.print/ln
System.out.print("the summation of number one and number two is " + sum);
Source:
http://www.homeandlearn.co.uk/java/java_int_variables.html
http://www.tutorialspoint.com/java/java_variable_types.htm
int a = 1;
int b = 2;
int c = 3;
int d = a + b + c;
System.out.println(d);
If you want to add up the numbers in an array or collection:
int[] numbers = { 1, 2, 3 };
int sum = 0;
for (int number : numbers) {
sum += number;
}
System.out.println(sum);

Categories