Hàm erf() trong Python Show
Nội Dung
1. Cú pháp sử dụng hàm math.erf() trong PythonHàm math.erf() trong Python được sử dụng để trả về hàm lỗi của một số. Trong toán học, hàm lỗi (cũng có tên là hàm lỗi Gauss), thường ký hiệu là erf, là một hàm phức của một biến phức được định nghĩa dưới dạng: Trong Python, để tính giá trị của một số x trong hàm lỗi, khi đó ta cần khai báo cú pháp như sau: math.erf(x) Tham số:
Lưu ý: Hàm math.erf() này chấp nhận một giá trị giữa -inf và +inf (âm vô cùng và dương vô cùng) và trả về giá trị trong khoảng -1 đến +1. 2. Ví dụ hàm math.erf() trong PythonVí dụ dưới đây, sử dụng hàm math.erf() để tính một vài giá trị trong hàm lỗi như sau: import math print (math.erf(0.67)) print (math.erf(1.34)) print (math.erf(-6)) Kết quả: 0.6566277023003051 0.9419137152583653 -1.0 Ví dụ tiếp theo, tính giá trị của một số âm và một số dương trong hàm lỗi bằng cách sử dụng hàm math.erf() như sau: import math print (math.erf(1.28)) print (math.erf(-1.28)) Kết quả: 0.9297341930135782 -0.9297341930135782 ❮ Math Methods ExamplePrint error function for different numbers: # Import math Library # Print error function for different numbers Try it Yourself » Definition and UsageThe This method accepts a value between - inf and + inf, and returns a value between - 1 to + 1. SyntaxParameter Values
Technical Details
More ExamplesExampleCalculate the mathematical error function of the same number, positive and negative: print (math.erf(1.28)) Try it Yourself » ❮ Math Methods Thư viện toán học Python cung cấp các Hàm và Hằng số / Thuộc tính khác nhau, cho phép chúng ta thực hiện các chức năng toán học. Không giống như các đối tượng toàn cục khác, Thuộc tính và Hàm bên trong đối tượng thư viện toán học Python là tĩnh. Vì vậy, chúng ta có thể truy cập các thuộc tính toán học Python dưới dạng pi và các hàm dưới dạng abs (số).
Thuộc tính đối tượng toán học PythonDanh sách các Thuộc tính hoặc Hằng số có sẵn trong mô-đun thư viện toán học Python.
Danh sách các Hàm toán học Python có sẵn trong Thư viện toán học. Vui lòng theo liên kết này để xem hướng dẫn về các hàm toán học có sẵn.
Hàm lũy thừa và lôgaritSau đây là danh sách các hàm Power và logarit có sẵn trong Thư viện toán học Python.
Hàm lượng giácSau đây là danh sách các hàm lượng giác có trong Thư viện toán học Python.
Hàm HyperbolicCác hàm lượng giác Hyperbolic trong Python cho phép chúng tôi thực hiện các hàm toán học sau trên Hyperbolic, thay vì Vòng tròn.
Các chức năng gócSau đây là danh sách các hàm Angular có trong Thư viện toán học Python.
Chức năng đặc biệtSau đây là danh sách các hàm đặc biệt có sẵn trong Thư viện toán học Python.
Ví dụ về các hàm trong toán học PythonCác ví dụ sau đây giúp bạn hiểu các hàm toán học này. Ví dụ về hằng số toán học trong PythonTrong ví dụ về hằng số Python này, chúng tôi sử dụng danh sách các hằng số có sẵn trong thư viện toán học. Chúng là pi, e, tau, inf và nan. import math as td print('pi Constant - Pi = ', td.pi) print('pi Constant - Degrees of Pi = ', td.degrees(td.pi)) print('ne Constant - e=", td.pi) print("e Constant - Degrees of e=", td.degrees(td.e)) print("ntau Constant - tau = ', td.tau) print('tau Constant - Degrees of tau = ', td.degrees(td.tau)) print('ninf Constant - Positive Infinity = ', td.inf) print('inf Constant - Negative Infinity = ', -td.inf) print('nNaN Constant - Not a Number=", td.nan) pi Constant - Pi = 3.141592653589793 pi Constant - Degrees of Pi = 180.0 e Constant - e = 3.141592653589793 e Constant - Degrees of e = 155.74607629780772 tau Constant - tau = 6.283185307179586 tau Constant - Degrees of tau = 360.0 inf Constant - Positive Infinity = inf inf Constant - Negative Infinity = -inf NaN Constant - Not a Number = nan Các hàm trong toán học Python Ví dụ 1Trong ví dụ này, chúng ta sẽ sử dụng fabs để tìm giá trị tuyệt đối và copysign để thay đổi dấu hiệu. Tiếp theo, chúng tôi sử dụng ceil và sàn để tìm các giá trị Trần và Sàn. Trong câu lệnh cuối cùng, chúng tôi đã sử dụng hàm giai thừa để tìm giai thừa của một giá trị nhất định. import math as mh x = 10.98 y = 30.22 z = -40.95 print("FABS - Absolute Value of z = ', mh.fabs(z)) print('FABS - Absolute Value of -124.897 = ', mh.fabs(-124.897)) print('ncopysign of x, z = ', mh.copysign(x, z)) print('copysign of z, x = ', mh.copysign(z, x)) print('nCEIL - Ceiling of x = ', mh.ceil(x)) print('CEIL - Ceiling of y = ', mh.ceil(y)) print('nFLOOR - Floor of x = ', mh.floor(x)) print('FLOOR - Floor of y = ', mh.floor(y)) print('nFactorial of 3 = ', mh.factorial(3)) print('Factorial of 5 = ', mh.factorial(5)) FABS - Absolute Value of z = 40.95 FABS - Absolute Value of -124.897 = 124.897 copysign of x, z = -10.98 copysign of z, x = 40.95 CEIL - Ceiling of x = 11 CEIL - Ceiling of y = 31 FLOOR - Floor of x = 10 FLOOR - Floor of y = 30 Factorial of 3 = 6 Factorial of 5 = 120 Các hàm trong toán học Python Ví dụ 2Trong ví dụ này, chúng tôi đã sử dụng fmod, frexp, fsum và gcd với các giá trị khác nhau. import math as gm print('FMOD - Mod of 2 and 3 = ', gm.fmod(2, 3)) print('FMOD - Mod of 225.55 and 5.5 = ', gm.fmod(222.55, 5.5)) print('nFREXP - Mantissa and Exponent Value of 5 = ', gm.frexp(5)) print('FREXP - Mantissa and Exponent Value of -9 = ', gm.frexp(-9)) print('nFSUM - Sum of Tuple Items=", gm.fsum((10, 20, 30, 40))) print("FSUM - Sum of List Items=", gm.fsum([5, 22, 35, 9])) print("nGCD of two 10 and 2 = ', gm.gcd(10, 2)) print('GCD of two 100 and 15 = ', gm.gcd(100, 15)) FMOD - Mod of 2 and 3 = 2.0 FMOD - Mod of 225.55 and 5.5 = 2.5500000000000114 FREXP - Mantissa and Exponent Value of 5 = (0.625, 3) FREXP - Mantissa and Exponent Value of -9 = (-0.5625, 4) FSUM - Sum of Tuple Items = 100.0 FSUM - Sum of List Items = 71.0 GCD of two 10 and 2 = 2 GCD of two 100 and 15 = 5 Các hàm trong toán học Python Ví dụ 3Trong ví dụ về Hàm toán học Python này, chúng tôi đã sử dụng round, ldexp, mode, trunc và phần còn lại. import math as at print('ROUND - Rounded Number 100.98763 = ', round(100.9876, 2)) print('ROUND - Rounded Number 125.932832 = ', round(125.932832, 3)) print('nLDEXP - LDEXP (FREXP inverse) Number of 4, 5 = ', at.ldexp(4, 5)) print('LDEXP - LDEXP (FREXP inverse) Number of -9, 2 = ', at.ldexp(-9, 2)) print('nMODF - Modf (Divided 1 to 2) Number of 100 = ', at.modf(100)) print('MODF - Modf (Divided 1 to 2) Number of 120.98 = ', at.modf(120.98)) print('nTRUNC - Truncated Number 100.98763 = ', at.trunc(100.9876)) print('ROUND - Truncated Number 125.932832 = ', at.trunc(-125.932832)) print('nRemainder of 29 and 5 = ', at.remainder(20, 5)) print('Remainder of 10 and 3 = ', at.remainder(10, 3)) ROUND - Rounded Number 100.98763 = 100.99 ROUND - Rounded Number 125.932832 = 125.933 LDEXP - LDEXP (FREXP inverse) Number of 4, 5 = 128.0 LDEXP - LDEXP (FREXP inverse) Number of -9, 2 = -36.0 MODF - Modf (Divided 1 to 2) Number of 100 = (0.0, 100.0) MODF - Modf (Divided 1 to 2) Number of 120.98 = (0.980000000000004, 120.0) TRUNC - Truncated Number 100.98763 = 100 ROUND - Truncated Number 125.932832 = -125 Remainder of 29 and 5 = 0.0 Remainder of 10 and 3 = 1.0 Ví dụ về hàm lôgaritTrong ví dụ Logarit Python này, chúng tôi sử dụng toán học exp, expm1 để nhận các giá trị exp. Tiếp theo, chúng tôi sử dụng log, log2 và log10 để lấy giá trị logarit tự nhiên, giá trị logarit cơ số 2. Và giá trị logarit cơ số 10. Sau đó, chúng tôi sử dụng pow để tìm x được nâng lên thành lũy thừa của y và sqrt để tìm căn bậc hai của một số. import math as th print('exp of 5 = ', th.exp(5)) print('exp of -3 = ', th.exp(-3)) print('nexpm1 of 8 = ', th.expm1(8)) print('expm1 of -5 = ', th.expm1(-5)) print('nLOG - logarithmic of 5 = ', th.log(5)) print('LOG - logarithmic of 100 Base 2 = ', th.log(100, 2)) print('nLOG2 - logarithmic of 120 Base 2 = ', th.log2(120)) print('nLOG10 - logarithmic of 150 Base 10 = ', th.log2(150)) print('nPOW - 2 Power 3 = ', th.pow(2, 3)) print('POW - 5 Power 4 = ', th.pow(5, 4)) print('nSQRT - Square Root of 25 = ', th.sqrt(25)) print('SQRT - Square Root of 19 = ', th.sqrt(19)) exp of 5 = 148.4131591025766 exp of -3 = 0.049787068367863944 expm1 of 8 = 2979.9579870417283 expm1 of -5 = -0.9932620530009145 LOG - logarithmic of 5 = 1.6094379124341003 LOG - logarithmic of 100 Base 2 = 6.643856189774725 LOG2 - logarithmic of 120 Base 2 = 6.906890595608519 LOG10 - logarithmic of 150 Base 10 = 7.22881869049588 POW - 2 Power 3 = 8.0 POW - 5 Power 4 = 625.0 SQRT - Square Root of 25 = 5.0 SQRT - Square Root of 19 = 4.358898943540674 Các hàm lượng giác Ví dụ 1Trong ví dụ toán lượng giác Python này, chúng ta sẽ sử dụng sin, cos và tan để tìm các giá trị sin, cosine và tiếp tuyến. Tiếp theo, chúng tôi sử dụng acos, asin, atan và atan2 để tìm các giá trị Arc cosine, Arc Sine và Arc Tangent. Trong tuyên bố cuối cùng, chúng tôi đã sử dụng giả thuyết import math as mt print('COS - Cosine of 10 = ', mt.cos(10)) print('COS - Cosine of -15 = ', mt.cos(-15)) print('nSIN - Sine of 3 = ', mt.sin(3)) print('SIN - Sine of -5 = ', mt.sin(-5)) print('nTAN - Tangent of 9 = ', mt.tan(9)) print('TAN - Tangent of -3 = ', mt.tan(-3)) print('nACOS - Arc Cosine of 1 = ', mt.acos(1)) print('ACOS - Arc Cosine of -0.78 = ', mt.acos(-0.78)) print('nASIN - Arc Sine of 1 = ', mt.asin(1)) print('ASIN - Arc Sine of -2 = ', mt.asin(-0.42)) print('nATAN - Arc Tangent of 0.72 = ', mt.atan(0.72)) print('ATAN - Arc Tangent of -2.71 = ', mt.atan(-2.71)) print('nATAN2 - Tangent of 2, 5 = ', mt.atan2(2, 5)) print('nHYPOT - Hypot Value of 2, 3 = ', mt.hypot(2, 3)) COS - Cosine of 10 = -0.8390715290764524 COS - Cosine of -15 = -0.7596879128588212 SIN - Sine of 3 = 0.1411200080598672 SIN - Sine of -5 = 0.9589242746631385 TAN - Tangent of 9 = -0.4523156594418099 TAN - Tangent of -3 = 0.1425465430742778 ACOS - Arc Cosine of 1 = 0.0 ACOS - Arc Cosine of -0.78 = 2.4654621440291318 ASIN - Arc Sine of 1 = 1.5707963267948966 ASIN - Arc Sine of -2 = -0.43344532006988595 ATAN - Arc Tangent of 0.72 = 0.6240230529767569 ATAN - Arc Tangent of -2.71 = -1.2172930308235297 ATAN2 - Tangent of 2, 5 = 0.3805063771123649 HYPOT - Hypot Value of 2, 3 = 3.6055512754639896 Các hàm lượng giác Ví dụ 2Trong ví dụ toán học Python này, chúng tôi sử dụng các hàm lượng giác Hyperbolic. Đầu tiên, chúng tôi sử dụng cosh, sinh và tanh để tìm các giá trị Hyperbolic Cosine, Sine và Tiếp tuyến. Tiếp theo, acosh, asinh và atanh để tìm giá trị cosin Hyperbolic Arc, Arc Sine và Hyperbolic Arc Tangent. import math as ma print('COSH - Hyperbolic Cosine of 2 = ', ma.cosh(2)) print('COSH - Hyperbolic Cosine of -1 = ', ma.cosh(-1)) print('nSINH - Hyperbolic Sine of 3 = ', ma.sinh(3)) print('SINH - Hyperbolic Sine of -5 = ', ma.sinh(-5)) print('nTANH - Hyperbolic Tangent of 1 = ', ma.tanh(1)) print('TANH - Hyperbolic Tangent of -3 = ', ma.tanh(-3)) print('nACOSH - Hyperbolic Arc Cosine of 10 = ', ma.acosh(10)) print('ACOSH - Hyperbolic Arc Cosine of 30.78 = ', ma.acosh(30.78)) print('nASINH - Hyperbolic Arc Sine of 15 = ', ma.asinh(15)) print('ASINH - Hyperbolic Arc Sine of -25 = ', ma.asinh(-25)) print('nATANH - Hyperbolic Arc Tangent of 0.57 = ', ma.atanh(0.57)) print('ATANH - Hyperbolic Arc Tangent of -0.71 = ', ma.atanh(-0.71)) COSH - Hyperbolic Cosine of 2 = 3.7621956910836314 COSH - Hyperbolic Cosine of -1 = 1.5430806348152437 SINH - Hyperbolic Sine of 3 = 10.017874927409903 SINH - Hyperbolic Sine of -5 = -74.20321057778875 TANH - Hyperbolic Tangent of 1 = 0.7615941559557649 TANH - Hyperbolic Tangent of -3 = -0.9950547536867305 ACOSH - Hyperbolic Arc Cosine of 10 = 2.993222846126381 ACOSH - Hyperbolic Arc Cosine of 30.78 = 4.119748326708938 ASINH - Hyperbolic Arc Sine of 15 = 3.4023066454805946 ASINH - Hyperbolic Arc Sine of -25 = -3.9124227656412556 ATANH - Hyperbolic Arc Tangent of 0.57 = 0.6475228448273728 ATANH - Hyperbolic Arc Tangent of -0.71 = -0.8871838632580928 Ví dụ về hàm góc và hàm đặc biệtTrong ví dụ Angular này, chúng tôi đã sử dụng độ và radian để chuyển đổi độ sang radian ngược lại. Tiếp theo, chúng tôi sử dụng gamma và lgamma để trả về các giá trị gamma. import math as gd print('DEGREES Function - Degrees Value of 6 = ', gd.degrees(5)) print('DEGREES Function - Degrees Value of 12 = ', gd.degrees(12)) print('nRADIANS Function - Radians Value of 350 = ', gd.radians(350)) print('nRADIANS Function - Radians Value of 680 = ', gd.radians(680)) print('nGAMMA Function - Gamma Value of 8 = ', gd.gamma(8)) print('LGAMMA Function - LGamma Value of 9 = ', gd.lgamma(9)) |