Python Forum
Finding square roots using long division.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding square roots using long division.
#6
(Feb-24-2021, 09:52 AM)Serafim Wrote: I have made an implementation that needs some explanation.
  1. I convert the input to a string as I want to simply move the decimal point out of the way. Stepwise floating point multiplication gives raise to round-off errors that severely affects the result.
  2. Two methods are not shown as they are tedious in their details but simple to implement.
    • "prepare" takes two parameters, "n" is the number to be "square rooted" an "d" is the number of desired decimals. The return value is a list with double figure integers (shortened if there is an intial zero) and the calculated number of decimals. E.g. prepare(123.456, 10) returns a=[1,23,45,60,0,0,0,0,0,0,0,0], d=10, while prepare(1.234, 0) returns a=[1,23,40] and d=2
    • "normalize" takes a string representation of a result and removes initial and trailing zeroes as well as the decimal point if there are no decimals
  3. The implementation has a flaw due to the nature of the long division method that I didn't bother to correct. There might be a round-off error in the last digit of a result with decimals so all such results ought to be rounded with omission of the last digit. The long division method is devised for integer arithmetics and is accurate when dealing with integers but as the method collects one digit at the time there is no telling if the first omitted digit in a potentially infinite number of decimals would be less than 5 or not.

1. Though simple, request the two modules: 'prepare', 'normalize' for the sake of consistency.

2. It seems the parameter 'd' to prepare is user defined. The only way to custom (input based) definition seems to convert input to string, and check for decimal. Say, '.' in string (if correct). Then, based on number of digits after decimal, can decide value of 'd'. Cannot though think of a criteria.

3. The possible flaw referred by you should be due to limited number of decimal digits? If yes, then the accuracy is affected. If so, then rounding is to be done by a seperate module?
Reply


Messages In This Thread
RE: Finding square roots using long division. - by jahuja73 - Feb-24-2021, 10:47 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Zero Division Error Leo 2 1,262 Mar-25-2022, 05:56 AM
Last Post: Leo
  Division problem Eric7Giants 1 1,708 Nov-16-2019, 05:50 AM
Last Post: ndc85430
  Count how many carpets you need to fill room floor without multiplication/division Ech0ke 1 2,331 Apr-20-2019, 07:50 PM
Last Post: ichabod801
  Zero Division Error moga2003 4 3,101 Mar-07-2019, 02:15 AM
Last Post: moga2003
  Magic square! frequency 1 2,564 Dec-17-2018, 06:35 PM
Last Post: micseydel
  Square reverse sum(overloaded) shihomiyano 6 4,117 Aug-18-2018, 06:27 AM
Last Post: micseydel
  Perfect Square program forumer444 4 8,964 Sep-01-2017, 09:32 PM
Last Post: forumer444
  Magic Square Puzzle Harnick 1 4,894 Aug-09-2017, 04:51 PM
Last Post: nilamo
  Square Root on calculator MP1234593 1 7,971 Jun-06-2017, 06:58 PM
Last Post: nilamo
  List of square roots python py7 6 6,422 Apr-08-2017, 11:26 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020