From 5751bafb8db06b843a9a79e100751cfcadc09f90 Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Fri, 27 Oct 2023 18:34:47 +0800 Subject: Rewrite Meter_humanUnit() to accept floating point value Since Meter_humanUnit() is often called with floating point values in Meter objects, rewrite the function to let it process `double` type natively, and save floating point to integer casts. The rewritten function: * Allows higher orders of magnitude including 'R' and 'Q', and addresses infinity. (The previous version has a maximum value of (2^64 - 1) representing 16 ZiB.) * Rounds values when they are in intervals (99.9, 100) and (9.99, 10), and displays them with correct precision (number of fraction digits). * Produces assertion error on negative and NaN values (undefined behavior). Signed-off-by: Kang-Che Sung --- Meter.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'Meter.h') diff --git a/Meter.h b/Meter.h index e0ca1f07..6b14634a 100644 --- a/Meter.h +++ b/Meter.h @@ -146,7 +146,9 @@ extern const MeterClass Meter_class; Meter* Meter_new(const Machine* host, unsigned int param, const MeterClass* type); -int Meter_humanUnit(char* buffer, unsigned long int value, size_t size); +/* Converts 'value' in kibibytes into a human readable string. + Example output strings: "0K", "1023K", "98.7M" and "1.23G" */ +int Meter_humanUnit(char* buffer, double value, size_t size); void Meter_delete(Object* cast); -- cgit v1.2.3