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

fix float64 precision division inaccuracy #150

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions json_schema_test_suite/multipleOf/data_22.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
19.99
1 change: 1 addition & 0 deletions json_schema_test_suite/multipleOf/schema_3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"multipleOf":0.01}
9 changes: 7 additions & 2 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"path/filepath"
)

const displayErrorMessages = false
Expand Down Expand Up @@ -212,6 +213,7 @@ func TestJsonSchemaTestSuite(t *testing.T) {
{"phase": "by number", "test": "35 is not multiple of 1.5", "schema": "multipleOf/schema_1.json", "data": "multipleOf/data_12.json", "valid": "false", "errors": "multiple_of"},
{"phase": "by small number", "test": "0.0075 is multiple of 0.0001", "schema": "multipleOf/schema_2.json", "data": "multipleOf/data_20.json", "valid": "true"},
{"phase": "by small number", "test": "0.00751 is not multiple of 0.0001", "schema": "multipleOf/schema_2.json", "data": "multipleOf/data_21.json", "valid": "false", "errors": "multiple_of"},
{"phase": "by floating point precision", "test": "19.99 is multiple of 0.01", "schema": "multipleOf/schema_3.json", "data": "multipleOf/data_22.json", "valid": "true"},
{"phase": "minItems validation", "test": "longer is valid", "schema": "minItems/schema_0.json", "data": "minItems/data_00.json", "valid": "true"},
{"phase": "minItems validation", "test": "exact length is valid", "schema": "minItems/schema_0.json", "data": "minItems/data_01.json", "valid": "true"},
{"phase": "minItems validation", "test": "too short is invalid", "schema": "minItems/schema_0.json", "data": "minItems/data_02.json", "valid": "false", "errors": "array_min_items"},
Expand Down Expand Up @@ -354,7 +356,10 @@ func TestJsonSchemaTestSuite(t *testing.T) {
panic(err.Error())
}

testwd := wd + "/json_schema_test_suite"
testwd, _ := filepath.Abs(wd + "/json_schema_test_suite")
if testwd[1] == 58 {
testwd = "/" + strings.Replace(testwd, "\\", "/", -1)
}

go func() {
err := http.ListenAndServe(":1234", http.FileServer(http.Dir(testwd+"/refRemote/remoteFiles")))
Expand All @@ -381,7 +386,7 @@ func TestJsonSchemaTestSuite(t *testing.T) {
// validate
result, err := Validate(schemaLoader, documentLoader)
if err != nil {
t.Errorf("Error (%s)\n", err.Error())
t.Fatalf("Error (%s)\n", err.Error())
}
givenValid := result.Valid()

Expand Down
2 changes: 1 addition & 1 deletion validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ func (v *subSchema) validateNumber(currentSubSchema *subSchema, value interface{
// multipleOf:
if currentSubSchema.multipleOf != nil {

if !isFloat64AnInteger(float64Value / *currentSubSchema.multipleOf) {
if !isFloat64AnInteger( (float64Value * 1000000) / (*currentSubSchema.multipleOf * 1000000) ) {
result.addError(
new(MultipleOfError),
context,
Expand Down