Skip to content

Commit

Permalink
Debug flexint_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
arturogonzalezm committed Jul 29, 2024
1 parent f07d4c3 commit a1b2340
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions pkg/flexint/flexint.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ func (i *Int64) UnmarshalJSON(data []byte) error {
// Try to unmarshal as a float64 and then convert
var floatVal float64
if err := json.Unmarshal(data, &floatVal); err == nil {
roundedVal := roundHalfToEven(floatVal)
if roundedVal > float64(math.MaxInt64) {
if floatVal > float64(math.MaxInt64) {
*i = Int64(math.MaxInt64)
} else if roundedVal < float64(math.MinInt64) {
} else if floatVal < float64(math.MinInt64) {
*i = Int64(math.MinInt64)
} else {
*i = Int64(roundedVal)
*i = Int64(roundHalfToEven(floatVal))
}
return nil
}
Expand All @@ -43,13 +42,12 @@ func (i *Int64) UnmarshalJSON(data []byte) error {
*i = Int64(intVal)
return nil
} else if floatVal, err := strconv.ParseFloat(strVal, 64); err == nil {
roundedVal := roundHalfToEven(floatVal)
if roundedVal > float64(math.MaxInt64) {
if floatVal > float64(math.MaxInt64) {
*i = Int64(math.MaxInt64)
} else if roundedVal < float64(math.MinInt64) {
} else if floatVal < float64(math.MinInt64) {
*i = Int64(math.MinInt64)
} else {
*i = Int64(roundedVal)
*i = Int64(roundHalfToEven(floatVal))
}
return nil
}
Expand All @@ -59,9 +57,6 @@ func (i *Int64) UnmarshalJSON(data []byte) error {
}

func roundHalfToEven(val float64) float64 {
if val == float64(int64(val)) {
return val
}
floor := math.Floor(val)
if val-floor > 0.5 || (val-floor == 0.5 && int64(floor)%2 != 0) {
return floor + 1
Expand Down

0 comments on commit a1b2340

Please sign in to comment.