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 |
| [comun](comun.md) | coomun { One friend suggested this :D ~drummyfish } |
| consume | consoom (see also [coom](coom.md)) |
| content delivery network | [censorship](censorship.md) delivery network |
| [copyright](copyright.md) | copywrong, copyrestriction, copyrape |
| [CSS](css.md) | cascading stylish [shit](shit.md) |
| [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
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:
{ 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)
{
if (r - l <= 1)
if (l > r)
break;
m = (l + r) / 2;
if (m * m > x)
r = m;
r = m - 1;
else
l = m;
l = m + 1;
}
return (r * r <= x ? r : l) + (x == 1);
return r;
}
```
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)

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