Skip to content

Commit

Permalink
utils: drop conversion float->string->float
Browse files Browse the repository at this point in the history
remove unclear conversion to string to handle float precision.

Closes: containers#22064

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Mar 18, 2024
1 parent 8d02d8a commit 2566ee2
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 53 deletions.
8 changes: 1 addition & 7 deletions cmd/podman/containers/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 0 additions & 20 deletions libpod/util_test.go

This file was deleted.

8 changes: 1 addition & 7 deletions pkg/domain/infra/abi/pods_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 0 additions & 19 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"os"
"os/exec"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 2566ee2

Please sign in to comment.