This commit is contained in:
Miloslav Ciz 2024-09-08 02:25:27 +02:00
parent b8828f4d2f
commit 5134e55998
5 changed files with 1828 additions and 1827 deletions

View file

@ -23,6 +23,7 @@ WORK IN PROGRESS
| code of conduct ([COC](coc.md)) | code of coercion, code of censorship | | code of conduct ([COC](coc.md)) | code of coercion, code of censorship |
| [comun](comun.md) | coomun { One friend suggested this :D ~drummyfish } | | [comun](comun.md) | coomun { One friend suggested this :D ~drummyfish } |
| consume | consoom (see also [coom](coom.md)) | | consume | consoom (see also [coom](coom.md)) |
| content delivery network | [censorship](censorship.md) delivery network |
| [copyright](copyright.md) | copywrong, copyrestriction, copyrape | | [copyright](copyright.md) | copywrong, copyrestriction, copyrape |
| [CSS](css.md) | cascading stylish [shit](shit.md) | | [CSS](css.md) | cascading stylish [shit](shit.md) |
| [C++](cpp.md) | crippled C | | [C++](cpp.md) | crippled C |

File diff suppressed because it is too large Load diff

22
sqrt.md
View file

@ -30,37 +30,41 @@ TODO
## Programming ## Programming
TODO Square root is a very common operation and oftentimes has to be VERY fast -- it's used a lot for example in [computer graphics](graphics.md) where it may need to be computed several million times per second. For this reason there oftentimes exist special instructions and otherwise hardware accelerated options, you will very likely have such function around on most computers -- in [C](c.md) math standard library there's the `sqrt` [floating point](float.md) function that will probably be very fast. But let's now consider you want to program your own square root, which may happen for example when dealing with [embedded](embedded.md) computers.
If we need extreme speed, we may use a [look up table](lut.md) with precomputed values. If we really need extreme speed, we may always use a [look up table](lut.md) with precomputed values. Of course a table with ALL the values would be very big, but remember we can make a much smaller table (where each item spans a bigger range) that will provide just a quick [estimate](approximation.md) from which you'll make just a few extra iterations towards the correct answer.
Within desired precision square root can be relatively quickly computed iteratively by [binary search](binary_search.md). Here is a simple [C](c.md) function computing integer square root this way: Within desired precision square root can be relatively quickly computed iteratively by [binary search](binary_search.md). Here is a simple [C](c.md) function computing integer square root this way:
{ I checked the code works for all values with 32 bit integer (remember that C specification allows integers to be as small as 16 bit though, remember to adjust the constants if you suspect you might hit this, overflows may happen). On my computer I measured this to be about 2 (with compiler optimization) to 4 (without) times slower than using the hardware accelerated float point stdlib function. ~drummyfish }
``` ```
unsigned int sqrt(unsigned int x) unsigned int sqrtInt(unsigned int x)
{ {
unsigned int l = 0, r = x / 2, m; unsigned int m, l = 0,
r = x < 8300000 ? x / 128 + 40 : 65535; // upper bound est. for 32 bit int
//r = x < 15000 ? x / 64 + 20 : 255; // <-- for 16 bit int use this
while (1) while (1)
{ {
if (r - l <= 1) if (l > r)
break; break;
m = (l + r) / 2; m = (l + r) / 2;
if (m * m > x) if (m * m > x)
r = m; r = m - 1;
else else
l = m; l = m + 1;
} }
return (r * r <= x ? r : l) + (x == 1); return r;
} }
``` ```
TODO: Heron's method TODO: Heron's method
The following is a **non-iterative [approximation](approximation.md)** of integer square root in [C](c.md) that has acceptable accuracy to about 1 million (maximum error from 1000 to 1000000 is about 7%): { Painstakingly made by me. ~drummyfish } The following is a **non-iterative [approximation](approximation.md)** of integer square root in [C](c.md) that has acceptable accuracy to about 1 million (maximum error from 1000 to 1000000 is about 7%): { Painstakingly made by me. This one was even faster than the stdlib function! ~drummyfish }
``` ```
int32_t sqrtApprox(int32_t x) int32_t sqrtApprox(int32_t x)

File diff suppressed because one or more lines are too long

View file

@ -3,9 +3,9 @@
This is an autogenerated article holding stats about this wiki. This is an autogenerated article holding stats about this wiki.
- number of articles: 593 - number of articles: 593
- number of commits: 877 - number of commits: 878
- total size of all texts in bytes: 4292310 - total size of all texts in bytes: 4301411
- total number of lines of article texts: 32718 - total number of lines of article texts: 32737
- number of script lines: 294 - number of script lines: 294
- occurrences of the word "person": 7 - occurrences of the word "person": 7
- occurrences of the word "nigger": 93 - occurrences of the word "nigger": 93
@ -35,60 +35,73 @@ longest articles:
top 50 5+ letter words: top 50 5+ letter words:
- which (2427) - which (2429)
- there (1861) - there (1864)
- people (1660) - people (1663)
- example (1448) - example (1452)
- other (1319) - other (1321)
- number (1237) - number (1237)
- about (1161) - about (1166)
- software (1155) - software (1157)
- program (976) - program (980)
- because (905) - because (906)
- their (893) - their (893)
- would (887) - would (888)
- called (827) - language (833)
- language (822) - called (829)
- being (813) - being (815)
- something (809) - something (812)
- things (804) - things (806)
- numbers (801) - numbers (801)
- simple (768) - simple (773)
- computer (753) - computer (755)
- without (723) - without (726)
- programming (712) - programming (717)
- function (707) - function (707)
- these (683) - these (684)
- different (678) - different (680)
- however (666) - however (670)
- system (650) - system (651)
- world (629) - world (629)
- doesn (621) - doesn (620)
- should (615) - should (617)
- while (593) - while (595)
- point (588) - point (588)
- games (583) - games (584)
- society (580) - society (580)
- drummyfish (563) - drummyfish (563)
- simply (557) - simply (557)
- using (552) - using (552)
- though (548) - still (552)
- still (548) - though (549)
- possible (540) - possible (541)
- memory (523) - memory (525)
- similar (520) - similar (520)
- https (517) - https (517)
- course (510) - course (511)
- value (503) - value (504)
- technology (498) - technology (498)
- always (497) - always (498)
- basically (488) - basically (489)
- really (479) - really (485)
- first (474) - first (476)
latest changes: latest changes:
``` ```
Date: Sat Sep 7 16:47:08 2024 +0200
diogenes.md
how_to.md
interplanetary_internet.md
jargon_file.md
loc.md
often_confused.md
programming.md
random_page.md
raycasting.md
steve_jobs.md
wiki_pages.md
wiki_stats.md
Date: Fri Sep 6 15:31:02 2024 +0200 Date: Fri Sep 6 15:31:02 2024 +0200
ai.md ai.md
doom.md doom.md
@ -109,23 +122,6 @@ Date: Thu Sep 5 21:54:17 2024 +0200
chess.md chess.md
cloudflare.md cloudflare.md
deep_blue.md deep_blue.md
drummyfish.md
gemini.md
how_to.md
jokes.md
lisp.md
lrs_dictionary.md
main.md
often_confused.md
oop.md
people.md
programming_language.md
random_page.md
smol_internet.md
soyence.md
wiby.md
wiki_pages.md
wiki_stats.md
``` ```
most wanted pages: most wanted pages:
@ -142,42 +138,42 @@ most wanted pages:
- [meme](meme.md) (10) - [meme](meme.md) (10)
- [drm](drm.md) (10) - [drm](drm.md) (10)
- [pointer](pointer.md) (9) - [pointer](pointer.md) (9)
- [emacs](emacs.md) (9)
- [syntax](syntax.md) (8) - [syntax](syntax.md) (8)
- [sdl](sdl.md) (8) - [sdl](sdl.md) (8)
- [hitler](hitler.md) (8) - [hitler](hitler.md) (8)
- [gpu](gpu.md) (8) - [gpu](gpu.md) (8)
- [gpl](gpl.md) (8) - [gpl](gpl.md) (8)
- [emacs](emacs.md) (8)
- [comun_shell](comun_shell.md) (8) - [comun_shell](comun_shell.md) (8)
- [war](war.md) (7) - [war](war.md) (7)
most popular and lonely pages: most popular and lonely pages:
- [lrs](lrs.md) (298) - [lrs](lrs.md) (299)
- [capitalism](capitalism.md) (247) - [capitalism](capitalism.md) (248)
- [c](c.md) (221) - [c](c.md) (223)
- [bloat](bloat.md) (214) - [bloat](bloat.md) (214)
- [free_software](free_software.md) (179) - [free_software](free_software.md) (179)
- [game](game.md) (142) - [game](game.md) (142)
- [suckless](suckless.md) (140) - [suckless](suckless.md) (140)
- [proprietary](proprietary.md) (125) - [proprietary](proprietary.md) (125)
- [minimalism](minimalism.md) (100) - [minimalism](minimalism.md) (101)
- [computer](computer.md) (99) - [computer](computer.md) (99)
- [kiss](kiss.md) (98) - [kiss](kiss.md) (98)
- [modern](modern.md) (95) - [modern](modern.md) (96)
- [gnu](gnu.md) (93) - [gnu](gnu.md) (93)
- [fun](fun.md) (93) - [fun](fun.md) (93)
- [linux](linux.md) (92) - [linux](linux.md) (92)
- [programming](programming.md) (90) - [programming](programming.md) (90)
- [censorship](censorship.md) (90) - [censorship](censorship.md) (90)
- [math](math.md) (88) - [math](math.md) (88)
- [hacking](hacking.md) (83)
- [fight_culture](fight_culture.md) (83) - [fight_culture](fight_culture.md) (83)
- [hacking](hacking.md) (82)
- [free_culture](free_culture.md) (82) - [free_culture](free_culture.md) (82)
- [shit](shit.md) (81)
- [less_retarded_society](less_retarded_society.md) (81) - [less_retarded_society](less_retarded_society.md) (81)
- [bullshit](bullshit.md) (81) - [bullshit](bullshit.md) (81)
- [shit](shit.md) (80) - [art](art.md) (79)
- [art](art.md) (78)
- [public_domain](public_domain.md) (77) - [public_domain](public_domain.md) (77)
- [corporation](corporation.md) (77) - [corporation](corporation.md) (77)
- [programming_language](programming_language.md) (75) - [programming_language](programming_language.md) (75)