What is causing NaN values in Java program?

  • Context: Java 
  • Thread starter Thread starter squelchy451
  • Start date Start date
  • Tags Tags
    Java Program
Click For Summary

Discussion Overview

The discussion revolves around a Java program intended to check if a number is an integer, with a focus on understanding the occurrence of NaN values in the output. Participants explore the implications of the code structure, particularly regarding the calculation of square roots and the intended functionality of the program.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant points out that the code calculates the square root of a negative number, leading to NaN values for variables b and c.
  • Another participant suggests changing the first if-statement to a while-statement to create a loop, indicating that the original code does not contain a loop.
  • A participant questions the purpose of the program, noting the discrepancy between the stated goal of checking for integers and the class name gcd, which implies finding the greatest common divisor.
  • There is a suggestion to use the floor() function to determine if a double value is an integer by checking if the difference between the value and its floor is zero.
  • One participant proposes starting with specific values for a and b to find positive integer solutions to the equation a² = 3b² + 42, indicating a potential approach to the problem.
  • Another participant mentions the java.math.BigInteger class as a tool for calculating GCD, suggesting it may be useful for handling large numbers.

Areas of Agreement / Disagreement

Participants express differing views on the purpose of the program and how to address the NaN issue. There is no consensus on the intended functionality or the best approach to resolve the problems presented in the code.

Contextual Notes

The discussion highlights limitations in the original code, such as the lack of a loop and the potential misunderstanding of the program's goal. There are unresolved questions about the types of numbers being processed and the specific requirements for finding GCD.

squelchy451
Messages
23
Reaction score
0
Hi

I wanted to make a program that would check if a number if it was an integer or not in Java. here's the source code:

import static java.lang.Math.*;

public class gcd {
public static void main(String [] args) {

double a = 1;
double b =1;
double c = 1;
int counter = 1;

if ( counter < 10) {

a=a+1;
b =10 * Math.sqrt((a * a - 42)/3);

c = b%10;

System.out.println("a: " + a);
System.out.println("b: " + b);
System.out.println("c: " + c);
System.out.println("counter: " + counter);

if(c == 0) {
System.out.println(b);
counter = counter + 1;
}
}
}
}

For the output I get
a: 2.0
b: NaN
c: NaN
counter:1

So the loop doesn't work and i get weird NaN values for b and c...i run it by changing the directory on command prompt and typing in "java gcd"

I used to use JCreator in my Java class...any recommended free programs for java?

Thanks!
 
Technology news on Phys.org
squelchy451 said:
So the loop doesn't work
There is no loop in your code. You probably want to change the first if-statement to a while-statement.
squelchy451 said:
and i get weird NaN values for b and c
Your code calculates the square root of a negative number. I have no idea what it is you think the code is supposed to do, but it surely is not what the name gcd (greatest common divisor) implies. If you want an algorithm for calculating gcd then search for "Euclid's algorithm".
 
The idea for this program is derived from Euclid's algorithm...i just need to see if a number is an integer or not

If I fix the loop to while and increase the upper bound of the counter to say 35, would this program help me see if b contains decimals or not?
 
It's very difficult to ascertain what you're trying to do. You said in the OP
squelchy451 said:
I wanted to make a program that would check if a number if it was an integer or not in Java.
yet the name of your class is gcd, which suggests you want to find the greatest common divisor.

What are you trying to do? If you want to check that a double is actually an integer, you can use the floor() function to find the largest integer that is less than or equal to your number. IOW, val - floor(val) should evaluate to 0.0 if val is an integer.
thread title is
 
It's very complicated and one of the steps to finding the GCD requires me to find a number that is an integer...
 
squelchy451 said:
It's very complicated and one of the steps to finding the GCD requires me to find a number that is an integer...

Euclid's algorithm is very easy to implement unless you have some very special needs. If you need to process arbitrary big numbers you can always fall back on using the java.math.BigInteger class which can calculate GCD for you [1].

I still have no idea what you mean with "find a number that is an integer". What type of numbers do you have and why do you need to find GCD in the first place?[1] http://java.sun.com/javase/6/docs/api/java/math/BigInteger.html#gcd(java.math.BigInteger)
 
Last edited:
It dawns on me that you perhaps are trying to find (positive) integer solutions to the equation a2 = 3b2+42. If so, I will suggest that you start with a suitable value of a (like 7) and b (like 0) and then test in a loop if a and b are a solution by inserting their value into the left hand side (lhs) and right hand side (rhs). If the lhs equals the rhs you have a solution which you can print, after which you can stop the loop or increase either a or b by one and loop for more solutions. If lhs is less than the rhs, increase a by one; if lhs is greater than the rhs, increase b by one.
 

Similar threads

Replies
8
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
13K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K