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
- represent amounts/money - problems with rounding, rounding from base2 to base10 conversions but also additional currencies topic
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
- A number that have fixed digits in decimal might have a periodic digits in binary format (hence rounding errors)
- https://en.wikipedia.org/wiki/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)
andround(Y)
might be the real values and then is fine to use them in multiplication.
References
- When working with numbers you must be careful on rounding error accumulations
- https://encyclopediaofmath.org/wiki/Accumulation_of_errors
- https://en.wikipedia.org/wiki/Round-off_error
- What Every Computer Scientist Should Know About Floating-Point Arithmetic by David Goldberg
- concepts:
- stable algorithms - An algorithm or numerical process is called stable if small changes in the input only produce small changes in the output and it is called unstable if large changes in the output are produced.12(https://en.wikipedia.org/wiki/Round-off_error#cite_note-Collins_2005-12)
- Numerical computation guide - https://docs.oracle.com/cd/E19957-01/806-3568/ncgTOC.html
- https://ec.europa.eu/eurostat/statistics-explained/index.php?title=Tutorial:Rounding_of_numbers
money & double & BigDecimal
- Double should be used on engineering computations but even then the precision might play games on us
- Is best to have a system where you know for sure what is the mandatory precision
- infinite precision numbers: BigInteger & BigDecimal
- CONS: slow
- fixed precision?
- infinite precision numbers: BigInteger & BigDecimal
- https://stackoverflow.com/questions/3413448/double-vs-bigdecimal
- http://java-performance.info/bigdecimal-vs-double-in-financial-calculations/
- https://dzone.com/articles/never-use-float-and-double-for-monetary-calculatio