Coder Social home page Coder Social logo

Comments (2)

nick-dd avatar nick-dd commented on June 7, 2024

I understand that this is due floating-point representation discreteness.
val := new(big.Rat).SetFloat64(0.01) gives huge numbers in a/b (namely, 5764607523034235/576460752303423488), and not exactly 1/100, as it has for multipleOf value.

It seems, the difference is that multipleOf is initiated with SetString(), while val - with SetFloat64().

As a workaround I'd use a way to bypass response validation if it were possible.

from ogen.

nick-dd avatar nick-dd commented on June 7, 2024

This is an example of the validation code it generated for the total field:

	if err := func() error {
		if value, ok := s.Total.Get(); ok {
			if err := func() error {
				if err := (validate.Float{
					MinSet:        false,
					Min:           0,
					MaxSet:        false,
					Max:           0,
					MinExclusive:  false,
					MaxExclusive:  false,
					MultipleOfSet: true,
					MultipleOf:    ratMap["1/100"],
				}).Validate(float64(value)); err != nil {
					return errors.Wrap(err, "float")
				}
				return nil
			}(); err != nil {
				return err
			}
		}
		return nil
	}(); err != nil {
		failures = append(failures, validate.FieldError{
			Name:  "total",
			Error: err,
		})
	}

And this is what in the ratMap:

var ratMap = map[string]*big.Rat{
	"1/10": func() *big.Rat {
		r, ok := new(big.Rat).SetString("1/10")
		if !ok {
			panic(fmt.Sprintf("rat %q: can't parse", "1/10"))
		}
		return r
	}(),
	"1/100": func() *big.Rat {
		r, ok := new(big.Rat).SetString("1/100")
		if !ok {
			panic(fmt.Sprintf("rat %q: can't parse", "1/100"))
		}
		return r
	}(),
}

from ogen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.