Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function/stdlib: Preserve infinity for floor/ceil #51

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cty/function/stdlib/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@ var CeilFunc = function.New(&function.Spec{
if err := gocty.FromCtyValue(args[0], &val); err != nil {
return cty.UnknownVal(cty.String), err
}
if math.IsInf(val, 0) {
return cty.NumberFloatVal(val), nil
}
return cty.NumberIntVal(int64(math.Ceil(val))), nil
},
})
Expand All @@ -394,6 +397,9 @@ var FloorFunc = function.New(&function.Spec{
if err := gocty.FromCtyValue(args[0], &val); err != nil {
return cty.UnknownVal(cty.String), err
}
if math.IsInf(val, 0) {
return cty.NumberFloatVal(val), nil
}
return cty.NumberIntVal(int64(math.Floor(val))), nil
},
})
Expand Down
21 changes: 21 additions & 0 deletions cty/function/stdlib/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stdlib

import (
"fmt"
"math"
"math/big"
"testing"

Expand Down Expand Up @@ -806,6 +807,16 @@ func TestCeil(t *testing.T) {
cty.NumberFloatVal(2),
false,
},
{
cty.NumberFloatVal(math.Inf(1)),
cty.NumberFloatVal(math.Inf(1)),
false,
},
{
cty.NumberFloatVal(math.Inf(-1)),
cty.NumberFloatVal(math.Inf(-1)),
false,
},
}

for _, test := range tests {
Expand Down Expand Up @@ -844,6 +855,16 @@ func TestFloor(t *testing.T) {
cty.NumberFloatVal(1),
false,
},
{
cty.NumberFloatVal(math.Inf(1)),
cty.NumberFloatVal(math.Inf(1)),
false,
},
{
cty.NumberFloatVal(math.Inf(-1)),
cty.NumberFloatVal(math.Inf(-1)),
false,
},
}

for _, test := range tests {
Expand Down