answersLogoWhite

0

/**recursive function to find square root of a double to a precision of

maxDepth = 10 decimal places

returns -1.0 when given a negative number*/

#define maxDepth 10

/*firstly find the rational part*/

double getSqrt(double numIn){

/*cant take the square root of a negitive,

and a square root should not be negative

so return -1*/

int candidate = 0;

if (numIn <0)

return -1.0;

/*try every integer until you get one whose square is higher than

the number required, and this clearly one more than the

integer part of your square root*/

do{

if (candidate *candidate *1.0 numIn)

return testVal;

if (testVal * testVal >numIn)

/*most square roots are irrational, and theirfore a maximum number of recursions

must be set, otherwise infinite recursion will occur*/

if (myDepth <maxDepth)

return getIrrational(numIn, testVal-1/10^myDepth, myDepth +1);

else

return testVal-1/10^myDepth;

}

//this can probably be improved on in terms of conciseness, but the logic is the only //way to find a sqrt without going into calculous

User Avatar

Wiki User

16y ago

What else can I help you with?

Related Questions

Write a program to calculate the area of a cylinder using a function?

give an example of calculation of mathematics


Write a C program to calculate the sum of squares of numbers from 1 to N?

#include &lt;iostream&gt; using namespace std; int main() { int i,sum; // variables sum = 0; // initialize sum /* recursive addition of squares */ for (i = 1; i &lt;= 30; i++) sum = sum + (i * i); cout &lt;&lt; sum &lt;&lt;" is the sum of the first 30 squares." &lt;&lt; endl; return 0; }


What is the use of function in c?

using function we can call the function in the program any where. by using functions we can reduce the code redundancy


How do you write a c program to calculate income tax using a function?

float income_tax (float income, float tax_percent) { return income * tax_percent / 100; }


How to calculate cubic function using point of inflection and extrema?

cubic function cubic function


How can I calculate the distance between two points in C?

To calculate the distance between two points in C, you can use the distance formula, which is the square root of the sum of the squares of the differences in the x and y coordinates of the two points. This can be implemented using the sqrt() function from the cmath library.


How do you calculate sine value without using math function?

Using its Taylor-series.


Write a program that read phrase and print the number of lower-case letter in it using function of counting?

write a program that reads a phrase and prints the number of lowercase latters in it using a function for counting? in C program


Write a c program for matrix addition using function?

#include&lt;


How can I use the scipy differentiation function to calculate the derivative of a mathematical function?

To calculate the derivative of a mathematical function using the scipy differentiation function, you can use the scipy.misc.derivative function. This function takes the mathematical function, the point at which you want to calculate the derivative, and the order of the derivative as input parameters. It then returns the numerical value of the derivative at that point.


How can you use Excel to calculate the remainder of a division question?

By using the =MOD function.


How do I calculate compound average growth rate using excel?

use the rate function