Hàm hypot[] trong Python trả về sqrt[x*x + y*y]. Cú pháp của hàm hypot[] trong Python: Ghi chú: Hàm này không có thể truy cập trực tiếp, vì thế chúng ta cần import math module và sau đó chúng ta cần gọi hàm này bởi sử dụng đối tượng math. Các tham số: x: Đây phải là một giá trị số. y: Đây phải là một giá trị số. Ví dụ sau minh họa cách sử dụng của hypot[] trong Python.Cú pháp
import math
print ["hypot[2, 2] : ", math.hypot[2, 2]]
print ["hypot[-3, 2] : ", math.hypot[-3, 2]]
print ["hypot[0, 3] : ", math.hypot[0, 3]]
Chạy chương trình Python trên sẽ cho kết quả:
hypot[2, 2] : 2.8284271247461903 hypot[-3, 2] : 3.6055512754639896 hypot[0, 3] : 3.0
Hàm hypot[] trong Python
Nội Dung
- 1. Cú pháp sử dụng hàm math.hypot[] trong Python
- 2. Ví dụ hàm math.hypot[] trong Python
1. Cú pháp sử dụng hàm math.hypot[] trong Python
Hàm math.hypot[] trong Python được sử dụng để trả về khoảng cách Euclide tính từ gốc tọa độ đến tọa độ của điểm đã cho. Trong toán học, chuẩn Euclid chính là khoảng cách từ điểm gốc đến tọa độ đã cho.
Đối với trường hợp trong không gian n chiều, các tọa độ sẽ là [x1, x2, x3, …, xn]. Vì vậy độ dài Euclide từ gốc đến điểm đó sẽ được tính bằng sqrt[x1 * x1 + x2 * x2 + x3 * x3 …. xn * xn].
Cú pháp của hàm math.hypot[] như sau:
math.hypot[x1, x2, x3, ..., xn]
Tham số:
- x1, x2, x3, …, xn là các điểm tọa độ trong không gian n chiều.
Giá trị trả về: | Một giá trị kiểu float, là khoảng cách từ gốc tọa độ đến điểm đã cho. |
Phiên bản Python: | 3.8 |
Lưu ý: Nếu các giá trị của điểm tọa độ x1, x2, x3, …, xn không thuộc kiểu số, khi đó sẽ không thể thực hiện chuẩn Euclide.
2. Ví dụ hàm math.hypot[] trong Python
Ví dụ dưới đây, sử dụng hàm math.hypot[] để tính toán khoảng cách Euclide từ gốc tọa độ đến các điểm đã cho trong không gian 3 chiều như sau:
import math print[math.hypot[10, 2, 4, 13]] print[math.hypot[4, 7, 8]] print[math.hypot[12, 14]]
Kết quả:
17.0 11.357816691600547 18.439088914585774
Ví dụ tiếp theo, truyền vào hàm các giá trị không phải kiểu số khi đó hàm sẽ không thể thực hiện tính khoảng cách Euclide và chương trình gây ra lỗi như sau:
import math print[math.hypot["a", "b", 4, 13]] print[math.hypot[4, 7, "c"]] print[math.hypot[12, NaN]]
Kết quả:
Traceback [most recent call last]: File "c:\Users\Administrator\test.py", line 3, in print[math.hypot["a", "b", 4, 13]] TypeError: must be real number, not str
Write a Python program to calculate the hypotenuse of a right angled triangle. From Wikipedia, Pictorial Presentation: Sample Solution-1: Python Code : Sample Output: The following tool visualize what the computer is doing step-by-step as it executes the said program: Python Basic: Exercise-60 with Solution
A right triangle or right-angled triangle, or more formally an orthogonal triangle, is a triangle in which one angle is a right angle. The relation between the sides and angles of a right triangle is the basis for
trigonometry. The side opposite the right angle is called the hypotenuse. If the lengths of all three sides of a right triangle are integers, the triangle is said to be a Pythagorean triangle and its side lengths are collectively known as a Pythagorean triple.Visualize Python code execution:
Sample Solution-2:
Python Code:
def test[x, y]: h = [x**2 + y**2]**0.5 return h print[test[3,4]] print[test[3.5,4.4]]Sample Output:
5.0 5.622277118748239Flowchart:
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code [and comments] through Disqus.
Previous: Write a Python program to convert height [in feet and inches] to centimeters.
Next: Write a Python program to convert the distance [in feet] to inches, yards, and miles.
Python: Tips of the Day
Iterating over a dictionary:
m = {'a': 1, 'b': 2, 'c': 3, 'd': 4} for key, value in m.items[]: print['{0}: {1}'.format[key, value]]