129k views
3 votes
( using c programming) We want to compute the squared root of an integer n with a precision 10d , where d is a positive integer.

(a) Define a function IntPartSqrt that takes as argument a positif integer n,and computes the integer part, a, of the squared root of n given that it satisfies the following inequalities a 2 ≤ n < (a+1) 2 (1)
(b) Define a function FracPartSqrt that uses a loop that keeps the following invariant a+ d ∑ i=1 ai10−i !2 ≤ n < a+ d ∑ i=1 ai10−i +ad+110−(d+1) !2 (2) with ai ∈ [0.9] and computes the digits a1,a2,...,ad of the fractional part of the squared root of n.
(c) Use IntPartSqrt and FracPartSqrt to define the function Sqrt, that takes as argument,n a positive integer and returns the squared root of n, with a precision 10−d .
(d) Write a driver program to test the functions defined above

1 Answer

3 votes

Final answer:

To calculate a square root in C with precise decimal accuracy, you create functions for the integer part and fractional part and then a master function that combines both. The method relies on maintaining specific inequalities and requires precise loop invariants.

Step-by-step explanation:

The task is to write a C program for computing the square root of an integer with a specific precision. This involves writing three functions: IntPartSqrt, FracPartSqrt, and Sqrt. Here's a step-by-step guide to achieve this:

  • IntPartSqrt will find the integer part of the square root by checking the inequalities a^2 ≤ n < (a+1)^2.
  • FracPartSqrt will calculate the fractional part by maintaining the invariant a + ∑_{i=1}^{d} a_i 10^{-i} )^2 ≤ n < ( a + ∑_{i=1}^{d} a_i 10^{-i} + a_{d+1} 10^{-(d+1)} )^2 within a loop and determining the digits a_1, a_2, ..., a_d.
  • Sqrt uses both previous functions to compute the square root of n with precision 10^{-d}.

The driver program would test the functions with various inputs to verify accuracy. In this computation, taking square roots of exponentials depends on adjusting the exponent to ensure it's divisible by two and then taking the square root of the digit term.

User Nigel Heffernan
by
8.1k points