1
You guys have a question. I’m giving a study in the library Math. What’s the difference between: Ceil, floor and trunc??
The idea is to take the floating part of the number. Hug!
1
You guys have a question. I’m giving a study in the library Math. What’s the difference between: Ceil, floor and trunc??
The idea is to take the floating part of the number. Hug!
2
Although this reply be based on Microsoft . net it is well didactic, translated via google Translate:
Follow these links to get the MSDN descriptions on:
- Math.Floor, rounded to negative infinity.
- Math.Ceiling, which rounds to positive infinity.
- Math.Truncate, rounded up or down in the direction of zero.
- Math.Round, rounded to the nearest integer or to the nearest specified number of decimals.
You can specify the behavior if it is exactly equidistant between two possibilities, such as rounding, so that the final digit is even ("Round(2.5,Midpointrounding. Toeven)" if making 2) or staying further from zero (" Round(2.5,Midpointrounding.Awayfromzero)" becoming 3).The following diagram and table may help:
-3 -2 -1 0 1 2 3 +--|------+---------+----|----+--|------+----|----+-------|-+ a b c d e a=-2.7 b=-0.5 c=0.3 d=1.5 e=2.8 ====== ====== ===== ===== ===== Floor -3 -1 0 1 2 Ceiling -2 0 1 2 3 Truncate -2 0 0 1 2 Round (ToEven) -3 0 0 2 3 Round (AwayFromZero) -3 -1 0 2 3
Note that Round is much more powerful than it looks, simply because can round to a specific number of decimal places. All others round to zero decimal always. For example:
n = 3.145; > a = System.Math.Round (n, 2, MidpointRounding.ToEven); // 3.14 > b = System.Math.Round (n, 2, MidpointRounding.AwayFromZero); // 3.15
With the other functions, you have to use the trick multiply / split to get the same effect:
c = System.Math.Truncate (n * 100) / 100; // 3.14 d = System.Math.Ceiling (n * 100) / 100; // 3.15
*Tachado added for parts not applicable in Python
I advise reading this reply, more precisely on the part of Extra information
0
Fábio, all are functions that return only the whole part.
trunc = Returns only the entire part of the number, ignoring the decimals
Ceil = Returns only the whole part of the number, rounding up
floor = Returns only the whole part of the number, rounded down
In practice, taking as input the value 1.9:
Code:
import math
teste = 1.9
print(math.trunc(teste))
print(math.floor(teste))
print(math.ceil(teste))
0
In this code shows the trunc, Ceil and floor methods:
import math
print(math.floor(23.91));
print(math.ceil(23.91));
print(math.trunc(23.91));
On the console shows the result: 23,24,23
Floor: returns the smallest integer among the number "x".
Ceil: returns the smallest integer greater than or equal to "x"
Trunc: Remove decimals from value.
More information:
Browser other questions tagged python mathematics
You are not signed in. Login or sign up in order to post.
Could you put a link to the information? + 1
– Maury Developer
@Maurydeveloper of course! It was stackoverflow in English Difference between Math.Floor() and Math.Truncate() filtering the answers, you can do a great study
– Luiz Augusto
Good. I found your answer quite good. And still with this link will help me a lot. Thank you.
– Maury Developer
"round to infinity" became a strange translation, gives the impression that the result will give infinity... "Towards" would be something like "towards", which is a more complicated way of saying that it rounds to a value greater or less than the current number ("toward the positive infinity" means that the value will be greater than the current one). Anyway, I think it would be better to indicate the python documentation, instead of translating another just to scratch part of the text. And there the text is better, ex: Ceil - "Return the smallest integer Greater than or Equal to x"
– hkotsubo
@hkotsubo understand, so I mentioned via google Translate, because I always had doubts at the time of translation, I believe that it is often better to understand and use the concept in English. But a doubt, as would be the translation, aimed at programming, Ceil and floor?
– Luiz Augusto
I would use what is in the Python documentation. Ex: "Ceil - returns the smallest integer that is greater than or equal to x". Ie,
ceil(2.5)
returns3
, because 3 is the smallest integer that is greater than or equal to 2.5 - this definition also helps to understand whyceil(-2.5)
results in-2
:-)– hkotsubo