Skip to content

Commit

Permalink
Rescue some case for float type data
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexius-Huang committed Oct 27, 2017
1 parent 95638fd commit e428340
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions vm/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package vm

import (
"math"
"strconv"

"github.com/goby-lang/goby/vm/classes"
"github.com/goby-lang/goby/vm/errors"
"strconv"
)

// FloatObject represents an inexact real number using the native architecture's double-precision floating point
Expand Down Expand Up @@ -406,7 +406,7 @@ func (f *FloatObject) numericComparison(t *thread, rightObject Object, operation
// toString returns the object's value as the string format, in non
// exponential format (straight number, without exponent `E<exp>`).
func (f *FloatObject) toString() string {
return strconv.FormatFloat(f.value, 'f', -1, 64)
return strconv.FormatFloat(f.value, 'f', -1, 64) // fmt.Sprintf("%f", f.value)
}

// toJSON just delegates to toString
Expand Down
17 changes: 17 additions & 0 deletions vm/float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,20 @@ func TestFloatConversions(t *testing.T) {
v.checkSP(t, i, 1)
}
}

func TestFloatEdgeCases(t *testing.T) {
tests := []struct {
input string
expected interface{}
}{
{`(0.1 + 0.2).to_s`, "0.30000000000000004"},
}

for i, tt := range tests {
v := initTestVM()
evaluated := v.testEval(t, tt.input, getFilename())
checkExpected(t, i, evaluated, tt.expected)
v.checkCFP(t, i, 0)
v.checkSP(t, i, 1)
}
}

0 comments on commit e428340

Please sign in to comment.