Ad

Round a given float number n according to the "Round half up" algorithm with a given number of decimal places m.

More about rounding here.

from decimal import Decimal, ROUND_HALF_UP

def roundHalfUp( n, m ):
    return float( Decimal( str( n ) ).quantize( Decimal( "1." + "0" * m ), rounding = ROUND_HALF_UP ) )
Code
Diff
  • def real_cube_root( n ):
        return (1 if n >= 0 else -1) * (abs( n ) ** (1 / 3))
    
    • def real_cube_root(n):
    • if n >= 0: return n ** (1/3)
    • else: return -abs(n ** (1/3))
    • def real_cube_root( n ):
    • return (1 if n >= 0 else -1) * (abs( n ) ** (1 / 3))