This repository has been archived by the owner on Dec 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcube_root_approximation.cpp
196 lines (158 loc) · 8.18 KB
/
cube_root_approximation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* file: cube_root_approximation.cpp
* type: C++ (source file)
* date: 19_JUNE_2023
* author: karbytes
* license: PUBLIC_DOMAIN
*/
/* preprocessing directives */
#include <iostream> // standard input (std::cin), standard output (std::cout)
#include <fstream> // file input, file output
#define MAXIMUM_X 1000 // constant which represents maximum absolute value of the program input value
#define E 0.00000001 // constant which represents the degree of accuracy of the square root approximation
/* function prototypes */
float absolute_value(float x);
long double difference(long double n, long double b);
long double compute_cube_root_of_real_number(float x, std::ostream & output);
/**
* Return the absolute value of a real number input, x.
*/
float absolute_value(float x)
{
if (x < 0) return -1 * x;
return x;
}
/**
* Return the absolute value of (n - (b * b * b)).
*/
long double difference(long double n, long double b)
{
if (n > (b * b * b)) return (n - (b * b * b));
return ((b * b * b) - n);
}
/**
* Compute the approximate cube root of a real number, x, using an iterative method.
*
* The cube root of x is x raised to the power of (1/3).
*
* Assume that x is a float type value and that output is an output stream object.
*
* This function returns a value whose data type is long double (which is a floating-point number).
*/
long double compute_cube_root_of_real_number(float x, std::ostream & output)
{
int i = 0;
float original_x = x;
long double A = 0.0, B = 0.0, C = 0.0, epsilon = 0.0;
x = absolute_value(x);
x = ((x > MAXIMUM_X) || (x < 1)) ? 0 : x; // If x is out of range, then set x to 0. Also, to avoid an infinite loop as a result of the absolute value of x being too small, set x to 0 if the absolute value of x is smaller than 1.
C = x;
output << "\n\nC = " << C << ". // real number to take the cube root of";
output << "\nB = " << B << ". // variable for storing the approximate cube root of x";
output << "\nA = " << A << ". // number to add to C before dividing the sum of A and C by 2 for each while loop iteration, i";
output << "\nepsilon = " << epsilon << ". // variable for storing the difference between the input value and B raised to the power of 3";
while (true)
{
output << "\n\ni := " << i << ".";
output << "\nC := " << C << ".";
output << "\nA := " << A << ".";
B = (A + C) / 2;
epsilon = difference(x, B);
output << "\nB := (A + C) / 2 = " << B << ".";
output << "\nepsilon = difference(x , B) = " << epsilon << ".";
if (epsilon <= E)
{
if (original_x < 0) return -1 * B;
return B;
}
if ((B * B * B) > x) C = B;
else A = B;
i += 1;
}
}
/* program entry point */
int main()
{
// Declare a float type variable and set its initial value to zero.
float x = 0.0;
// Declare a double type variable and set its initial value to zero.
long double S = 0.0;
// Declare a variable for storing the program user's answer of whether or not to continue inputting values.
int input_additional_values = 1;
// Declare a file output stream object.
std::ofstream file;
// Set the number of digits of floating-point numbers which are printed to the command line terminal to 100 digits.
std::cout.precision(100);
// Set the number of digits of floating-point numbers which are printed to the file output stream to 100 digits.
file.precision(100);
/**
* If cube_root_approximation_output.txt does not already exist in the same directory as cube_root_approximation.cpp,
* create a new file named cube_root_approximation_output.txt.
*
* Open the plain-text file named cube_root_approximation_output.txt
* and set that file to be overwritten with program data.
*/
file.open("cube_root_approximation_output.txt");
// Print an opening message to the command line terminal.
std::cout << "\n\n--------------------------------";
std::cout << "\nStart Of Program";
std::cout << "\n--------------------------------";
// Print an opening message to the file output stream.
file << "--------------------------------";
file << "\nStart Of Program";
file << "\n--------------------------------";
// Prompt the user to enter an x value as many times as the user chooses to.
while (input_additional_values != 0)
{
// Print "Enter a real number (represented using only base-ten digits with an optional radix and with an optional negative sign), x, which is no larger than {MAXIMUM_X}: " to the command line terminal.
std::cout << "\n\nEnter a real number (represented using only base-ten digits with an optional radix and with an optional negative sign), x, which is no larger than " << MAXIMUM_X << ": ";
// Scan the command line terminal for the most recent keyboard input value.
std::cin >> x;
// Print "The value which was entered for x is {x}." to the command line terminal.
std::cout << "\nThe value which was entered for x is " << x << ".";
// Print "The value which was entered for x is {x}." to the file output stream.
file << "\n\nThe value which was entered for x is " << x << ".";
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Print "Computing the approximate cube root of x:" to the command line terminal.
std::cout << "\n\nComputing the approximate cube root of x:";
// Print "Computing the approximate cube root of x:" to the file output stream.
file << "\n\nComputing the approximate cube root of x:";
// Compute the approximate cube root of x using the Bijection Method, print the computational steps to the command line terminal, and store the function result in S.
S = compute_cube_root_of_real_number(x, std::cout);
// Compute the approximate square root of x using Heron's Method and print the computational steps to the file output stream.
compute_cube_root_of_real_number(x, file);
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Print "S = approximate_cube_root({x}) = {S}." to the command line terminal.
std::cout << "\n\nS = approximate_cube_root(" << x << ") = " << S << ".";
// Print "S = approximate_cube_root({x}) = {S}." to the file output stream.
file << "\n\nS = approximate_cube_root(" << x << ") = " << S << ".";
// Print "(S * S * S) = " << {(S * S * S)} << ". // the approximate value of x" to the command line terminal.
std::cout << "\n\n(S * S * S) = " << (S * S * S) << ". // the approximate absolute value of x";
// Print "(S * S * S) = " << {(S * S * S)} << ". // the approximate value of x" to the command line terminal.
std::cout << "\n\n(S * S * S) = " << (S * S * S) << ". // the approximate absolute value of x";
// Ask the user whether or not to continue inputing values.
std::cout << "\n\nWould you like to continue inputing program values? (Enter 1 if YES. Enter 0 if NO): ";
// Scan the command line terminal for the most recent user input entered via keyboard to store in the variable named input_additional_values.
std::cin >> input_additional_values;
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
}
// Print a closing message to the command line terminal.
std::cout << "\nEnd Of Program";
std::cout << "\n--------------------------------\n\n";
// Print a closing message to the file output stream.
file << "\nEnd Of Program";
file << "\n--------------------------------";
// Close the file output stream.
file.close();
// Exit the program.
return 0;
}