Java What is causing NaN values in Java program?

  • Thread starter Thread starter squelchy451
  • Start date Start date
  • Tags Tags
    Java Program
AI Thread Summary
The discussion centers around a Java program intended to check if a number is an integer, but it encounters issues with NaN values and a lack of a loop. The original code uses an if-statement instead of a while-loop, which is necessary for repeated checks. The calculation of the square root of a negative number leads to NaN results for variables b and c. Participants suggest using the floor() function to determine if a double is an integer by checking if the difference between the value and its floor is zero. The conversation also touches on the purpose of the program, with suggestions to clarify the goal, particularly regarding the greatest common divisor (GCD) and the use of Euclid's algorithm. For handling larger numbers, the java.math.BigInteger class is recommended. Overall, the thread emphasizes the need for clearer objectives and proper coding techniques to achieve the intended functionality.
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, woudl 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

Back
Top