0

I am basically totalling a number which becomes very large. I am using float as I wanted to retain the level of precision, but it only goes up to 100k. Here is some sample output.

 x 361 y 800 width 530 h 800 intensity 93581.52477873862
 x 362 y 800 width 530 h 800 intensity 93829.5759344697
 x 363 y 800 width 530 h 800 intensity 94079.55780857801
 x 364 y 800 width 530 h 800 intensity NaN
 x 365 y 800 width 530 h 800 intensity NaN
 x 366 y 800 width 530 h 800 intensity NaN

I have tried double etc. and had no luck! Anyone have any ideas on what is best to use?

Thanks

redrubia
  • 2,256
  • 6
  • 33
  • 47
  • 3
    Floats can go much higher than this, so something else is wrong. See also [this question](http://stackoverflow.com/questions/2618059/in-java-what-does-nan-mean) for some info on the meaning of NaN. Is your code dividing inadvertently zero by zero or something like that? – DNA Oct 01 '12 at 21:20

4 Answers4

2

Try BigDecimal. It keeps the precision (e.g. number of fraction digits) you want.

icza
  • 389,944
  • 63
  • 907
  • 827
  • I absolutely agree with icza's suggestion. If you are doing anything even remotely serious with decimal arithmetic (especially currency), you should *always* use 'BigDecimal'. There are plenty of answers here on SO to tell you why that is. – DuncanKinnear Oct 02 '12 at 19:44
2

float can "go higher then 10k" and double is always the preferred choice. If the numbers get really big and you need all digits, then use BigDecimal.

If you can't get above 10k, then there's some "other" problem in your calculation.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
0

Floats can go way higher than this - try for example:

    float x = 1000000000000000000000000000000000000.0f; 
    System.out.println(x);
    System.out.println(x*x);

which outputs:

1.0E36
Infinity

So something else is wrong. Note that using really big numbers results in Infinity not NaN.

Is your code dividing inadvertently zero by zero or something like that? What is the actual calculation you are performing?

See also this question for some info on the meaning of NaN.

Community
  • 1
  • 1
DNA
  • 42,007
  • 12
  • 107
  • 146
0

You are having a different problem than you think you are having.

Fact 1: float can get far larger than 10k. Around 340000000000000000000000000000000000k.

Fact 2: float addition overflows result in Infinity, not NaN (Not a Number).

You have an error in your mathematics somewhere. The calculation at x 364 is resulting in some arithmetic error that is messing up your sum. The precision is not the problem.

Dan Bliss
  • 1,694
  • 13
  • 10