Jump to content

Java: NaN

Go to solution Solved by Blade of Grass,
3 minutes ago, mikestechs said:

Yes it is. But how can i do--- if result is NaN that the program will say System.out.println("Impossible to do");

System.out.println((Double.isNaN(1.0) ? "Impossible to do" : 1.0));

Not too sure exactly what you want, but Double.isNaN is a static method which returns a boolean value, so use it as a parameter in an if statement, ternary, etc. 

Hello guys, I'm starting with Java programming and i just want to ask...

If i will have this:

-----------------

a = -1,5;

b = -2,2;

 

write = Math.pow (a,b);

System.out.println(write);

 

// I'll get this 

 

Nan

-----------------

 

And i want do something like if the "program" say NaN it will write something like "impossible" or something like that.

 

I'm new in this so i tried something with if ( write == "NaN")

but that was stupid idea becouse (write) will be always number. 

 

Any ideas :) ? 

Link to comment
https://linustechtips.com/topic/540653-java-nan/
Share on other sites

Link to post
Share on other sites

3 minutes ago, mikestechs said:

Yes it is. But how can i do--- if result is NaN that the program will say System.out.println("Impossible to do");

System.out.println((Double.isNaN(1.0) ? "Impossible to do" : 1.0));

Not too sure exactly what you want, but Double.isNaN is a static method which returns a boolean value, so use it as a parameter in an if statement, ternary, etc. 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
https://linustechtips.com/topic/540653-java-nan/#findComment-7160155
Share on other sites

Link to post
Share on other sites

1 hour ago, Stuff_ said:

Is it throwing an exception on NaN? I don't recall Java, but if it's throwing an exception, you should catch it and signal the error then.

Java doesn't throw an exception on NaN. 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
https://linustechtips.com/topic/540653-java-nan/#findComment-7161553
Share on other sites

Link to post
Share on other sites

Except, this isn't impossible.  Not mathematically at least.  In fact it's not even a complex/imaginary number.  That function is obviously messed up...

Solve your own audio issues  |  First Steps with RPi 3  |  Humidity & Condensation  |  Sleep & Hibernation  |  Overclocking RAM  |  Making Backups  |  Displays  |  4K / 8K / 16K / etc.  |  Do I need 80+ Platinum?

If you can read this you're using the wrong theme.  You can change it at the bottom.

Link to comment
https://linustechtips.com/topic/540653-java-nan/#findComment-7161803
Share on other sites

Link to post
Share on other sites

12 hours ago, mikestechs said:

Hello guys, I'm starting with Java programming and i just want to ask...

If i will have this:

-----------------

a = -1,5;

b = -2,2;

 

write = Math.pow (a,b);

System.out.println(write);

 

// I'll get this 

 

Nan

-----------------

 

And i want do something like if the "program" say NaN it will write something like "impossible" or something like that.

 

I'm new in this so i tried something with if ( write == "NaN")

but that was stupid idea becouse (write) will be always number. 

 

Any ideas :) ? 

 

 

Are you actually using commas (,) as decimal separators or is that just a typo on the forum? It should be points (.) anyway.

Link to comment
https://linustechtips.com/topic/540653-java-nan/#findComment-7163263
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×