Trunc on the other hand is simply truncating the value we provide as an argument, throwing away everything after the decimal point. It is further left along the number line. So floor(-3.1) returns -4 because -4 is the largest number less than -3.1. Return the floor of x, the largest integer less than or equal to x. So why does floor(-3.1) return -4? The answer can be found in the Python documentation for the math module. However, the behaviour of floor and trunc begins to diverge when we pass in negative numbers as arguments. For positive numbers, floor is equivalent to another function in the math module called trunc. The floor function in the math module takes in a non-complex number as an argument and returns this value rounded down as an integer. In this post I want to go into a little more detail about these rounding functions, in addition to the built-in round function, and highlight some things you should be aware of when using them. The source of this unexpected behaviour turned out to be the floor function: one of several rounding functions defined in the math module. Recently we released a post on the floor division and modulo operators, where we highlighted some potentially unexpected behaviour when using these operators with negative numbers.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2023
Categories |