Fix mxb::to_binary_size

The function now handles negative and zero values.
This commit is contained in:
Markus Mäkelä
2019-06-19 16:44:37 +03:00
parent 6a83abe5cf
commit 03a7850de4
2 changed files with 3 additions and 3 deletions

View File

@ -28,7 +28,7 @@ namespace maxbase
* *
* @return Value as a human readable size e.g. 5.01MiB * @return Value as a human readable size e.g. 5.01MiB
*/ */
std::string to_binary_size(int64_t size); std::string to_binary_size(size_t size);
/** /**
* Format parameters to a string. Uses printf-formatting. * Format parameters to a string. Uses printf-formatting.

View File

@ -58,10 +58,10 @@ const char* get_binary_size_suffix(int i)
namespace maxbase namespace maxbase
{ {
std::string to_binary_size(int64_t size) std::string to_binary_size(size_t size)
{ {
// Calculate log1024(size) and round it up // Calculate log1024(size) and round it up
int idx = floor(log(size) / log(1024)); int idx = floor(log(size ? size : 1) / log(1024));
double num = size / pow(1024, idx); double num = size / pow(1024, idx);
char buf[200]; // Enough for all possible values char buf[200]; // Enough for all possible values
snprintf(buf, sizeof(buf), "%.2lf%s", num, get_binary_size_suffix(idx)); snprintf(buf, sizeof(buf), "%.2lf%s", num, get_binary_size_suffix(idx));