Return int from floor(), ceil() and trunc()

pull/498/head
A. R. Shajii 2023-10-31 10:18:54 -04:00
parent 584ea3b493
commit ad504b78b0
2 changed files with 46 additions and 40 deletions

View File

@ -111,9 +111,9 @@ def _check2(x: float, y: float, r: float, can_overflow: bool = False):
return r return r
def ceil(x: float) -> float: def ceil(x: float) -> int:
""" """
ceil(float) -> float ceil(float) -> int
Return the ceiling of x as an Integral. Return the ceiling of x as an Integral.
This is the smallest integer >= x. This is the smallest integer >= x.
@ -125,11 +125,11 @@ def ceil(x: float) -> float:
%y = call double @llvm.ceil.f64(double %x) %y = call double @llvm.ceil.f64(double %x)
ret double %y ret double %y
return f(x) return int(f(x))
def floor(x: float) -> float: def floor(x: float) -> int:
""" """
floor(float) -> float floor(float) -> int
Return the floor of x as an Integral. Return the floor of x as an Integral.
This is the largest integer <= x. This is the largest integer <= x.
@ -141,7 +141,7 @@ def floor(x: float) -> float:
%y = call double @llvm.floor.f64(double %x) %y = call double @llvm.floor.f64(double %x)
ret double %y ret double %y
return f(x) return int(f(x))
def fabs(x: float) -> float: def fabs(x: float) -> float:
""" """
@ -454,9 +454,9 @@ def log1p(x: float) -> float:
""" """
return _check1(x, _C.log1p(x)) return _check1(x, _C.log1p(x))
def trunc(x: float) -> float: def trunc(x: float) -> int:
""" """
trunc(float) -> float trunc(float) -> int
Return the Real value x truncated to an Integral Return the Real value x truncated to an Integral
(usually an integer). (usually an integer).
@ -468,7 +468,7 @@ def trunc(x: float) -> float:
%y = call double @llvm.trunc.f64(double %x) %y = call double @llvm.trunc.f64(double %x)
ret double %y ret double %y
return _check1(x, f(x)) return int(_check1(x, f(x)))
def erf(x: float) -> float: def erf(x: float) -> float:
""" """
@ -797,9 +797,9 @@ def isfinite(x: float32) -> bool:
return not (isnan(x) or isinf(x)) return not (isnan(x) or isinf(x))
@overload @overload
def ceil(x: float32) -> float32: def ceil(x: float32) -> int:
""" """
ceil(float32) -> float32 ceil(float32) -> int
Return the ceiling of x as an Integral. Return the ceiling of x as an Integral.
This is the smallest integer >= x. This is the smallest integer >= x.
@ -811,12 +811,12 @@ def ceil(x: float32) -> float32:
%y = call float @llvm.ceil.f32(float %x) %y = call float @llvm.ceil.f32(float %x)
ret float %y ret float %y
return f(x) return int(f(x))
@overload @overload
def floor(x: float32) -> float32: def floor(x: float32) -> int:
""" """
floor(float32) -> float32 floor(float32) -> int
Return the floor of x as an Integral. Return the floor of x as an Integral.
This is the largest integer <= x. This is the largest integer <= x.
@ -828,7 +828,7 @@ def floor(x: float32) -> float32:
%y = call float @llvm.floor.f32(float %x) %y = call float @llvm.floor.f32(float %x)
ret float %y ret float %y
return f(x) return int(f(x))
@overload @overload
def fabs(x: float32) -> float32: def fabs(x: float32) -> float32:
@ -1170,9 +1170,9 @@ def log1p(x: float32) -> float32:
return _C.log1pf(x) return _C.log1pf(x)
@overload @overload
def trunc(x: float32) -> float32: def trunc(x: float32) -> int:
""" """
trunc(float32) -> float32 trunc(float32) -> int
Return the Real value x truncated to an Integral Return the Real value x truncated to an Integral
(usually an integer). (usually an integer).
@ -1184,7 +1184,7 @@ def trunc(x: float32) -> float32:
%y = call float @llvm.trunc.f32(float %x) %y = call float @llvm.trunc.f32(float %x)
ret float %y ret float %y
return f(x) return int(f(x))
@overload @overload
def erf(x: float32) -> float32: def erf(x: float32) -> float32:

View File

@ -32,6 +32,7 @@ def test_isfinite():
@test @test
def test_ceil(): def test_ceil():
assert isinstance(math.ceil(1.0), int)
assert math.ceil(3.3) == 4 assert math.ceil(3.3) == 4
assert math.ceil(0.5) == 1 assert math.ceil(0.5) == 1
assert math.ceil(1.0) == 1 assert math.ceil(1.0) == 1
@ -43,6 +44,7 @@ def test_ceil():
@test @test
def test_floor(): def test_floor():
assert isinstance(math.floor(1.0), int)
assert math.floor(3.3) == 3 assert math.floor(3.3) == 3
assert math.floor(0.5) == 0 assert math.floor(0.5) == 0
assert math.floor(1.0) == 1 assert math.floor(1.0) == 1
@ -344,6 +346,7 @@ def test_log1p():
@test @test
def test_trunc(): def test_trunc():
assert isinstance(math.trunc(1.0), int)
assert math.trunc(1.0) == 1 assert math.trunc(1.0) == 1
assert math.trunc(-1.0) == -1 assert math.trunc(-1.0) == -1
assert math.trunc(1.5) == 1 assert math.trunc(1.5) == 1
@ -813,24 +816,26 @@ def test_float32_isfinite():
@test @test
def test_float32_ceil(): def test_float32_ceil():
assert math.ceil(3.3f32) == 4.0f32 assert isinstance(math.ceil(1.0f32), int)
assert math.ceil(0.5f32) == 1.0f32 assert math.ceil(3.3f32) == 4
assert math.ceil(1.0f32) == 1.0f32 assert math.ceil(0.5f32) == 1
assert math.ceil(1.5f32) == 2.0f32 assert math.ceil(1.0f32) == 1
assert math.ceil(-0.5f32) == 0.0f32 assert math.ceil(1.5f32) == 2
assert math.ceil(-1.0f32) == -1.0f32 assert math.ceil(-0.5f32) == 0
assert math.ceil(-1.5f32) == -1.0f32 assert math.ceil(-1.0f32) == -1
assert math.ceil(-1.5f32) == -1
@test @test
def test_float32_floor(): def test_float32_floor():
assert math.floor(3.3f32) == 3.0f32 assert isinstance(math.floor(1.0f32), int)
assert math.floor(0.5f32) == 0.0f32 assert math.floor(3.3f32) == 3
assert math.floor(1.0f32) == 1.0f32 assert math.floor(0.5f32) == 0
assert math.floor(1.5f32) == 1.0f32 assert math.floor(1.0f32) == 1
assert math.floor(-0.5f32) == -1.0f32 assert math.floor(1.5f32) == 1
assert math.floor(-1.0f32) == -1.0f32 assert math.floor(-0.5f32) == -1
assert math.floor(-1.5f32) == -2.0f32 assert math.floor(-1.0f32) == -1
assert math.floor(-1.5f32) == -2
@test @test
@ -1124,14 +1129,15 @@ def test_float32_log1p():
@test @test
def test_float32_trunc(): def test_float32_trunc():
assert math.trunc(1.0f32) == 1.0f32 assert isinstance(math.trunc(1.0f32), int)
assert math.trunc(-1.0f32) == -1.0f32 assert math.trunc(1.0f32) == 1
assert math.trunc(1.5f32) == 1.0f32 assert math.trunc(-1.0f32) == -1
assert math.trunc(-1.5f32) == -1.0f32 assert math.trunc(1.5f32) == 1
assert math.trunc(1.99999f32) == 1.0f32 assert math.trunc(-1.5f32) == -1
assert math.trunc(-1.99999f32) == -1.0f32 assert math.trunc(1.99999f32) == 1
assert math.trunc(0.99999f32) == 0.0f32 assert math.trunc(-1.99999f32) == -1
assert math.trunc(-100.999f32) == -100.0f32 assert math.trunc(0.99999f32) == 0
assert math.trunc(-100.999f32) == -100
@test @test