-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathPythagorean.qhelp
More file actions
33 lines (28 loc) · 1.13 KB
/
Pythagorean.qhelp
File metadata and controls
33 lines (28 loc) · 1.13 KB
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
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
Calculating the length of the hypotenuse using the standard formula <code>c = sqrt(a**2 + b**2)</code> may lead to overflow if the two other sides are both very large.
Even though <code>c</code> will not be much bigger than <code>max(a, b)</code>, either <code>a**2</code> or <code>b**2</code> (or both) will.
Thus, the calculation could overflow, even though the result is well within representable range.
</p>
</overview>
<recommendation>
<p>
Rather than <code>sqrt(a**2 + b**2)</code>, use the built-in function <code>hypot(a,b)</code> from the <code>math</code> library.
</p>
</recommendation>
<example>
<p>
The following code shows two different ways of computing the hypotenuse.
The first is a direct rewrite of the Pythagorean theorem, the second uses the built-in function.
</p>
<sample src="Pythagorean.py" />
</example>
<references>
<li>Python Language Reference: <a href="https://docs.python.org/library/math.html#math.hypot">The hypot function</a></li>
<li>Wikipedia: <a href="https://en.wikipedia.org/wiki/Hypot">Hypot</a>.</li>
</references>
</qhelp>