What is causing NaN values in Java program?

  • Java
  • Thread starter squelchy451
  • Start date
  • Tags
    Java Program
In summary: This process will eventually produce a solution for a and b.In summary, the code does not work because the code does not have a loop. The code should be changed to while and the upper bound of the counter should be increased to say 35.
  • #1
squelchy451
25
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
  • #2
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".
 
  • #3
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?
 
  • #4
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
 
  • #5
It's very complicated and one of the steps to finding the GCD requires me to find a number that is an integer...
 
  • #6
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:
  • #7
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.
 

1. Why is my Java program not running at all?

There could be several reasons for this. One possibility is that there is an error in your code that is preventing the program from compiling or executing. Another possibility is that you do not have the necessary Java Runtime Environment (JRE) installed on your computer. You can check your code for errors and make sure you have the correct JRE version by consulting Java documentation or reaching out to a Java expert.

2. My Java program is running, but it is not producing the expected output. What could be causing this?

This could be due to a logical error in your code. Double check your program's logic and make sure you are using the correct syntax. It could also be an issue with your input data, so make sure you are providing the correct input for your program to produce the expected output. If you are still having trouble, consider debugging your code or seeking assistance from a Java expert.

3. I keep getting error messages when trying to run my Java program. How do I fix them?

Error messages are the computer's way of communicating that something is wrong with your code. Read the error message carefully and try to understand what it is telling you. Then, go back to your code and fix the issue that is causing the error. If you are unsure of how to fix the error, consult Java documentation or seek help from a Java expert.

4. My Java program was working before, but now it suddenly stopped working. What could have changed?

There are a few possible explanations for this. It could be that you made a change to your code that introduced an error, causing the program to stop working. Another possibility is that there was a change in your computer's environment, such as an update to the JRE or a change in your computer's settings, that is now causing the program to malfunction. Try to identify any recent changes and troubleshoot from there.

5. How can I prevent my Java program from crashing?

There are a few things you can do to prevent your Java program from crashing. First, make sure your code is well-written and free of errors. Next, consider using exception handling to catch and handle potential errors in your program. Finally, make sure you are using appropriate data structures and algorithms to optimize your program's performance and prevent crashes.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
765
  • Programming and Computer Science
Replies
2
Views
602
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
735
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
7
Views
2K
Back
Top