Skip to content

Commit

Permalink
Return int from floor(), ceil() and trunc()
Browse files Browse the repository at this point in the history
  • Loading branch information
arshajii committed Oct 31, 2023
1 parent 584ea3b commit ad504b7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 40 deletions.
36 changes: 18 additions & 18 deletions stdlib/math.codon
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ def _check2(x: float, y: float, r: float, can_overflow: bool = False):

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.
This is the smallest integer >= x.
Expand All @@ -125,11 +125,11 @@ def ceil(x: float) -> float:
%y = call double @llvm.ceil.f64(double %x)
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.
This is the largest integer <= x.
Expand All @@ -141,7 +141,7 @@ def floor(x: float) -> float:
%y = call double @llvm.floor.f64(double %x)
ret double %y

return f(x)
return int(f(x))

def fabs(x: float) -> float:
"""
Expand Down Expand Up @@ -454,9 +454,9 @@ def log1p(x: float) -> float:
"""
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
(usually an integer).
Expand All @@ -468,7 +468,7 @@ def trunc(x: float) -> float:
%y = call double @llvm.trunc.f64(double %x)
ret double %y

return _check1(x, f(x))
return int(_check1(x, f(x)))

def erf(x: float) -> float:
"""
Expand Down Expand Up @@ -797,9 +797,9 @@ def isfinite(x: float32) -> bool:
return not (isnan(x) or isinf(x))

@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.
This is the smallest integer >= x.
Expand All @@ -811,12 +811,12 @@ def ceil(x: float32) -> float32:
%y = call float @llvm.ceil.f32(float %x)
ret float %y

return f(x)
return int(f(x))

@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.
This is the largest integer <= x.
Expand All @@ -828,7 +828,7 @@ def floor(x: float32) -> float32:
%y = call float @llvm.floor.f32(float %x)
ret float %y

return f(x)
return int(f(x))

@overload
def fabs(x: float32) -> float32:
Expand Down Expand Up @@ -1170,9 +1170,9 @@ def log1p(x: float32) -> float32:
return _C.log1pf(x)

@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
(usually an integer).
Expand All @@ -1184,7 +1184,7 @@ def trunc(x: float32) -> float32:
%y = call float @llvm.trunc.f32(float %x)
ret float %y

return f(x)
return int(f(x))

@overload
def erf(x: float32) -> float32:
Expand Down
50 changes: 28 additions & 22 deletions test/stdlib/math_test.codon
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def test_isfinite():

@test
def test_ceil():
assert isinstance(math.ceil(1.0), int)
assert math.ceil(3.3) == 4
assert math.ceil(0.5) == 1
assert math.ceil(1.0) == 1
Expand All @@ -43,6 +44,7 @@ def test_ceil():

@test
def test_floor():
assert isinstance(math.floor(1.0), int)
assert math.floor(3.3) == 3
assert math.floor(0.5) == 0
assert math.floor(1.0) == 1
Expand Down Expand Up @@ -344,6 +346,7 @@ def test_log1p():

@test
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.5) == 1
Expand Down Expand Up @@ -813,24 +816,26 @@ def test_float32_isfinite():

@test
def test_float32_ceil():
assert math.ceil(3.3f32) == 4.0f32
assert math.ceil(0.5f32) == 1.0f32
assert math.ceil(1.0f32) == 1.0f32
assert math.ceil(1.5f32) == 2.0f32
assert math.ceil(-0.5f32) == 0.0f32
assert math.ceil(-1.0f32) == -1.0f32
assert math.ceil(-1.5f32) == -1.0f32
assert isinstance(math.ceil(1.0f32), int)
assert math.ceil(3.3f32) == 4
assert math.ceil(0.5f32) == 1
assert math.ceil(1.0f32) == 1
assert math.ceil(1.5f32) == 2
assert math.ceil(-0.5f32) == 0
assert math.ceil(-1.0f32) == -1
assert math.ceil(-1.5f32) == -1


@test
def test_float32_floor():
assert math.floor(3.3f32) == 3.0f32
assert math.floor(0.5f32) == 0.0f32
assert math.floor(1.0f32) == 1.0f32
assert math.floor(1.5f32) == 1.0f32
assert math.floor(-0.5f32) == -1.0f32
assert math.floor(-1.0f32) == -1.0f32
assert math.floor(-1.5f32) == -2.0f32
assert isinstance(math.floor(1.0f32), int)
assert math.floor(3.3f32) == 3
assert math.floor(0.5f32) == 0
assert math.floor(1.0f32) == 1
assert math.floor(1.5f32) == 1
assert math.floor(-0.5f32) == -1
assert math.floor(-1.0f32) == -1
assert math.floor(-1.5f32) == -2


@test
Expand Down Expand Up @@ -1124,14 +1129,15 @@ def test_float32_log1p():

@test
def test_float32_trunc():
assert math.trunc(1.0f32) == 1.0f32
assert math.trunc(-1.0f32) == -1.0f32
assert math.trunc(1.5f32) == 1.0f32
assert math.trunc(-1.5f32) == -1.0f32
assert math.trunc(1.99999f32) == 1.0f32
assert math.trunc(-1.99999f32) == -1.0f32
assert math.trunc(0.99999f32) == 0.0f32
assert math.trunc(-100.999f32) == -100.0f32
assert isinstance(math.trunc(1.0f32), int)
assert math.trunc(1.0f32) == 1
assert math.trunc(-1.0f32) == -1
assert math.trunc(1.5f32) == 1
assert math.trunc(-1.5f32) == -1
assert math.trunc(1.99999f32) == 1
assert math.trunc(-1.99999f32) == -1
assert math.trunc(0.99999f32) == 0
assert math.trunc(-100.999f32) == -100


@test
Expand Down

0 comments on commit ad504b7

Please sign in to comment.