Can someone please explain this?


#1

3/15

def is_int(x):
  absolute = abs(x)
  rounded = round(absolute)
  return absolute - rounded == 0

#2

What is an absolute value?

If x is less than zero, using the abs() function will make it a positive value.

What is rounding?

If x is some value containing a decimal fraction, then rounding will,

a. round down to the nearest integer if the decimal fraction is less than 0.5; and,
b. round up to the nearest integer if the decimal fraction is 0.5 or greater.

>>> round(1.5)
2
>>> round(1.49)
1
>>> 

As we can see, the above function could give incorrect results depending on the size of the decimal fraction. If the rounding is downward, the result will be correct, and if the rounding is upward, the result will be wrong.

Whomever wrote this code should go back and rethink the approach.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.