What was your method for sqrt approximation?
-
-
Vastauksena käyttäjille @DefPriPub ja @Peter_shirley
I will have to go back and look. Been long enough that I don’t recall. I’ll follow up when I can get to my computer.
1 vastaus 0 uudelleentwiittausta 0 tykkäystä -
It looks like I settled on a recursive newtonian approximation. Code is similar to this: https://gist.github.com/alexshtf/eb5128b3e3e143187794 … FWIW, I didn't bother trying to make it fast. I wanted constexpr and never got around to making a faster approximation.
1 vastaus 0 uudelleentwiittausta 1 tykkäys -
For a real-time approximation, I would probably unroll it to be non-recursive, and iterate a fixed number of times, probably templating on # of iterations so that I could have a few version. sqrt_fast and sqrt_precise, for instance.
2 vastausta 0 uudelleentwiittausta 0 tykkäystä -
Vastauksena käyttäjille @tloch14 ja @Peter_shirley
Do you have a fast sqrt() function (or approximation) for double precision floats? All the ones I'm finding online are for 32 bit, or for the inverse sq. root which is what I don't want to calculate.
1 vastaus 0 uudelleentwiittausta 0 tykkäystä -
Vastauksena käyttäjille @DefPriPub ja @Peter_shirley
I don’t. You’re right about there being a lot of 32 bit ones, and they usually rely on UB, reinterpret-casting and bit shifting for their speed. Sorry.
1 vastaus 0 uudelleentwiittausta 0 tykkäystä -
https://www.codeproject.com/Articles/69941/Best-Square-Root-Method-Algorithm-Function-Precisi … This page has several implementations and some are double. Only one that's faster than std::sqrt. Could be a good starting place for implementing your own.
1 vastaus 0 uudelleentwiittausta 0 tykkäystä -
For instance, if you iterate sqrt8 (babylonian method) a fixed number of times instead of comparing to see if it's close enough, you may get a reasonable speed/precision tradeoff.
1 vastaus 0 uudelleentwiittausta 0 tykkäystä -
Vastauksena käyttäjille @tloch14 ja @Peter_shirley
After about two days I've trying, I'd like to report that I haven't been able to find an approximation that was faster. In most cases, I wasn't able to beat std::sqrt(). The times that I did, my approximation was horribly in correct. std::sqrt() is king. All shall praise it.
6 vastausta 1 uudelleentwiittaus 2 tykkäystä -
Vastauksena käyttäjille @DefPriPub ja @Peter_shirley
I was afraid this might be the case. With cos/sin, the domain is small (-p/2i to pi/2) so you can modulo your input and work with small numbers to reduce error from so few iterations. Such a benefit can’t be said about sqrt. (At least I don’t think)
1 vastaus 0 uudelleentwiittausta 0 tykkäystä
That domain should be -pi/2 ... pi/2 but I have fat fingers.
Lataaminen näyttää kestävän hetken.
Twitter saattaa olla ruuhkautunut tai ongelma on muuten hetkellinen. Yritä uudelleen tai käy Twitterin tilasivulla saadaksesi lisätietoja.