Code Monkey home page Code Monkey logo

Comments (4)

jkinable avatar jkinable commented on August 25, 2024

Hi Mikhail,
Thanks for reporting this. This Math Utility was originally contributed by Frans Lelieveld, so I'm not very familiar with its implementation. Would you be able (and willing) to look into this issue and see whether this is actually a bug in the code, or for example a rounding issue? Your input is a very long number, and the scaled difference seems very small compared to the very big number you put in, so I wouldn't be surprised if this issue is caused by rounding.

from jorlib.

MikhailZvagelsky avatar MikhailZvagelsky commented on August 25, 2024

from jorlib.

MikhailZvagelsky avatar MikhailZvagelsky commented on August 25, 2024

Joris, I found that the method gives wrong answer for the number 4 * 10^74 + 1 with rounding mode UP. The square root of this number exceeds 2 * 10^37, so correct UP rounding with 1 digit precision should be the number 3 * 10^37. But

BigDecimal number = new BigDecimal(new BigInteger("4"), -74).add(BigDecimal.ONE);
mathContext = new MathContext(1, RoundingMode.UP);
System.out.println(MathUtil.sqrt(number, mathContext).toPlainString());

produces 2E+37.

from jorlib.

jkinable avatar jkinable commented on August 25, 2024

Hm I agree with your example. It seems that this error occurs with very
tiny numbers. Given that you were able to study the book which was used to
implement this algorithm, would you be able to spot the implementation
issue? The book refers to an implementation itsqrt.cc, which you can find
on the publisher's website:
http://extras.springer.com/2001/978-3-642-56735-3/hfloat/hfloat/src/hf/itsqrt.cc
The first line states: "#warning 'sqrt_iteration() is experimental (loss
of precision !)'". So I'm not surprised that you encounter precision errors
with such small numbers. Here's another implementation:
https://github.com/rizar/matrix/blob/master/src/com/github/rizar/bigdecimalmath/BigDecimalMath.java
Does this one have similar issues?
I'm happy to help you out wherever I can, but I have very little knowledge
about this specific domain...

Joris

On Fri, Apr 22, 2016 at 9:40 AM, Mikhail Zvagelsky <[email protected]

wrote:

Joris, I found that the method gives wrong answer for the number 4 *
10^74 + 1 with rounding mode UP. The square root of this number exceeds 2

  • 10^37, so correct UP rounding with 1 digit precision should be the
    number 3 * 10^37. But

BigDecimal number = new BigDecimal(new BigInteger("4"),
-74).add(BigDecimal.ONE);
mathContext = new MathContext(1, RoundingMode.UP);
System.out.println(MathUtil.sqrt(number, mathContext).toPlainString());

produces 2E+37.


You are receiving this because you commented.
Reply to this email directly or view it on GitHub
https://github.com/jkinable/jorlib/issues/4#issuecomment-213431613

from jorlib.

Related Issues (16)

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.