Rễ hình Python: Thực và phức tạp
Một cách nhanh chóng để có được căn bậc hai của giá trị là sử dụng toán tử số mũ
value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
3 với 0,5 làm tham số thứ hai. Xem bên dưới để biết ví dụ nhanh về điều này:exponentiation operator value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
3 with 0.5 as the second parameter. See below for a quick example of this:Sử dụng
value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
3 không phải là cách duy nhất, hoặc cách chính xác nhất, để tính toán căn bậc hai trong Python. Chúng tôi sẽ xem xét cách bạn có thể tính toán căn bậc hai của giá trị bằng cách sử dụng số mũ, cùng với các hàm value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
5 và value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
6 value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
7 và cũng xem xét các lợi thế của từng tùy chọn.Sử dụng toán tử số liệu
value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
3 là một cách dễ dàng để có được căn bậc hai của một số. Nhà điều hành này nâng cao hạng đầu tiên cho sức mạnh của toán hạng thứ hai.Để có được căn bậc hai, bạn cần sử dụng 0,5 làm toán hạng thứ hai, như được hiển thị trong phần giới thiệu.
Đoạn trích sau đây cho thấy một ví dụ khác về cách chúng ta có thể sử dụng
value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
9 để tính toán căn bậc hai cho một phạm vi giá trị:values = [16, 25, 36, 49, 64]
for x in values:
x_sqrt = x**0.5
print[f'value: {x} square root {x_sqrt}']
Out:
value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
Chúng ta cũng có thể sử dụng toán tử số mũ với các giá trị âm:
Trong trường hợp này, Python nhận thấy hoạt động là
[1.2246467991473532e-16+2j]
0, cho chúng ta -2. Tuy nhiên, [1.2246467991473532e-16+2j]
1 cung cấp cho chúng tôi:Out:
[1.2246467991473532e-16+2j]
Vì căn bậc hai của số âm đưa ra câu trả lời phức tạp, chúng tôi khuyên bạn nên sử dụng
[1.2246467991473532e-16+2j]
2, như được hiển thị ở cuối phần tiếp theo.Lưu ý rằng toán hạng thứ hai của
value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
3 có thể là bất kỳ số thực nào. Do đó, bạn chỉ sử dụng 0,5 khi tìm kiếm căn bậc hai. Xem bên dưới để biết một số ví dụ khác về các giá trị mà bạn có thể tính toán với value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
3:x = 4
print[f' x squared: {x**2}']
print[f' x cubed: {x**3}']
print[f' x to the power of -1 [x/x squared]: {x**-1}']
print[f' x to the power of -2 [x/x cubed]: {x**-2}']
Out:
x squared: 16
x cubed: 64
x to the power of -1 [x/x squared]: 0.25
x to the power of -2 [x/x cubed]: 0.0625
Một cách khác để tính toán căn bậc hai của giá trị là bằng cách sử dụng hàm
[1.2246467991473532e-16+2j]
5 từ thư viện value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
5. Ví dụ dưới đây cho thấy cách chúng ta có thể áp dụng hàm [1.2246467991473532e-16+2j]
5 vào ví dụ được sử dụng trong phần value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
9:import math
values = [16, 25, 36, 49, 64]
for x in values:
x_sqrt = math.sqrt[x]
print[f'value: {x} square root {x_sqrt}']
Out:
value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
Như kết quả cho thấy, sử dụng
[1.2246467991473532e-16+2j]
5 cho kết quả tương tự như value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
9. Ưu điểm của việc sử dụng value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
9 khi bắt đầu bài viết này là value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
3 không yêu cầu nhập như [1.2246467991473532e-16+2j]
5.Mặt khác, nhiều người lập luận rằng
[1.2246467991473532e-16+2j]
5 thường thực hiện nhanh hơn; Xem trang Overflow Stack này để biết sự cố tốc độ.Sử dụng giá trị âm với
[1.2246467991473532e-16+2j]
5 sẽ ném lỗi x = 4
print[f' x squared: {x**2}']
print[f' x cubed: {x**3}']
print[f' x to the power of -1 [x/x squared]: {x**-1}']
print[f' x to the power of -2 [x/x cubed]: {x**-2}']
6, như được hiển thị bên dưới:Out:
---------------------------------------------------------------------------
ValueError Traceback [most recent call last]
in
----> 1 math.sqrt[-4]
ValueError: math domain error
Tùy thuộc vào cách bạn muốn xử lý rễ vuông của các tiêu cực, việc xử lý
x = 4
print[f' x squared: {x**2}']
print[f' x cubed: {x**3}']
print[f' x to the power of -1 [x/x squared]: {x**-1}']
print[f' x to the power of -2 [x/x cubed]: {x**-2}']
7 có thể thích hợp hơn. Ngoài ra, chúng ta có thể tránh điều này bằng cách sử dụng [1.2246467991473532e-16+2j]
2, như chúng ta sẽ thấy trong phần tiếp theo.Bạn cũng có thể tính toán căn bậc hai của các số âm và phức tạp bằng thư viện
x = 4
print[f' x squared: {x**2}']
print[f' x cubed: {x**3}']
print[f' x to the power of -1 [x/x squared]: {x**-1}']
print[f' x to the power of -2 [x/x cubed]: {x**-2}']
9. Xem bên dưới để biết ví dụ về điều này:import cmath
c = [12 + 16j]
cmath.sqrt[c]
Một cách tuyệt vời để tránh nhận
x = 4
print[f' x squared: {x**2}']
print[f' x cubed: {x**3}']
print[f' x to the power of -1 [x/x squared]: {x**-1}']
print[f' x to the power of -2 [x/x cubed]: {x**-2}']
6 là sử dụng [1.2246467991473532e-16+2j]
2 để xử lý các ngoại lệ.Ví dụ: chúng ta có thể làm điều này bằng cách sử dụng tập lệnh sau:
import math
import cmath
values = [16, -25, 36, -49, 64]
for x in values:
try:
x_sqrt = math.sqrt[x]
except ValueError:
x_sqrt = cmath.sqrt[x]
print[f'value: {x} square root {x_sqrt}']
Out:
value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
0Nếu bạn đang làm việc với các mảng Numpy, bạn cũng có tùy chọn sử dụng
x squared: 16
x cubed: 64
x to the power of -1 [x/x squared]: 0.25
x to the power of -2 [x/x cubed]: 0.0625
2 [x squared: 16
x cubed: 64
x to the power of -1 [x/x squared]: 0.25
x to the power of -2 [x/x cubed]: 0.0625
3 trong ví dụ].Sử dụng chức năng này với một mảng sẽ tạo ra một mảng mới chứa tất cả các gốc vuông của mảng gốc. Ví dụ dưới đây cho thấy cách chúng ta có thể tạo một mảng bằng danh sách từ các ví dụ trước và sau đó áp dụng
x squared: 16
x cubed: 64
x to the power of -1 [x/x squared]: 0.25
x to the power of -2 [x/x cubed]: 0.0625
3:value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
1Out:
value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
2Điều đáng chú ý là bạn cũng có thể sử dụng
x squared: 16
x cubed: 64
x to the power of -1 [x/x squared]: 0.25
x to the power of -2 [x/x cubed]: 0.0625
3 trên các giá trị đơn lẻ, nhưng chúng tôi không đề xuất điều này vì Numpy được tối ưu hóa để làm việc với các mảng.Thật đơn giản để tính toán căn bậc hai của một giá trị trong Python bằng toán tử số liệu
value: 16 square root 4.0
value: 25 square root 5.0
value: 36 square root 6.0
value: 49 square root 7.0
value: 64 square root 8.0
3 hoặc [1.2246467991473532e-16+2j]
5. Điều đáng nói là [1.2246467991473532e-16+2j]
5 thường là nhanh hơn của cả hai và bằng cách sử dụng [1.2246467991473532e-16+2j]
2, bạn có thể nhận được căn bậc hai của một số phức. Khi làm việc với các mảng, bạn cũng có tùy chọn sử dụng hàm x squared: 16
x cubed: 64
x to the power of -1 [x/x squared]: 0.25
x to the power of -2 [x/x cubed]: 0.0625
2 để tính căn bậc hai của mọi giá trị.