GithubHelp home page GithubHelp logo

Comments (8)

stephentyrone avatar stephentyrone commented on June 10, 2024 1

This is covered by the root function in the API, but the implementation needs to catch up:

Double.root(1000, 3)

This "should" produce 10, but there's currently a placeholder implementation that uses pow(x, 1/n). I'll re-purpose this bug to cover fixing that.

from swift-numerics.

ocampor avatar ocampor commented on June 10, 2024 1

Thanks for your answer! I was trying to understand to see if I could help.

Sent with GitHawk

from swift-numerics.

stephentyrone avatar stephentyrone commented on June 10, 2024

Related to #9

from swift-numerics.

ocampor avatar ocampor commented on June 10, 2024

Hey @stephentyrone, Do you think that the best solution is to implement it from scratch, or do you have something else in mind?

from swift-numerics.

stephentyrone avatar stephentyrone commented on June 10, 2024

Implementing from scratch is in some ways the simplest option, but also requires the most work (and incurs the most ongoing validation burden--I wrote the system pow for Apple's platforms, so I know exactly how to implement root, but I also definitely know very much how much work maintaining math library functions entails).

C2x should add rootn to the standard C library; ideally we'll simply use that when it's available. But, it remains to be seen just how quickly it becomes available on various platforms that Swift targets.

It's possible to fix-up exact cases (on platforms with a sub-ulp accurate pow) by computing pow(result, n) and comparing to x. This is really simple, but unnecessarily slow, and doesn't work if the host system pow isn't good enough.

We could special-case n=2 and n=3; that would cover 99% of exact use cases on Darwin (but I'm not sure about the quality of cbrt on other platforms), but still leaves the bug in place for the rare 1% of cases.

I think I'll probably end up implementing root directly, but there's other stuff that I'll attend to first, since the error here is tolerable most of the time.

from swift-numerics.

stephentyrone avatar stephentyrone commented on June 10, 2024

Ah. It's a deceptively tricky issue to handle well; if you're interested in a small quick fix, special casing 2 and 3 should be easy. The other steps are all pretty involved =)

from swift-numerics.

Roman-Kerimov avatar Roman-Kerimov commented on June 10, 2024

@stephentyrone, are there any cases where pow(x, 1/2) returns an inexact result?

from swift-numerics.

stephentyrone avatar stephentyrone commented on June 10, 2024

@Roman-Kerimov Not on a system with a good-quality math library (because 1/2 is representable, if pow has sub-ulp accuracy then pow(x, 1/2) will get all exact cases right). The problem with pow(x, 1/3) is that 1/3 is not representable in a binary floating-point type.

When a host-system pow does not have sub-ulp accuracy, then pow(x, 1/2) will probably be wrong for some cases as well.

from swift-numerics.

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.