How to handle floats and avoid NaN

  • Thread starter mehtakrishna2002
  • Start date
M

mehtakrishna2002

Hi,
I am trying to do some matrix manipulation using a matrix module in
java that i downloaded from the web. When I deal with large matrices
(60x60), I start getting NaN for a lot of my calculations. I think It
is due to the way in which Java handles floats.
Can someone suggest a fix for this problem?
Regards,
Krishna
 
O

Oliver Wong

Hi,
I am trying to do some matrix manipulation using a matrix module in
java that i downloaded from the web. When I deal with large matrices
(60x60), I start getting NaN for a lot of my calculations. I think It
is due to the way in which Java handles floats.
Can someone suggest a fix for this problem?

Try to find out exactly what sequence of operations you're applying to
get NaN (division by zero?)

Alternatively, post some code.

- Oliver
 
G

Googmeister

Hi,
I am trying to do some matrix manipulation using a matrix module in
java that i downloaded from the web. When I deal with large matrices
(60x60), I start getting NaN for a lot of my calculations. I think It
is due to the way in which Java handles floats.
Can someone suggest a fix for this problem?
Regards,
Krishna

Are you sure it's not a problem with your mathematical model
or algorithm? Java handles floating point values using (a subset
of) the IEEE standard, so you would probably experience
similar behavhior in every other numerical computing
environment where you do the same computation.
 
P

Patricia Shanahan

Hi,
I am trying to do some matrix manipulation using a matrix module in
java that i downloaded from the web. When I deal with large matrices
(60x60), I start getting NaN for a lot of my calculations. I think It
is due to the way in which Java handles floats.
Can someone suggest a fix for this problem?
Regards,
Krishna

Usually, you should use double rather than float. You may already be
doing so, and just using "floats" to mean either floating point type.

Your problem is probably not "due to the way in which Java handles
floats", because other Java floating point programs do not exhibit it.
For example, the Java linpack at
http://www.netlib.org/benchmark/linpackjava/ solves a system of 500
linear equations, factorizing a 500x500 dense matrix, without getting
into NaN trouble.

Appearance of NaNs in the middle of a calculation usually indicates
either an implementation problem or an unsuitable algorithm for the data
size and other characteristics. The fix is to analyze where the NaNs are
coming from.

Patricia
 
R

Rebecca

there must be soemthing in your calculations that is causing a NaN. Y
don't you trace it and see where it's happening? Like maybe you are
dividing by zero. That would cause a NaN. Or square root of a negative
number. There are all things you'll need to handle.
 
R

Roedy Green

I am trying to do some matrix manipulation using a matrix module in
java that i downloaded from the web. When I deal with large matrices
(60x60), I start getting NaN for a lot of my calculations. I think It
is due to the way in which Java handles floats.

1. use doubles
2. check earlier for NaNs rather than letting them propagate in
useless calculation.

The problem is essentially some mathematical algorithms sometimes
produce infinities.
 
O

Oliver Wong

Roedy Green said:
1. use doubles
2. check earlier for NaNs rather than letting them propagate in
useless calculation.

The problem is essentially some mathematical algorithms sometimes
produce infinities.

AFAIK, IEEE standards (which Java uses), has values for positive and
negative infinity which are distinct from NaN.

- Oliver
 
G

Googmeister

Oliver said:
AFAIK, IEEE standards (which Java uses), has values for positive and
negative infinity which are distinct from NaN.

You are correct, but producing infinities can subsequently lead to
NaNs,
e.g., if you compute Infinity - Infinity.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top