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

multipleOf doesn't work for smaller multiples due to precision #400

Open
deep-creek opened this issue Feb 17, 2025 · 0 comments
Open

multipleOf doesn't work for smaller multiples due to precision #400

deep-creek opened this issue Feb 17, 2025 · 0 comments

Comments

@deep-creek
Copy link
Contributor

deep-creek commented Feb 17, 2025

When defining a schema like this:
{ "$schema": "http://json-schema.org/draft-04/schema#", "id": "https://example.com/multipleOf", "type": "object", "properties": { "myNumber": { "type": "number", "multipleOf": 0.1 } }, "required": [ "myNumber" ] }

And a test like this:
func TestExampleJson_UnmarshalJSON(t *testing.T) { json :={"myNumber": 148.1}`

a := ExampleJson{}
err := a.UnmarshalJSON([]byte(json))

assert.NoError(t, err)

}
`

The decoding fails with an error. That is due to math.Abs(math.Mod(plain.MyNumber, 0.1)) returning 0.0999999999999861 instead of something lower than 1e-10.

The decoding code should not be:
if math.Abs(math.Mod(plain.MyNumber, 0.1)) > 1e-10 { return fmt.Errorf("field %s: must be a multiple of %v", "myNumber", 0.100000) }
but instead be:
{ remainder := math.Mod(plain.MyNumber, 0.1) if !(math.Abs(remainder) < 1e-10 || math.Abs(remainder-0.1) < 1e-10) { return fmt.Errorf("field %s: must be a multiple of %v", "myNumber", 0.100000) } }

@deep-creek deep-creek changed the title multipleOf doesn't work for bigger values due to precision multipleOf doesn't work for smaller multiples due to precision Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant