Answered step by step
Verified Expert Solution
Question
1 Approved Answer
A common application in computer graphics, as well as in many different subfields in science and engineering, is to compute the distance between two points
A common application in computer graphics, as well as in many different subfields in science and engineering, is to compute the distance between two points which involves pythagorean theorem and hence a square root calculation. However, as accuracy of the square root increases the time to compute it also increases. Often, a very coarse granularity approximation of the square root is good enough. Use Babylonian method (special case of Newton's method--Newton's method is a method in calculus for determining a zero of a function) and Halley approximation to find crude estimates of square root of numbers. You may need to do some research about these algorithms. (A) Babylonian Method: 1) Start with an initial estimate. A rough estimate is to use the number of digits of the number to the left of the decimal point and raise 3 to the power of that number of digits. For example, if the user enters 45346.984, the number of digits to the left of the decimal point is 5 and the initial estimate is x_0 = 3^5 = 243, 2) Iterate through the following series until a desired level of accuracy is achieved: xn = [ x_(n-1) + number / x_(n-1) ] / 2 (number is the user-entered value for sqrt). Halley Method: (a) Start with an initial estimate. A rough estimate is to use the number of digits of the number to the left of the decimal point and raise 3 to the power of that number of digits. For example, if the user enters 45346.984, the number of digits to the left of the decimal point is 5 and the initial estimate is x_0 = 3^5 = 243, (b) Iterate through the following series until a desired level of accuracy is achieved: x_n = [ x_(n-1) * [ {x_(n-1)}^2 + 3 * number ] / [ 3 * {x_(n-1)}^2 + number ] Babylonian method is simpler but converges to the real square root slower (quadratic). Halley method is more involved but converges to the square root faster (cubical). In other words, with the same number of iterations, the accuracy of Halley method will be better. Write a program that will have three functions: babylonianSquareRoot, halleySquareRoot and wholeNumberOfDigits that will find the square root of a number entered by the user. The latter function will find the number of digits of a number passed to it to the left of the decimal point. Expected output: Enter a number to find the square root : 81 Square root approximation by using the Babylonian Method is 9.00000000000000000000 Square root approximation by using Halley Method is 9.00000000000000000000 Square root by cmath function sqrt is 9.00000000000000000000 Enter a number to find the square root : 9922338833 Square root approximation by using the Babylonian Method is 99610.93731614013086073101 Square root approximation by using Halley Method is 99610.93731614015996456146 Square root by cmath function sqrt is 99610.93731614014541264623 Enter a number to find the square root : 12345678.434523565 Square root approximation by using the Babylonian Method is 3513.64176240600318124052 Square root approximation by using Halley Method is 3513.64176240600272649317 Square root by cmath function sqrt is 3513.64176240600318124052
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started