Someone taught me how to approx distance between two points without sqrt.
max(abs(x),abs(y)) + min(abs(x),abs(y))/2
https://git.sr.ht/~rabbits/decadv/tree/main/item/tcc/day5.tal#L50
This entry was edited (11 months ago)
Someone taught me how to approx distance between two points without sqrt.
max(abs(x),abs(y)) + min(abs(x),abs(y))/2
https://git.sr.ht/~rabbits/decadv/tree/main/item/tcc/day5.tal#L50
Diego F. Goberna
in reply to Devine Lu Linvega • • •phoebos
in reply to Devine Lu Linvega • • •Devine Lu Linvega
in reply to phoebos • • •~rabbits/decadv (main): tcc/day5.tal - sourcehut git
git.sr.htpaloma kop
in reply to Devine Lu Linvega • • •Devine Lu Linvega
Unknown parent • • •Devine Lu Linvega
in reply to Devine Lu Linvega • • •Devine Lu Linvega
in reply to Devine Lu Linvega • • •Devine Lu Linvega
in reply to Devine Lu Linvega • • •DHeadshot's Alt
in reply to Devine Lu Linvega • • •Lovebyte Demoparty
in reply to Devine Lu Linvega • • •🌈 Andrew ☄️
in reply to Devine Lu Linvega • • •Nifty! I've been doing an approximate `sqrt(n)` recently with this iterative approach:
a = n
for 1..m:
a = (a + n / a) / 2
So I tried this using your approach for an initial guess:
a = max(abs(x),abs(y)) + min(abs(x),abs(y))/2;
s = x*x + y*y;
if (a != 0.0) {
a = (a + s / a) / 2;
a = (a + s / a) / 2;
}
Within a range of -100 to 100 this takes your approximation from a max error of about 34% to a max error of 0.4%. The average error goes from 9% to 0.4%.
It's still just 7% max error and 0.4% average error if you only do one iteration.
Sorry, no fancy visualisations, but here’s a picture of them both overlayed (I considered a “they're the same picture" meme instead):
Devine Lu Linvega
Unknown parent • • •Tom Forsyth
in reply to Devine Lu Linvega • • •https://flylib.com/books/en/2.729.1/high_speed_vector_magnitude_approximation.html
HIGH-SPEED VECTOR MAGNITUDE APPROXIMATION | Chapter Thirteen. Digital Signal Processing Tricks
flylib.comjohnfredcee
in reply to Devine Lu Linvega • • •