Talk:Square root algorithms: Difference between revisions

Content deleted Content added
Credit for Quake3 InvSqrt() algorithm: just make sure it is there
Update archiving templates after a page move (Report bot issues)
 
(226 intermediate revisions by 78 users not shown)
Line 1:
{{User:MiszaBot/config
==Ibn al-Banna==
| algo = old(360d)
| archive = Talk:Square root algorithms/Archive %(counter)d
| counter = 1
| maxarchivesize = 150K
| archiveheader = {{Automatic archive navigator}}
| minthreadstoarchive = 2
| minthreadsleft = 5
}}
{{WikiProject banner shell|class=C|
{{WikiProject Mathematics|importance= low}}
}}
{{archive box|auto=long}}
 
== Reciprocal of the square root ==
*Surprising that the user [[user:pgk]] removed the section on [[Ibn al-Banna]] method of computing square roots. Its good if a reason is also provided for the edit done on the page. (LATER: the user has emailed me giving the reason for his edit: bad formatting)
:Actually, this method appears already in the article, under the section "Babylonian Method". Do you have any reference that it is [[Ibn al-Banna]] who invented the method? This seems questionable. Also, notice that the first time you edited the article, a large portion of it was deleted - See this [http://en.wikipedia.org/w/index.php?title=Methods_of_computing_square_roots&diff=59669964&oldid=58125411 diff]. My guess is that you have used an external text editing program, and something went bad with the copying back and forth. You should also use edit summaries when editing articles. -- [[User:Meni Rosenfeld|Meni Rosenfeld]] ([[User Talk:Meni Rosenfeld|talk]]) 13:50, 21 June 2006 (UTC)
 
This piece of code is a composite of a quirky square root starting estimate and Newton's method iterations. Newton's method is covered elsewhere; there's a section on '''Rough estimate''' already - the estimation code should go there. Also, I first saw this trick in Burroughs B6700 system intrinsics about 1979, and it predated my tenure there, so it's been around a long time. That's well before the drafting of IEEE 754 in 1985. Since the trick is based on linear approximation of an arc seqment of x<sup>2</sup> which in the end, is how all estimates must be made, I'm certain that the method has been reinvented a number of times.
*I agree that something definitely went wrong while editing the article first time. As for the reference, it is mentioned in the book 'A History of Algorithms: from the Pebble to the Microchip' by Barbin and Borowczyk. Maybe we should change the heading title of the Babylonian Method to the Ibn-Al Banna Method? [[User:Chem1|Chem1]]
 
There're numerous issues with this code:
:No, "Babylonian method" is a very well known name for this, and Ibn-Al Banna came much, much later. You might want to add a reference to him in the Babylonian section (and remove the redundant information). I am wondering why he is notable for discovering something that had already been discovered. Could you provide a quote from your source? - [[User:Rainwarrior|Rainwarrior]] 15:38, 21 June 2006 (UTC)
*the result of type punning via pointer dereferencing in C/C++ is undefined
::I've had a look in said book at [http://www.amazon.com/gp/sitbv3/reader/002-4489914-8840066?asin=3540633693&pageID=S05W&checkSum=Yku6hBg0butI/B1ma/mOsKxFLOvqnF/ltkkJ0vWADaI= Amazon reader], and what you are probably referring to is from page 202 (in the edition I looked at), which says "The method was described in the 13th century by the mathematician Ibn al-Banna". But the method mentioned is ''not'' the x := (x + r/x) / 2 method, which is clearly described in the text as being known much before al-Banna (for example, it was known to Theon of Alexandria around 370 AD). So, please remove the irrelevant content from the [[Ibn al-Banna]] article, and be more careful when making edits in the future. -- [[User:Meni Rosenfeld|Meni Rosenfeld]] ([[User Talk:Meni Rosenfeld|talk]]) 19:19, 21 June 2006 (UTC)
*the result of bit-twiddling floating point numbers, bypassing the API, is undefined
*we don't know whether values like zero, infinity and unnormalized floating point numbers, and big/little endian formats are correctly handled.
*It definitely won't work on architectures like Unisys mainframes which are 48/96 bit, or 64-bit IEEE floats. Restructuring the expression to make it work in those cases is non-trivial.
*since I can readily find, by testing incremental offsets from the original, a constant which reduces the maximum error, the original constant isn't optimal, probably resulting from trial and error. How does one verify something that's basically a plausible random number? That it works for a range of typical values is cold comfort. (Because its only use is as an estimate, maybe we don't actually care that enumerable cases aren't handled(?)... they'll just converge slowly.)
*because it requires a multiply to get back a square root, on architectures without fast multiply, it won't be such a quick estimate relative to others (if multiply and divide are roughly comparable, it'll be no faster than a random seed plus a Newton iteration).
 
I think we should include at least an outline of the derivation of the estimate expression, thus: a normalized floating point number is basically some power of the base multiplied by 1+k, where 0 <= k < 1. The '1' is not represented in the mantissa, but is implicit. The square root of a number around 1, i.e. 1+k, is (as a linear approximation) 1+k/2. Shifting the fp number represented as an integer down by 1 effectively divides k by 2, since the '1' is not represented. Subtracting that from a constant fixes up the 'smeared' mantissa and exponent, and leaves the sign bit flipped, so the result is an estimate of the reciprocal square root, which requires a multiply to re-vert back to the square root. It's just a quick way of guessing that gives you 1+ digits of precision, not an algorithm.
* I have removed the mentioning of this method from the [[Ibn al-Banna]] article. Also, I have removed the section from the [[Methods of computing square roots]] article and have added a line to the Babylonian section about al-Banna describing a similar method.
 
That cryptic constant is actually a composite of three bitfields, and twiddling it requires some understanding of what those fields are. It would be clearer, but a few more operations, to do that line as a pair of bitfield extract/inserts. But we're saving divides in the subsequent iterations, so the extra 1-cycle operations are a wash.
::I would say based on the descriptions given that it is the same method, not merely similar. But, this isn't important. Do you know any more about Ibn al-Banna? His biography page has very little information so far... - [[User:Rainwarrior|Rainwarrior]] 01:23, 22 June 2006 (UTC)
 
== Undefined behaviour ==
 
The examples using unions are invalid C, as they invoke undefined behaviour. An easy solution that is probably even clearer for the purpose of example code would be to use memcpy, e.g.
* I have found some stuff on the Internet [http://www.bookrags.com/biography-ibn-al-banna-scit-02123/index.html here] and [http://www.muslimheritage.com/topics/default.cfm?TaxonomyTypeID=101&TaxonomySubTypeID=19&TaxonomyThirdLevelID=134&ArticleID=445 here]. Is it OK to mention this?
::Yes, it is okay to add this information to the [[Ibn al-Banna]] article, as long as you don't copy it word for word (otherwise it's a copyright violation) and provide links to these sources, so that the information can be checked. About al-Banna's method: It is difficult to say for sure, according to the text in the book, what exactly is the algorithm - It gives an equation which should be solved, but doesn't specify the way to solve it - but my impression is that this is not the Babylonian method, but rather something that is reminiscent of the long division-like method. -- [[User:Meni Rosenfeld|Meni Rosenfeld]] ([[User Talk:Meni Rosenfeld|talk]]) 14:23, 22 June 2006 (UTC)
 
float f;
::I am currently trying to understand the method given in the book and attributed to [[Ibn al-Banna|al-Banna]]. Hopefully I will come up with a 'layman' explanation for this method.
uint32_t u;
memcpy (&u, &f, sizeof (u));
 
[[Special:Contributions/37.49.68.13|37.49.68.13]] ([[User talk:37.49.68.13|talk]]) 13:08, 1 April 2022 (UTC)
:::al-Banna's method seems to be the same as the "long-division-like algorithm" already described in this article, '''not''' the 0.5(x+r/x) one. I think, in the course of editing, his section got moved around or otherwise mis-associated. --[[User:Histrion|Jay (Histrion)]] ([[User talk:Histrion|talk]] • [[Special:Contributions/Histrion|contribs]]) 20:59, 22 June 2006 (UTC)
 
:Type punning with unions is undefined in C++, but not in C. This is a topic of much confusion.
* Have a look [http://www.amazon.com/gp/sitbv3/reader/ref=sib_dp_srch_pop/103-0386412-8294252?%5Fencoding=UTF8&keywords=al-banna&v=search-inside&asin=3540633693# here]. This is the algorithm for the method used by al-Banna. ''NOTE: this link opens up search results in the Amazon Reader. Click Next at the bottom of the page and click link # 12 on the resulting page. Pages 206-207 describe in detail what the 'root, not a root' method is''. Cheers!
:The following is pulled from a footnote around section 6.5.2.3 of the C17 standard:
:"If the member used to read the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called “type punning”). This might be a trap representation."
:This basically says, 'you may use a union to reinterpret the bits of one type into another but we're not going to promise that the new interpretation will be valid'
:I will say that the C code in this article is rather clunky and may benefit from a bitfield to separate the different sections of the float representation so it is easier to read and understand, but I will have to flatly disagree with you that <code>memcpy() </code>is more appropriate than a union in this code snippet. [[User:WillisHershey|WillisHershey]] ([[User talk:WillisHershey|talk]]) 17:24, 25 September 2023 (UTC)
 
== Lucas sequence method - original research? ==
== Duplication? ==
 
I could not find any relevant research papers on the use of Lucas sequences for computing real square roots.
A lot of the algorithms in this article are variations on each other; for instance, the N + d/2N method is just the first iteration of Newton's algorithm; and the section listed under "Finding square roots using mental arithmetic" is really just the long-division-like algorithm without writing it out in a long division format. Can we either remove some of the redundancy, or, perhaps more usefully, tie the related algorithms together better? --[[User:Histrion|Jay (Histrion)]] ([[User talk:Histrion|talk]] • [[Special:Contributions/Histrion|contribs]]) 16:27, 22 June 2006 (UTC)
The closest I found is
 
G. Adj and F. Rodríguez-Henríquez, "Square Root Computation over Even Extension Fields," in IEEE Transactions on Computers, vol. 63, no. 11, pp. 2829-2841, Nov. 2014, doi: 10.1109/TC.2013.145.
i agree, at least the section under "Quadratic equation method" can be deleted completely, because it's explanation is way to complicated and the result basically boils down to the babylonian formula. It's obvious if you look closely at the given formula sqrt(r)=N+d/(N+sqrt(r)), which will be iterated to approximate sqrt(r). N is an initial guess (the same as x0 in the babylonian method) and d=r-N^2. if you substitute sqrt(r) with x (just another name) and N with x (since N is an approximation of x), the formula reads x=x+(r-x^2)/(x+x), which reduces to x=(x+r/x)/2, the babylonian formula. Besides the formula - due to the usage of constant N & d - converges slower. If nobody objects, i'll remove the section [[User:Catskineater|Catskineater]] 19:44, 1 September 2006 (UTC)
 
which is concerned with square roots in finite fields and uses a different algorithm.
:I think you could delete any or all of this article, except the Babylonian method which is the only method worth using. In any case, the article is much too long. [[User:JRSpriggs|JRSpriggs]] 05:32, 2 September 2006 (UTC)
Should this paragraph be removed as original research?
Or it could also simply be made much shorter, by avoiding to repeat the properties of Lucas sequences. [[User:BlueRavel|BlueRavel]] ([[User talk:BlueRavel|talk]]) 23:27, 3 December 2023 (UTC)
 
: {{ping|BlueRavel}} I have searched, and I too failed to find any relevant source for this. It was posted into the article in 2009 without any explanation, by an editor who has never made any other substantial contribution, just one other very small edit. It looks as though it may well be original research, but whether it is or not, it is unsourced, so I have removed it. [[User:JBW|JBW]] ([[User talk:JBW|talk]]) 21:54, 5 December 2023 (UTC)
== Spring cleaning! ==
 
== Merge "Approximations that depend on the floating point representation" into "Initial estimate" ==
Yes, the article has been in a bad state for too long. I am currently in the middle of some major changes to the article, mostly trimming down some irrelevant information. I hope to be finished by tomorrow. Feedback welcome. -- [[User:Meni Rosenfeld|Meni Rosenfeld]] ([[User Talk:Meni Rosenfeld|talk]]) 15:42, 8 September 2006 (UTC)
 
I believe the section "Approximations that depend on the floating point representation" should be merged into "Initial estimate", since it is a special case of "Binary estimates". Merging would clear up the fact that the floating point trick gives an initial rough approximation, which is then typically iteratively improved.
:The article seems to be using "''r''" for the number whose square root is desired. Since "root" begins with "r", this is counter-intuitive. I suggest we pick another symbol for that number! And perhaps we could use "r" for the root itself? [[User:JRSpriggs|JRSpriggs]] 07:22, 9 September 2006 (UTC)
 
I also believe the "Initial estimate" section should appear after the section on Heron's method, as the reader is likely more interested in the general idea of iterative refinement than in the details of how to obtain a good initial estimate in all possible ways.
I'm not sure this is necessary, but I won't object to it. Which letter do you have in mind? As for using "r" for the root, I'm rather against it - It will be clearer if we explicitly write <math>\sqrt{number}</math> every time. -- [[User:Meni Rosenfeld|Meni Rosenfeld]] ([[User Talk:Meni Rosenfeld|talk]]) 08:53, 9 September 2006 (UTC)
 
Additionally, in my opinion the entirety of the article could benefit from some trimming/rewriting, as many sections contain redundant information, unnecessary details, and awkward formulations. [[User:BlueRavel|BlueRavel]] ([[User talk:BlueRavel|talk]]) 14:54, 4 December 2023 (UTC)
:How about using ''S'' for the square whose root is desired? Do we need a consistent convention for the iterative approximations to the root? [[User:JRSpriggs|JRSpriggs]] 09:52, 10 September 2006 (UTC)
 
:: Your proposition makes sense to me, and I dont necessarily disagree. That said though, as a pure mathematician, I am uninclined to blur the lines between programmatical issues and mathematical problems. I think maintaining a distinction is appropriate. An analysis of the pure mathematical problem of initial estimation in these abstract reiterative processes is a decidedly distinct discussion from considerations in this programming language, or that programming language, or this architecture, or that architecture. The former is future-proofed, the latter is not. [[User:CogitoErgoCogitoSum|CogitoErgoCogitoSum]] ([[User talk:CogitoErgoCogitoSum|talk]]) 21:09, 11 February 2024 (UTC)
Fine with me. The article currently uses ''x''<sub>''n''</sub> for the iterations wherever it is applicable. -- [[User:Meni Rosenfeld|Meni Rosenfeld]] ([[User Talk:Meni Rosenfeld|talk]]) 10:55, 10 September 2006 (UTC)
 
== Finished,Useful more or lessaddition?? ==
Not sure if its useful, but I have found that, in general, <math>\sqrt{x+2} \approx \frac{x+1}{\sqrt{x}}</math>, and if {{math|''x''{{=}}''n''{{sup|''2''}}}} we get <math>\sqrt{n^2+2} \approx n + \frac{1}{n}</math>.
 
Similarly <math>\sqrt{x+4} \approx \frac{x+2}{\sqrt{x}}</math>.
The one section I don't know what to make of is [[Methods of computing square roots#Approximations that depend on IEEE representation|Approximations that depend on IEEE representation]] (the last one). It looks like a useful section; But it looks like it can be written better, and I don't have the knowledge to do it. If anyone with better understanding of IEEE can take a look at it, it would be great. Also, adding references would be good, though I don't know of any good sources. -- [[User:Meni Rosenfeld|Meni Rosenfeld]] ([[User Talk:Meni Rosenfeld|talk]]) 14:45, 9 September 2006 (UTC)
 
I sometimes use this for quick pencil and paper calculations, if Im close enough to a convenient value.
:I've been trying to find a source for it for a while to no avail. I understand IEEE but I'd have to take a close look at it to understand what's going on (it's kind of a reverse engineering problem). What we have now describes how to do it (maybe, I haven't been able to verify that it works), but not why it works. I think it's probably a valid technique, and is certainly of use to graphics programmers who may need a quick way to normalize a vector, but I haven't taken the time to look at it yet. - [[User:Rainwarrior|Rainwarrior]] 23:28, 9 September 2006 (UTC)
 
Not sure if this is a known or established property, proven, bounded, or if its already in the article in some alternative capacity, or if its even appropriate for this article. I do know the taylor series approximation with two terms connects these expressions.
== Digit by digit method is not fully explained. ==
[[User:CogitoErgoCogitoSum|CogitoErgoCogitoSum]] ([[User talk:CogitoErgoCogitoSum|talk]]) 21:05, 11 February 2024 (UTC)
: There is nothing special about 2 and 4: <math>\sqrt{x+2c} \approx \frac{x+c}{\sqrt{x}}</math> provided that c is small compared to x. This is, in fact, just the first two terms of the series given in the article under the section heading "Taylor series". [[User:JBW|JBW]] ([[User talk:JBW|talk]]) 01:45, 13 February 2024 (UTC)
 
: I don't think they are useful. In the first, you have replaced a square root and an addition with a square root, an addition, and a division to get an approximate answer. [[User:Bubba73|Bubba73]] <sup>[[User talk:Bubba73|You talkin' to me?]]</sup> 08:02, 13 February 2024 (UTC)
As explained in the current article, it doesn't provide enough
details of the method so one could apply it.
 
You could find a more detailed explanation: http://www.mathpath.org/Algor/squareroot/algor.square.root.htm
{{unsigned|Muttley.meen}}
 
:Care to explain which detail was missing? Often, providing excessive details makes it ''harder'' to apply the method. That's what examples are for. -- [[User:Meni Rosenfeld|Meni Rosenfeld]] ([[User Talk:Meni Rosenfeld|talk]]) 11:29, 16 September 2006 (UTC)
 
::I hope the edit which I did on 15 Sept 2006 made it clearer. Is there still a problem? [[User:JRSpriggs|JRSpriggs]] 03:09, 18 September 2006 (UTC)
 
:::Sory for the late reply. Now looks better. As for the example, yes, most of the time is better but the definition of the algorithm is also needed. -- [[User:Muttley.meen|Muttley.meen]] ([[User Talk:Muttley.meen|talk]])
 
== Why is it called the "Babylonian method"? ==
 
I added three notes related to the Babylonian method for computing square roots.
Is the ancient Babylonian method essentially the same as the method that is now called the "Babylonian method"? --[[User:Jtir|Jtir]] 20:20, 12 October 2006 (UTC)
:Basically, it is not known how the Babylonians computed square roots, although there are informed conjectures. I still haven't discovered the origin of the term "Babylonian method" in modern usage. I put my conclusions in a note here: [[Square root of 2#Notes]] and simplified the note in this article. --[[User:Jtir|Jtir]] 18:58, 15 October 2006 (UTC)
 
=="Inverse square root" eh? ==
Why does [[inverse square root]] redirect here? {{unsigned|75.177.191.14}}
 
:Because of the section [[Methods of computing square roots#Iterative methods for reciprocal square roots]]. "Inverse square root" is the same as "reciprocal square root". [[User:JRSpriggs|JRSpriggs]] 10:45, 4 December 2006 (UTC)
 
== Continued fractions ==
 
[[User:JRSpriggs|JRSpriggs]], perhaps you can explain:
#What made you modify the equation for <math>a_{n+1}</math>? It is now more cumbersome and less correct, since what we know is <math>a_0</math>,not <math>\sqrt{S}</math>.
#What were you trying to accomplish with the example? Is it to demonstrate why the method works? In this case, it would be better to provide an actual proof. Is it to show how to use the method? In this case, don't you think it should be kept as simple as possible - sticking to the actual method given, which doesn't use any square roots except for ''intsqrt'' at the very beginning?
-- [[User:Meni Rosenfeld|Meni Rosenfeld]] ([[User Talk:Meni Rosenfeld|talk]]) 18:07, 5 December 2006 (UTC)
:I could not understand the method given (before my example) until I ignored it and worked out the continued fractions for several square roots from scratch. The given method is completely un-intuitive and no hint of justification or proof is given. "Simplicity" is less important than allowing people to understand what the method is and why it works. One does not normally remember exactly most of the mathematical formulas or facts which one studies. One just remembers the general approach and then figures it out again as needed. That would be impossible for the given method without an example, such as mine, or some other kind of proof. If you compare the formula which I enhanced with my example, you will see that the floor (integer part) of the expression containing the square root is actually what one uses in the example. The fact that one can replace the square root with its floor within that expression is very useful, but not immediately obvious. So the intermediate expression which I added is necessary to make it clear. [[User:JRSpriggs|JRSpriggs]] 05:04, 6 December 2006 (UTC)
Again, when you say you didn't understand, did you not understand how to use the method, or why it works? In the former case, the method was really as simple as it gets, I don't know what else to say. In the latter case, perhaps an explanation of how to use the method simply and efficiently should be separated from an explanation of why it works and how to perform the calculation without having the formulae readily available? -- [[User:Meni Rosenfeld|Meni Rosenfeld]] ([[User Talk:Meni Rosenfeld|talk]]) 07:07, 6 December 2006 (UTC)
 
== Credit for Quake3 InvSqrt() algorithm ==
Why did a someone with IP "69.181.250.222" (on December 21st, 2006 at 02:34) say that Greg Walsh created the InSqrt() algorithm from Quake3? Not only should this be cited, but from what I hear, no one knows who created it. Read that external link about the Quake3 algorithm to see what I mean. I think this either needs to be cited or removed.
[[User:65.110.239.80|65.110.239.80]]
 
:Ok, to answer my own question, after the (main) Quake3 article (that is in the external links) was published, Greg Walsh contacted the author and owned up to creating that algorithm. I added the link to the follow up story in the external links section, but I still believe this fact needs to be cited. I don't know if this link then belongs in both the references or just the external links or both, so I would like someone else to make that decision with the information I have found. [[User:65.110.239.80|65.110.239.80]]
 
::Just make sure it is there. If you put it in the wrong section, someone will move it to the right one. [[User:JRSpriggs|JRSpriggs]] 13:19, 28 January 2007 (UTC)