Giter VIP home page Giter VIP logo

Comments (6)

asik avatar asik commented on July 24, 2024

If you can be confident Math.Sqrt will give you the same results on all runtimes and machines you're targeting, then by all means feel free to use it. I think that on older versions of .NET, the 32-bit runtime will emit FSQRT while newer ones will use equivalent SSE instruction.

from fixedmath.net.

urben1680 avatar urben1680 commented on July 24, 2024

I decided to not use it as I cannot be sure it generates the same result.
What I did instead is creating a lookup table. The value of index i is Convert.ToInt32((i+0.5)*(i+0.5)) so using BinarySearch results in finding the value without need to check for rounding. Only thing to do is getting the absolute value. First tests indicate it is just 4 times slower than Math.Sqrt on my machine.

I am not sure if it is useful for you too as your binary tree would be twice as high at Fix64 and probably won't be as fast per step when using long.

from fixedmath.net.

asik avatar asik commented on July 24, 2024

The tradeoff is going be in accuracy, unless you make the LUT huge. Especially with numbers very close to 0, given the nature of sqrt. But maybe it'd be worth having a FastSqrt if it's good enough.

from fixedmath.net.

urben1680 avatar urben1680 commented on July 24, 2024

In the case of 32bit integers there are just sqrt(2^31-1) possible numbers which can be squared without overflow. So my table only needs that many entries and is as accurate as possible.

64bit integers need a tree with 1024 times the filesize if you want the same accuracy but of course that is going to be too huge.

from fixedmath.net.

asik avatar asik commented on July 24, 2024

Ah it's a reverse lookup and you find the value by binary search, ok yeah that's a good idea. Thanks for sharing.

from fixedmath.net.

urben1680 avatar urben1680 commented on July 24, 2024

I want to give you an update as I again worked on my Sqrt function.
My earlier descibed method does not work as I miscalculated how big the LUT has to be (I forgot it is not just natural numbers). It is not efficient at all anymore. Turned out that the method you use is still the fastest suited for me.

But I accomplished another thing. I came up with a method that calculates Pythagoras' theorem, which calculates the long side of a right-angled triangle. This method is 20% faster than when used Sqrt for the same task.

  • I first calculate the ratio of a and b, with a being the smaller number.
  • Then I put the value into my LUT with precalculated numbers of sqrt(value*value+1).
  • The result is multiplied with b using my fixed-point multiply method.
  • Now I have c with the highest possible precision.

The LUT contains 2^precision_bits numbers. In my case 2^18.

The LUT might turn out too big for you and Pythagoras' theorem is not that important for this case, but still you might find this interesting. I skipped how I came up with the LUT but if you really want to know that I can add that as well.

from fixedmath.net.

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.