Python - abs vs fabs

前端 未结 4 695
无人共我
无人共我 2021-01-30 07:44

I noticed that in python there are two similar looking methods for finding the absolute value of a number:

First

abs(-5)

Second

4条回答
  •  无人共我
    2021-01-30 08:28

    abs() : Returns the absolute value as per the argument i.e. if argument is int then it returns int, if argument is float it returns float. Also it works on complex variable also i.e. abs(a+bj) also works and returns absolute value i.e.math.sqrt(((a)**2)+((b)**2)

    math.fabs() : It only works on the integer or float values. Always returns the absolute float value no matter what is the argument type(except for the complex numbers).

提交回复
热议问题