Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it better to use trunc() instead of int() to convert floating type numbers to integers?

trunc and int functions return the same output for every float type inputs that I have tried.

They differ in the way that int can also be used to convert numerical strings to integers.

So I have a two-fold question:

  1. I would like to know if, apart from strings, is there any input for which trunc and int give different outputs?

  2. If not, when is it better to just use trunc to convert floating type numbers to integers?

like image 223
whateven Avatar asked Sep 07 '18 13:09

whateven


People also ask

What happens when you convert a float to an int Python?

Convert a float to an int always results in a data loss. The trunc() function returns the integer part of a number. The floor() function returns the largest integer less than or equal to a number.

How do you convert a float to an int in Python?

Python also has a built-in function to convert floats to integers: int() . In this case, 390.8 will be converted to 390 . When converting floats to integers with the int() function, Python cuts off the decimal and remaining numbers of a float to create an integer.

Which method is used to convert integer data type to float?

Integer and Float Conversions Integers and floats are data types that deal with numbers. To convert the integer to float, use the float() function in Python.

How do you convert a float to an integer?

A float value can be converted to an int value no larger than the input by using the math. floor() function, whereas it can also be converted to an int value which is the smallest integer greater than the input using math. ceil() function.


1 Answers

int and math.trunc have a somewhat similar relationship as str and repr. int delegates to a type's __int__ method, and falls back to the __trunc__ method if __int__ is not found. math.trunc delegates to the type's __trunc__ method directly and has no fallback. Unlike __str__ and __repr__, which are always defined for object, both int and math.trunc can raise errors out of the box.

For all the built-in types that I am aware of, both __int__ and __trunc__ are defined sensibly where appropriate. However, you can define your own set of test classes to see what errors you get:

class A:
    def __int__(self):
        return 1

class B:
    def __trunc__(self):
        return 1

class C(): pass

math.trunc(A()) and math.trunc(C()) will both raise TypeError: type X doesn't define __trunc__ method. int(C()) will raise TypeError: int() argument must be a string, a bytes-like object or a number, not 'C'. However, int(A()), int(B()) and math.trunc(B()) will all succeed.

In the end the decision as to which method to use is one of connotation. trunc is inherently a math operation similar to floor, while int is a general purpose conversion, and succeeds in more cases.

And don't forget about operator.index and the __index__ method.

like image 169
Mad Physicist Avatar answered Sep 18 '22 16:09

Mad Physicist