NUMBERS-147: Fix conversion from double to support 2^31

The value 2^31 can be in the numerator or denominator. Previously the
conversion from double supported up to Integer.MAX_VALUE which is

Adds common test cases for conversion from double with a max

Changed the conversion from a double to use the absolute value and
restore the sign at the end. The conversion is thus identical for
positive or negative values.

Throw an illegal argument exception if maxDenominator is zero. A zero
invalidates the conversion from a double with a restricted denominator.

Replace Math.abs with compareUnsigned. This is appropriate as the fraction p/q is
always positive.

Add test cases for overflow double conversions.

Fix the fall-back to always use p1/q1 when either p2 or q2 overflow.

Move the default max iterations to a constant.

Validate epsilon and max iterations are positive.
5 files changed
tree: e26ee15fe2788fe5994c8120753f2ac9d73c10d3
  1. .gitattributes
  2. .gitignore
  3. .travis.yml
  9. commons-numbers-angle/
  10. commons-numbers-arrays/
  11. commons-numbers-combinatorics/
  12. commons-numbers-complex-streams/
  13. commons-numbers-complex/
  14. commons-numbers-core/
  15. commons-numbers-examples/
  16. commons-numbers-field/
  17. commons-numbers-fraction/
  18. commons-numbers-gamma/
  19. commons-numbers-primes/
  20. commons-numbers-quaternion/
  21. commons-numbers-rootfinder/
  22. dist-archive/
  23. doc/
  24. pom.xml
  25. siteMods/
  26. src/

Apache Commons Numbers

Build Status Coverage Status Maven Central

The Apache Commons Numbers project provides number types and utilities.


More information can be found on the Apache Commons Numbers homepage. The Javadoc for each of the modules can be browsed:

Questions related to the usage of Apache Commons Numbers should be posted to the user mailing list.

Where can I get the latest release?

You can download source and binaries from our download page.

Alternatively you can pull it from the central Maven repositories:



We accept Pull Requests via GitHub. The developer mailing list is the main channel of communication for contributors. There are some guidelines which will make applying PRs easier for us:

  • No tabs! Please use spaces for indentation.
  • Respect the code style.
  • Create minimal diffs - disable on save actions like reformat source code or organize imports. If you feel the source code should be reformatted create a separate PR for this change.
  • Provide JUnit tests for your changes and make sure your changes don't break any existing tests by running mvn clean test.

If you plan to contribute on a regular basis, please consider filing a contributor license agreement. You can learn more about contributing via GitHub in our contribution guidelines.


This code is under the Apache Licence v2.

See the NOTICE file for required notices and attributions.


You like Apache Commons Numbers? Then donate back to the ASF to support the development.

Additional Resources