utils: drop conversion float->string->float

remove unclear conversion to string to handle float precision.

Closes: https://github.com/containers/podman/issues/22064

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano 2024-03-18 22:45:32 +01:00
parent 8d02d8a96b
commit 2566ee2f38
No known key found for this signature in database
GPG Key ID: 67E38F7A8BA21772
4 changed files with 2 additions and 53 deletions

View File

@ -15,7 +15,6 @@ import (
"github.com/containers/podman/v5/cmd/podman/validate"
"github.com/containers/podman/v5/libpod/define"
"github.com/containers/podman/v5/pkg/domain/entities"
"github.com/containers/podman/v5/utils"
"github.com/docker/go-units"
"github.com/spf13/cobra"
)
@ -243,12 +242,7 @@ func (s *containerStats) MemUsageBytes() string {
}
func floatToPercentString(f float64) string {
strippedFloat, err := utils.RemoveScientificNotationFromFloat(f)
if err != nil {
// If things go bazinga, return a safe value
return "--"
}
return fmt.Sprintf("%.2f", strippedFloat) + "%"
return fmt.Sprintf("%.2f%%", f)
}
func combineHumanValues(a, b uint64) string {

View File

@ -1,20 +0,0 @@
//go:build !remote
package libpod
import (
"testing"
"github.com/containers/podman/v5/utils"
"github.com/stretchr/testify/assert"
)
func TestRemoveScientificNotationFromFloat(t *testing.T) {
numbers := []float64{0.0, .5, 1.99999932, 1.04e+10}
results := []float64{0.0, .5, 1.99999932, 1.04}
for i, x := range numbers {
result, err := utils.RemoveScientificNotationFromFloat(x)
assert.NoError(t, err)
assert.Equal(t, result, results[i])
}
}

View File

@ -10,7 +10,6 @@ import (
"github.com/containers/podman/v5/libpod"
"github.com/containers/podman/v5/pkg/domain/entities"
"github.com/containers/podman/v5/pkg/rootless"
"github.com/containers/podman/v5/utils"
"github.com/docker/go-units"
)
@ -85,12 +84,7 @@ func combineBytesValues(a, b uint64) string {
}
func floatToPercentString(f float64) string {
strippedFloat, err := utils.RemoveScientificNotationFromFloat(f)
if err != nil || strippedFloat == 0 {
// If things go bazinga, return a safe value
return "--"
}
return fmt.Sprintf("%.2f", strippedFloat) + "%"
return fmt.Sprintf("%.2f%%", f)
}
func pidsToString(pid uint64) string {

View File

@ -6,7 +6,6 @@ import (
"io"
"os"
"os/exec"
"strconv"
"strings"
"time"
@ -111,24 +110,6 @@ func TarWithChroot(source string) (io.ReadCloser, error) {
return chrootarchive.Tar(source, nil, source)
}
// RemoveScientificNotationFromFloat returns a float without any
// scientific notation if the number has any.
// golang does not handle conversion of float64s that have scientific
// notation in them and otherwise stinks. please replace this if you have
// a better implementation.
func RemoveScientificNotationFromFloat(x float64) (float64, error) {
bigNum := strconv.FormatFloat(x, 'g', -1, 64)
breakPoint := strings.IndexAny(bigNum, "Ee")
if breakPoint > 0 {
bigNum = bigNum[:breakPoint]
}
result, err := strconv.ParseFloat(bigNum, 64)
if err != nil {
return x, fmt.Errorf("unable to remove scientific number from calculations: %w", err)
}
return result, nil
}
// GuardedRemoveAll functions much like os.RemoveAll but
// will not delete certain catastrophic paths.
func GuardedRemoveAll(path string) error {