-5

I was thinking of writing a class (actually a set of classes with one public generic class) for "infinite" integers, that stores fractions as fractions (no floating points) and could handle imaginary numbers and the like. Before I start, does this already exist?

Greg Valcourt
  • 691
  • 6
  • 13
  • 4
    I struggle to believe you are the first person with these requirements; so I can't believe it doesn't exist somewhere. But questions asking us to find or recommend libraries are off-topic. – Andy Turner Apr 15 '17 at 15:22
  • Take a look at Apache Commons' [Math](http://commons.apache.org/proper/commons-math/) libraries. It already has a pretty good amount of stuff to go by. That said, if you think you can do better than those guys, then go for it. – M. Prokhorov Apr 15 '17 at 15:23
  • That said, @AndyTurner is right - this is not really the place to ask if there is a library that does X. Usually there are, but those answers wouldn't be good because they spoil as libraries get deprecated and rereleased. – M. Prokhorov Apr 15 '17 at 15:27
  • You can easily write a class to store a numerator and a denominator, but IEEE754 floating point can only be so accurate. – Jacob G. Apr 15 '17 at 15:28
  • @matoni: You can represent imaginary numbers in `BigDecimal`? – T.J. Crowder Apr 15 '17 at 15:31
  • @AndyTurner The OP actually did not ask for a *library*. The essense of the question is "how do I represent infinitely accurate numbers in Java" which is OK in my book. And you don't need a library for that. – lexicore Apr 15 '17 at 15:50

2 Answers2

3

At risk of sounding pedantic: you've described several sets of numbers at once. Integers are just the positive and negative whole numbers, including zero. Fractions are the rational numbers, a superset of the integers. Imaginary numbers are another set, and the infinities are in the hyperreal and surreal sets.

The BigInteger class may be useful for storing your huge integers. It would be relatively easy to build a wrapper class with the sum of rational numbers (expressed as some BigInteger p over another BigInteger q), and you could extend these to have a rational part, imaginary part, infinitesimal part, and infinitely large part. This doesn't address irrational numbers, though.

1

I guess you're just looking for BigDecimal or BigInteger which can handle integers with arbitrary length or precision.

Concerning "imaginary numbers", I guess you're talking about complex numbers. Check Commons Math for that.

lexicore
  • 42,748
  • 17
  • 132
  • 221