Fix spurious validate= due to float arithmetics bug#462
Open
booxter wants to merge 2 commits intoovn-kubernetes:mainfrom
Open
Fix spurious validate= due to float arithmetics bug#462booxter wants to merge 2 commits intoovn-kubernetes:mainfrom
booxter wants to merge 2 commits intoovn-kubernetes:mainfrom
Conversation
math.Pow returns float64, which - at this power level (^63) - is
imprecise. It means that when we compared:
`int(math.Pow(2, 63) - 1` and `math.MaxInt64`
in `getIntegerValidations`, we always got inequality and a spurious
validate= tag in the generated models.
This results in the following diff if `make lint` is executed:
```diff
--- a/ovsdb/serverdb/database.go
+++ b/ovsdb/serverdb/database.go
@@ -22,11 +22,11 @@ type Database struct {
UUID string `ovsdb:"_uuid"`
Cid *string `ovsdb:"cid"`
Connected bool `ovsdb:"connected"`
- Index *int `ovsdb:"index"`
+ Index *int `ovsdb:"index" validate:"omitempty,max=9223372036854775806"`
Leader bool `ovsdb:"leader"`
- Model DatabaseModel `ovsdb:"model" validate:"oneof='standalone' 'clustered' 'relay'"`
- Name string `ovsdb:"name"`
- Schema *string `ovsdb:"schema"`
+ Model DatabaseModel `ovsdb:"model" validate:"max=9223372036854775806,oneof='standalone' 'clustered' 'relay'"`
+ Name string `ovsdb:"name" validate:"max=9223372036854775806"`
+ Schema *string `ovsdb:"schema" validate:"omitempty,max=9223372036854775806"`
Sid *string `ovsdb:"sid"`
}
```
Good news is there's no requirement to use `math.Pow` here, since we can
just refer to `math.MaxInt64`, so we can replace the formula with it.
I believe the original `math.Pow` implementation was supposed to reflect
the mathematical formula from RFC7047:
```
<integer>
A JSON number with an integer value, within the range -(2**63)...+
(2**63)-1.
```
...but it didn't consider float arithmetics implications.
Signed-off-by: Ihar Hrachyshka <[email protected]>
Signed-off-by: Ihar Hrachyshka <[email protected]>
Author
package main
import (
"fmt"
"math"
)
func main() {
powValue := int(math.Pow(2, 63)) - 1
maxInt64 := math.MaxInt64
// Calculate 2^63 by multiplying 2 sixty-three times
multiplied := 1
for i := 0; i < 63; i++ {
multiplied *= 2
}
multiplied -= 1 // subtract 1 to match the others
fmt.Printf("int(math.Pow(2, 63)) - 1 = %d\n", powValue)
fmt.Printf("math.MaxInt64 = %d\n", maxInt64)
fmt.Printf("2*2*...(63 times) - 1 = %d\n", multiplied)
fmt.Println()
fmt.Printf("Pow == MaxInt64? = %v\n", powValue == maxInt64)
fmt.Printf("Multiplied == MaxInt64? = %v\n", multiplied == maxInt64)
}The exact power level at which this breaks down can be demonstrated with: package main
import (
"fmt"
"math"
)
func main() {
for n := 50; n <= 55; n++ {
powFloat := math.Pow(2, float64(n))
canDistinguish := (powFloat + 1) != powFloat
fmt.Printf("2^%d + 1 != 2^%d: %v\n", n, n, canDistinguish)
}
} |
Author
|
Nevermind, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
math.Pow returns float64, which - at this power level (^63) - is
imprecise. It means that when we compared:
int(math.Pow(2, 63) - 1andmath.MaxInt64in
getIntegerValidations, we always got inequality and a spuriousvalidate= tag in the generated models.
This results in the following diff if
make lintis executed:Good news is there's no requirement to use
math.Powhere, since we canjust refer to
math.MaxInt64, so we can replace the formula with it.I believe the original
math.Powimplementation was supposed to reflectthe mathematical formula from RFC7047:
...but it didn't consider float arithmetics implications.