Skip to content

Commit

Permalink
fix divided integer by integer is hidden decimal place and give integer
Browse files Browse the repository at this point in the history
  • Loading branch information
gravataLonga committed Jul 3, 2022
1 parent a6f749f commit 9dc2a6a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions evaluator/digit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func TestEvalDigitExpression(t *testing.T) {
`4 / 2`,
2,
},
{
`100 / 8`,
12.5,
},
{
`4 % 2`,
0,
Expand Down
2 changes: 1 addition & 1 deletion evaluator/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestHashLiterals(t *testing.T) {
{
"one": 10 - 9,
two: 1 + 1,
"thr" + "ee": 6 / 2,
"thr" + "ee": 3,
4: 4,
true: 5,
false: 6
Expand Down
16 changes: 14 additions & 2 deletions evaluator/integer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package evaluator

import "ninja/object"
import (
"math"
"ninja/object"
)

func evalIntegerInfixExpression(
operator string,
Expand All @@ -23,7 +26,16 @@ func evalIntegerInfixExpression(
case "%":
return &object.Integer{Value: leftVal % rightVal}
case "/":
return &object.Integer{Value: leftVal / rightVal}
left := float64(leftVal)
right := float64(rightVal)
total := left / right

// @todo check if we need a ELIPSON var or if there any way of doing
if math.Round(total)-total <= 0.00000000000001 {
return &object.Integer{Value: int64(total)}
}

return &object.Float{Value: left / right}
case "<":
return nativeBoolToBooleanObject(leftObject.Compare(rightObject) == -1)
case ">":
Expand Down

0 comments on commit 9dc2a6a

Please sign in to comment.