Giter Site home page Giter Site logo

sqrt rounding about big-math HOT 2 CLOSED

eobermuhlner avatar eobermuhlner commented on June 14, 2024
sqrt rounding

from big-math.

Comments (2)

eobermuhlner avatar eobermuhlner commented on June 14, 2024

Actually the last digit in the calculated precision is not possible to calculate absolutely correct due to rounding issues beyond the specified precision.

For this reason the documentation of the Java 9 sqrt() function has a caveat:

The value of the returned result is always within one ulp of the exact decimal value for the precision in question. If the rounding mode is HALF_UP, HALF_DOWN, or HALF_EVEN, the result is within one half an ulp of the exact decimal value.

This holds also true for the calculations in BigDecimalMath.
I should probably add this to the documentation of BigDecimalMath.

The BigDecimalMath.sqrt() calculates with 6 digits additional precision and then rounds the result to the specified MathContext - which in average is more accurate than the Java 9 implementation of sqrt() (at least it was last time a looked at it).

from big-math.

MikeMcl avatar MikeMcl commented on June 14, 2024

This holds also true for the calculations in BigDecimalMath.

Not quite. The examples show the result can be off by just over half an ulp with rounding mode HALF_UP.

Anyway, I think your choice to calculate to 6 additional digits before rounding is perfectly reasonable. I just raised the issue for completeness because the behaviour isn't documented.

I think its only in Java 15 that BigDecimal sqrt gets the rounding spot on (at the cost of considerable added complexity).

from big-math.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.