Integers & Floats

1. Does it take more memory to store small or large integers?
It takes more memory to store large integers due to the higher number of bits compared to small integers.
2. Explain the methods math.floor() and math.ceil().
  • math.floor() returns the largest integer less than (or equal to) the number.
  • math.ceil() returns the smallest integer greater than (or equal to) the number.
3. What will be returned from math.floor(-2.2)?
-3
4. What will be returned from math.ceil(-2.2)?
-2
5. Explain the method math.trunc().
The math.trunc returns the integer portion (loses everything after the decimal points).
6. Explain the method round().
The round() function is used to round a number to the nearest integer or to a specified number of decimal places.
7. State the main advantage of using the float class compared to decimals class.
The float class and operations tend to be faster and more memory-efficient.
8. State the main advantage of using the decimal class compared to the float class.
The decimals class offers precise decimal arithmetic without the inherent rounding errors associated with floating-point representation.
9. Explain how python rounds the floats. Explain the advantage of utilising this method.
  • Definition: Python uses banker’s rounding. In this method, if a number is exactly halfway between two possible rounded values, it rounds to the nearest even number.
  • Advantage: It helps to evenly distribute rounding errors over a large set of numbers, minimizing systematic biases.
Last updated on 19 Nov 2023