1

Is there a data type that can handle/store numbers exceeding type double in Java ? As far as I know, Java type double can store numbers in range 1.0E+38 to 1.0E-45.

What if I need Java program to read numbers exceedeing digit precision of 1.0E+38 and 1.0E-45? for example 1.0E-180. Currently those numbers are recognized as 0.0 . Don't ask why I need such ridiculous numbers. I got what was given to me.

Rad226
  • 97
  • 8
  • 7
    `BigDecimal` should be able to handle those numbers. – OH GOD SPIDERS Jul 27 '21 at 10:12
  • The limits you are quoting are wrong. A 32-bits `float` goes from 1.0E-38 (1.4E-45 including subnormal values) to 3.4E+38. A 64-bits `double` goes from 1.0E-308 (4.9E-324 including subnormal values) to 1.8E+308. So `double` already meets your requirement. – Thomas Jul 27 '21 at 10:21
  • @Thomas actually I belive those limits are correct in regards to saftware I'm using. This software is Gephi node-graph viewer (written in Java). It will zero out edges between nodes in graph if weight value of those edges is precisely above range I posted. It may not apply to Java as a hole but it does apply to this software - and I'm looking for a workaround. – Rad226 Jul 27 '21 at 11:27
  • Unless you can get the source code for Gephi and modify it to handle the full range of doubles, you need to find new graphing software. – Gilbert Le Blanc Jul 27 '21 at 11:46
  • @GilbertLeBlanc Gephi source is available: https://github.com/gephi/gephi But that would require me to learn Java from scratch and I don't have the time. So I guess I'll try to persuade others to switch to something else. – Rad226 Jul 27 '21 at 12:48

1 Answers1

3

As far as I know, Java type double can store numbers in range 1.0E+38 to 1.0E-45.

Incorrect; it can go as far as 1.7976931348623157 * 10^308 and 4.9406564584124654 x 10^-324.

However, there are only at most 2^64 numbers that can be stored in a double (reason: Think about it. Pigeon hole principle). There are an infinite amount of numbers between 0 and 1, let alone between those 2 extremes. doubles work by silently rounding, all the time, to the nearest number that is one of the chosen few 2^64. Let's call those blessed numbers.

These numbers are not equally distributed. Near 0, there are A LOT of these, as you move away from 0 there are fewer and fewer. Eventually (at around 2^52), the distance between any 2 blessed numbers is more than 1.0.

BigDecimal is one solution. There are others (for starters, double CAN represent 1e-108 - you're doing something wrong in your 'translate input data to a double value' code), but keep in mind that doubles at those extremes are incredibly inaccurate.

BD with numbers like that are incredibly slow, and out of the box, BDs cant divide out of the box (for the same reason you can't divide 1 by 3 and get a perfect result: 0.333333... and where does that stop?) - you need to configure it so it cuts off at some point. They're hard to use, but perhaps your only option.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • I'd like to point out that in software that I use, that digit precision range of 1.0E+38 to 1.0E-45 holds. That software is Gephi node-graph viewer. Id edge between node in graph has weight value outside of said 1.0E+38 to 1.0E-45, it'll be treated as 0.0 and excluded from graph. – Rad226 Jul 27 '21 at 11:30