Tôi đang tạo một ứng dụng máy tính cho tất cả các loại thuật toán toán học. Tuy nhiên, tôi muốn xác định xem một gốc có phức tạp hay không và sau đó có một ngoại lệ cho nó. Tôi đã nghĩ ra điều này:
if x == complex[]:
print["Error 05: Complex Root"]
Tuy nhiên, không có gì được xác định hoặc in khi tôi chạy ứng dụng, biết rằng
x = 5j
if isinstance[x, complex]:
print 'X is complex'
1 là một gốc phức tạp.Đã hỏi ngày 21 tháng 3 năm 2012 lúc 23:54Mar 21, 2012 at 23:54
2
Tôi không chắc chắn 100% những gì bạn đang hỏi, nhưng nếu bạn muốn kiểm tra xem một biến có loại phức tạp bạn có thể sử dụng isInstance không. Ví dụ,
x = 5j
if isinstance[x, complex]:
print 'X is complex'
bản in
X is complex
Đã trả lời ngày 21 tháng 3 năm 2012 lúc 23:58Mar 21, 2012 at 23:58
Adam Mihalcinadam MihalcinAdam Mihalcin
13,9K4 Huy hiệu vàng33 Huy hiệu bạc51 Huy hiệu Đồng4 gold badges33 silver badges51 bronze badges
0
>>> isinstance[1j, complex]
True
Đã trả lời ngày 21 tháng 3 năm 2012 lúc 23:57Mar 21, 2012 at 23:57
0
Thử cái này:
if isinstance[x, complex]:
print["Error 05: Complex Root"]
Điều này in lỗi cho
x = 5j
if isinstance[x, complex]:
print 'X is complex'
2, x = 5j
if isinstance[x, complex]:
print 'X is complex'
3, nhưng không in bất cứ thứ gì cho x = 5j
if isinstance[x, complex]:
print 'X is complex'
4, x = 5j
if isinstance[x, complex]:
print 'X is complex'
5, v.v.Cũng nghĩ về việc ném một lỗi [
x = 5j
if isinstance[x, complex]:
print 'X is complex'
6 hoặc x = 5j
if isinstance[x, complex]:
print 'X is complex'
7] khi biến phức tạp.Đã trả lời ngày 21 tháng 3 năm 2012 lúc 23:59Mar 21, 2012 at 23:59
TadecktadeckTadeck
128K28 Huy hiệu vàng149 Huy hiệu bạc197 Huy hiệu đồng28 gold badges149 silver badges197 bronze badges
Trong Numpy v1.15, bao gồm một hàm: numpy.iscomplex [x]
trong đó
x = 5j
if isinstance[x, complex]:
print 'X is complex'
1 là số, đó là được xác định.Đã trả lời ngày 27 tháng 8 năm 2018 lúc 18:53Aug 27, 2018 at 18:53
1
Một cách để làm điều đó có thể là làm,
if type[x] == complex[]:
print["Error 05: Complex Root"]
Như những người khác đã chỉ ra, isinstance cũng hoạt động
Đã trả lời ngày 7 tháng 2 năm 2019 lúc 10:01Feb 7, 2019 at 10:01
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọcnumpy.iscomplex[] function tests element-wise whether it is a complex number or not[not infinity or not Not a Number] and returns the result as a boolean array. Syntax :
numpy.iscomplex[array]
Bàn luận
array : [array_like] Input array whose element we want to test
Hàm numpy.iscomplex [] kiểm tra phần tử khôn ngoan cho dù đó có phải là số phức hay không [không vô cực hay không không phải là số] và trả về kết quả như một mảng boolean. Cú pháp: & nbsp;
boolean array containing the result
Tham số: & nbsp;
Trả lại: & nbsp;
Mã 1: & NBSP;
Python
x = 5j
if isinstance[x, complex]:
print 'X is complex'
9 X is complex
0Các
Is Complex : [ True False] Is Complex : [ True False]
Các
Trả lại: & nbsp;
Mã 1: & NBSP;
Python
x = 5j
if isinstance[x, complex]:
print 'X is complex'
9 X is complex
0Các
X is complex
1array : [array_like] Input array whose element we want to test0
Các
Đầu ra: & nbsp;
Mã 2: & NBSP;
if isinstance[x, complex]:
print["Error 05: Complex Root"]
5if isinstance[x, complex]:
print["Error 05: Complex Root"]
6 if isinstance[x, complex]:
print["Error 05: Complex Root"]
7if isinstance[x, complex]:
print["Error 05: Complex Root"]
8if isinstance[x, complex]:
print["Error 05: Complex Root"]
9if type[x] == complex[]:
print["Error 05: Complex Root"]
0__28if type[x] == complex[]:
print["Error 05: Complex Root"]
22Các
x = 5j
if isinstance[x, complex]:
print 'X is complex'
0Các//docs.scipy.org/doc/numpy-dev/reference/generated/numpy.isfinite.html
Đầu ra: & nbsp;These codes won’t run on online IDE’s. So please, run them on your systems to explore the working.
Mã 2: & NBSP;Mohit Gupta_OMG 😀. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.