Math library

Asked

Viewed 562 times

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!

3 answers

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:

  1. Math.Floor, rounded to negative infinity.
  2. Math.Ceiling, which rounds to positive infinity.
  3. Math.Truncate, rounded up or down in the direction of zero.
  4. 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

  • Could you put a link to the information? + 1

  • @Maurydeveloper of course! It was stackoverflow in English Difference between Math.Floor() and Math.Truncate() filtering the answers, you can do a great study

  • 1

    Good. I found your answer quite good. And still with this link will help me a lot. Thank you.

  • "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 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?

  • 1

    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) returns 3, because 3 is the smallest integer that is greater than or equal to 2.5 - this definition also helps to understand why ceil(-2.5) results in -2 :-)

Show 1 more comment

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:

  • trunc = Returns 1, because it ignores the decimal part
  • Ceil = Returns 2, for rounded up
  • floor = Returns 1, 'cause it’s rounded down

Code:

import math

teste = 1.9

print(math.trunc(teste))
print(math.floor(teste))
print(math.ceil(teste))

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.