Skip to content

Commit 94f9513

Browse files
[Edit] C++: Math functions: sqrt() (#7270)
* [Edit] C++: Math functions: sqrt() * Update sqrt.md * Update content/cpp/concepts/math-functions/terms/sqrt/sqrt.md * Update content/cpp/concepts/math-functions/terms/sqrt/sqrt.md * Update content/cpp/concepts/math-functions/terms/sqrt/sqrt.md ---------
1 parent 4599445 commit 94f9513

File tree

1 file changed

+99
-19
lines changed
  • content/cpp/concepts/math-functions/terms/sqrt

1 file changed

+99
-19
lines changed
Lines changed: 99 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,139 @@
11
---
22
Title: 'sqrt()'
3-
Description: 'Returns square root of the argument.'
3+
Description: 'Returns the square root of a number in C++.'
44
Subjects:
5+
- 'Code Foundations'
56
- 'Computer Science'
67
Tags:
8+
- 'Algorithms'
79
- 'Functions'
8-
- 'Arithmetic'
10+
- 'Math'
911
CatalogContent:
1012
- 'learn-c-plus-plus'
1113
- 'paths/computer-science'
1214
---
1315

14-
The **`sqrt()`** function returns the square root of the argument.
16+
C++ **`sqrt()`** function returns the square root of a number. This mathematical function is part of the C++ standard library and is defined in the `<cmath>` header file.
1517

16-
## Syntax
18+
## Syntax of C++ `sqrt()`
1719

1820
```pseudo
19-
sqrt(x)
21+
sqrt(num)
2022
```
2123

22-
If the parameter `x` is negative (less than 0), then a domain error will occur.
24+
**Parameters:**
2325

24-
## Example
26+
- `num`: A floating-point number (double, float, or long double) for which the square root is to be calculated
2527

26-
The following example uses the `sqrt()` function to find the square root of `4`:
28+
**Return value:**
29+
30+
The `sqrt()` function returns the square root of the given argument as a floating-point value. Its return type matches the type of the argument (e.g., float, double, or long double).
31+
32+
> **Note:** If a negative argument is passed to `sqrt()`, a domain error occurs and the function returns `NaN` (Not a Number).
33+
34+
## Example 1: Basic Usage of `sqrt()` function
35+
36+
This example demonstrates the basic usage of the `sqrt()` function with different numerical values:
37+
38+
```cpp
39+
#include <iostream>
40+
#include <cmath>
41+
using namespace std;
42+
43+
int main() {
44+
// Calculate square root of different numbers
45+
cout << "Square root of 16: " << sqrt(16) << endl;
46+
cout << "Square root of 25: " << sqrt(25) << endl;
47+
cout << "Square root of 2: " << sqrt(2) << endl;
48+
cout << "Square root of 100.5: " << sqrt(100.5) << endl;
49+
50+
return 0;
51+
}
52+
```
53+
54+
This example results in the following output:
55+
56+
```shell
57+
Square root of 16: 4
58+
Square root of 25: 5
59+
Square root of 2: 1.41421
60+
Square root of 100.5: 10.025
61+
```
62+
63+
> **Note:** Output may vary slightly depending on platform-specific floating-point rounding.
64+
65+
## Example 2: Distance Calculation using C++ `sqrt()`
66+
67+
This example shows how `sqrt()` is used to calculate the distance between two points using the Pythagorean theorem:
2768

2869
```cpp
2970
#include <iostream>
3071
#include <cmath>
72+
using namespace std;
3173

3274
int main() {
33-
double x = 4;
34-
double result;
75+
// Calculate distance between two points (x1, y1) and (x2, y2)
76+
double x1 = 1.0, y1 = 2.0;
77+
double x2 = 4.0, y2 = 6.0;
78+
79+
// Apply distance formula: sqrt((x2-x1)^2 + (y2-y1)^2)
80+
double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
3581

36-
result = std::sqrt(x);
82+
cout << "Distance between points (" << x1 << ", " << y1 << ") and ("
83+
<< x2 << ", " << y2 << ") is: " << distance << endl;
3784

38-
std::cout << "The square root of " << x << " is " << result << "\n";
39-
// Output: The square root of 4 is 2
85+
return 0;
4086
}
4187
```
4288

43-
## Codebyte Example
89+
This example results in the following output:
4490

45-
The following example is runnable and returns the square root of `9` with the `sqrt()` function:
91+
```shell
92+
Distance between points (1, 2) and (4, 6) is: 5
93+
```
94+
95+
## Codebyte Example: Using `sqrt()` to Solve Quadratic Equation
96+
97+
This example demonstrates using `sqrt()` to solve a quadratic equation using the quadratic formula:
4698

4799
```codebyte/cpp
48100
#include <iostream>
49101
#include <cmath>
102+
using namespace std;
50103
51104
int main() {
52-
double x = 9;
53-
double result;
105+
// Solve quadratic equation ax^2 + bx + c = 0
106+
double a = 1.0, b = -5.0, c = 6.0;
107+
108+
// Calculate discriminant
109+
double discriminant = b * b - 4 * a * c;
54110
55-
result = std::sqrt(x);
111+
if (discriminant >= 0) {
112+
// Calculate roots using quadratic formula
113+
double root1 = (-b + sqrt(discriminant)) / (2 * a);
114+
double root2 = (-b - sqrt(discriminant)) / (2 * a);
56115
57-
std::cout << "The square root of " << x << " is " << result << "\n";
116+
cout << "For equation " << a << "x^2 + " << b << "x + " << c << " = 0" << endl;
117+
cout << "Root 1: " << root1 << endl;
118+
cout << "Root 2: " << root2 << endl;
119+
} else {
120+
cout << "No real roots exist for this equation." << endl;
121+
}
122+
123+
return 0;
58124
}
59125
```
126+
127+
## Frequently Asked Questions
128+
129+
### 1. What happens if I pass a negative number to `sqrt()`?
130+
131+
Passing a negative number to `sqrt()` results in a domain error and returns `NaN` (Not a Number).
132+
133+
### 2. What header file do I need to include to use `sqrt()`?
134+
135+
You need to include the `<cmath>` header file to use the `sqrt()` function.
136+
137+
### 3. What is the return type of the function `sqrt()` in C++?
138+
139+
The `sqrt()` function returns a floating-point value of the same type as the input parameter. If you pass a float, it returns a float; if you pass a double, it returns a double, and if you pass a long double, it returns a long double.

0 commit comments

Comments
 (0)