Математичні функції в Python | Набір 1 (числові функції)

У Python можна з легкістю виконати ряд математичних операцій, імпортувавши модуль під назвою «math», який визначає різні функції, що полегшує наші завдання. 1. ceil() :- Ця функція повертає найменше ціле значення більше числа . Якщо число вже є цілим, повертається те саме число. 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
   # 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  (  5    15  ))   

Вихід:

The copysigned value of -10 and 5.5 is : -5.5 The gcd of 5 and 15 is : 5 

Часова складність: O(мін(кд))

Допоміжний простір: О(1)