Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- This program computes the square root of a number to the nearest ten billionths place.
- CSC 1253 Project #2
- @author Andrew Armbrust
- @since
- File: SquareRootFinder.cpp
- Instructor: Dr. Duncan
- */
- #include <iostream>
- #include <cmath>
- #include <iomanip>
- using namespace std;
- int main()
- {
- double mid, Epsilon,low, high, numb;
- double squareRoot;
- cout<< "Enter a number to find its square root ->";
- cin >> numb;
- Epsilon= 1E-10;
- if( numb < 0 )
- {
- cout<< " sqrt" <<"(" << numb<< ") Nan ";
- }
- else if (numb <= 1)
- {
- low = 0.0000000000000;
- high = 1.000000000000;
- mid = (low + high)/2;
- cout << setprecision(8.0) << low << '\n';
- cout<<setprecision(8)<<high<<endl;
- while (mid *mid - numb > Epsilon){
- if ((mid * mid - numb) < numb)
- low = mid;
- else
- {
- high = mid;
- mid = (low + high)/2;
- squareRoot=mid;
- }
- }
- cout<<setprecision(8)<<squareRoot;
- }
- else if (numb > 1)
- {
- low = 1;
- high = numb;
- mid = (low + high)/2;
- cout<<low;
- cout<<high;
- while (mid *mid - numb > Epsilon){
- if ((mid * mid - numb) < 0)
- low = mid;
- else
- {
- high = mid;
- mid = (low + high)/2;
- squareRoot=mid;
- }
- }
- cout<<squareRoot;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement