Python では、タスクを容易にするさまざまな関数を定義する「math」という名前のモジュールをインポートすることで、多くの数学演算を簡単に実行できます。 1. セル() :- この関数は、 数値より大きい最小の整数値 。数値がすでに整数の場合は、同じ数値が返されます。 2. フロア() :- この関数は、 数値より小さい最大の整数値 。数値がすでに整数の場合は、同じ数値が返されます。
Python# Python code to demonstrate the working of # ceil() and floor() # importing 'math' for mathematical operations import math a = 2.3 # returning the ceil of 2.3 print ('The ceil of 2.3 is : ' end='') print (math.ceil(a)) # returning the floor of 2.3 print ('The floor of 2.3 is : ' end='') print (math.floor(a))
出力:
The ceil of 2.3 is : 3 The floor of 2.3 is : 2
時間計算量: ○(1)
補助スペース: ○(1)
3.fabs() :- この関数は、 絶対値 番号の。 4.階乗() :- この関数は、 階乗 番号の。数値が整数でない場合は、エラー メッセージが表示されます。
Python
# Python code to demonstrate the working of # fabs() and factorial() # importing 'math' for mathematical operations import math a = -10 b= 5 # returning the absolute value. print ('The absolute value of -10 is : ' end='') print (math.fabs(a)) # returning the factorial of 5 print ('The factorial of 5 is : ' end='') print (math.factorial(b))
出力:
The absolute value of -10 is : 10.0 The factorial of 5 is : 120
時間計算量: O(b)
補助スペース: ○(1)
5. コピーサイン(a b) :- この関数は、数値を返します。 値は「a」ですが、符号は「b」です 。戻り値はfloat型になります。 6. gcd() :- この関数は、 2 つの数の最大公約数 その議論の中で言及されています。この関数は Python 3.5 以降で動作します。
# Python code to demonstrate the working of # copysign() and gcd() # importing 'math' for mathematical operations import math a = -10 b = 5.5 c = 15 d = 5 # returning the copysigned value. print ('The copysigned value of -10 and 5.5 is : ' end='') print (math.copysign(5.5 -10)) # returning the gcd of 15 and 5 print ('The gcd of 5 and 15 is : ' end='') print (math.gcd(515))
出力:
The copysigned value of -10 and 5.5 is : -5.5 The gcd of 5 and 15 is : 5
時間計算量: O(分(cd))
補助スペース: ○(1)
Cの配列内の文字列