Comments (6)
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.
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.
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.
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.
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.
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)
- Thanks HOT 7
- Could you add Pow()? HOT 3
- Seems to be possible to convert into hardware implementation HOT 6
- Is this library correct in terms of Base10 calculations? HOT 1
- ReflectionTypeLoadException: The classes in the module cannot be loaded. HOT 2
- request for Fixed32 too HOT 4
- Function Pow bug report HOT 1
- how to transfer by network? HOT 2
- A bug when use Acos() to 2 normalized vector HOT 2
- Suggestion adding **Fixed Point** keyword in description HOT 1
- Is changing "Fractional_Places" supported? HOT 2
- Debugging Determinism? (discussion) HOT 6
- Converting float to Fix64 consistent across CPU architectures? HOT 5
- Determinism HOT 3
- Suggestion: need profiling.
- Problem: Sqrt() for Q48.15 format implementation HOT 1
- FixedMath.Net with Net 3.5 (Unity3D) HOT 6
- Any plans to make all conversions deterministic? HOT 15
- Conversions from float and double are unspecified in case of overflow
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fixedmath.net.