Intro to Numbers - raisercostin/software-wiki GitHub Wiki

Topics: rounding, ceil, floor, base, integer, decimal, precision, mantis, exponent, significant, statistics

There are different needs when working with numbers in real systems

Java - fixed point arithmetic's (replacements of double, float)

Read this https://jvns.ca/blog/2023/01/13/examples-of-floating-point-problems/

  • https://www.google.com/search?q=java+fast+decimal+limbrary
  • arbitrary precision: BigInteger, BigDecimal
    • BigDecimal: Part of Java’s standard library, ideal for high-precision arithmetic.
  • decimal4j - Java library for fast fixed-point arithmetic based on longs with support for up to 18 decimal places.
  • Apache Commons Math Apache Commons Math: Comprehensive library providing advanced mathematics and statistics functions, including linear algebra, probability, and optimization.
  • Joda-Money Joda-Money: Specifically designed for handling money and currency, offering robust support for currencies and monetary amounts.
  • Java Money (JSR 354) Java Money (JSR 354): Standard API for representing, transporting, and performing calculations with Money and Currency.
  • JScience: Provides a robust framework for scientific computing, including units and measures, which are essential for precise calculations.
  • EJML EJML (Efficient Java Matrix Library): A library for linear algebra and matrix operations, optimized for speed and memory usage.
  • NumPy4J NumPy4J: A library that allows for NumPy-like operations in Java, useful for those familiar with NumPy from Python.
  • JGraphT - JGraphT: A library for graph theory, offering algorithms for numerical problems related to graph structures.
  • Colt Colt: Provides scalable scientific and technical computing in Java, with an emphasis on matrices and numerical algorithms.
  • Java Algebra System JAS (Java Algebra System): A library focused on algebra and polynomial calculations.

Rounding is different depending on the radix

Amounts of money should always be in fixed precision

  • Always use BigDecimal instead of double and float for storing amounts in arbitrary precision.

Numeric rounding accumulation

Samples

  • round(X)*round(Y) is very different and always worse than round(X*Y)`
  • sometimes for accounting purposes round(X) and round(Y) might be the real values and then is fine to use them in multiplication.

References

money & double & BigDecimal