From: Gerhard Sittig Date: Sat, 22 Jan 2022 12:47:17 +0000 (+0100) Subject: std: remove open coded array items count, make floats stand out X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=695aeaf7392e5d4a6186861520df15a3ec7e4b0d;p=libsigrok.git std: remove open coded array items count, make floats stand out Replace the open coded literal 2 in std.c routines, prefer ARRAY_SIZE() instead in the GVariant construction. To not confuse that item count with other literals 2 which are related to avoiding floating point issues by using an epsilon around an imprecise value. See a larger context for an illustration of the commit's motivation. Use literal 2.0 instead of 2 where floating point numbers are involved. To raise awareness during maintenance. --- diff --git a/src/std.c b/src/std.c index 2c4546d6..959d0005 100644 --- a/src/std.c +++ b/src/std.c @@ -605,7 +605,8 @@ SR_PRIV GVariant *std_gvar_tuple_array(const uint64_t a[][2], unsigned int n) rational[1] = g_variant_new_uint64(a[i][1]); /* FIXME: Valgrind reports a memory leak here. */ - g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2)); + g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, + ARRAY_SIZE(rational))); } return g_variant_builder_end(&gvb); @@ -624,7 +625,8 @@ SR_PRIV GVariant *std_gvar_tuple_rational(const struct sr_rational *r, unsigned rational[1] = g_variant_new_uint64(r[i].q); /* FIXME: Valgrind reports a memory leak here. */ - g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2)); + g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, + ARRAY_SIZE(rational))); } return g_variant_builder_end(&gvb); @@ -692,12 +694,12 @@ SR_PRIV GVariant *std_gvar_min_max_step_thresholds(const double min, const doubl * We will never see exactly 0.0 because of the error we're * accumulating, so catch the "zero" value and force it to be 0. */ - v = ((d > (-step / 2)) && (d < (step / 2))) ? 0 : d; + v = ((d > (-step / 2.0)) && (d < (step / 2.0))) ? 0 : d; range[0] = g_variant_new_double(v); range[1] = g_variant_new_double(v); - gvar = g_variant_new_tuple(range, 2); + gvar = g_variant_new_tuple(range, ARRAY_SIZE(range)); g_variant_builder_add_value(&gvb, gvar); } @@ -711,7 +713,7 @@ SR_PRIV GVariant *std_gvar_tuple_u64(uint64_t low, uint64_t high) range[0] = g_variant_new_uint64(low); range[1] = g_variant_new_uint64(high); - return g_variant_new_tuple(range, 2); + return g_variant_new_tuple(range, ARRAY_SIZE(range)); } SR_PRIV GVariant *std_gvar_tuple_double(double low, double high) @@ -721,7 +723,7 @@ SR_PRIV GVariant *std_gvar_tuple_double(double low, double high) range[0] = g_variant_new_double(low); range[1] = g_variant_new_double(high); - return g_variant_new_tuple(range, 2); + return g_variant_new_tuple(range, ARRAY_SIZE(range)); } SR_PRIV GVariant *std_gvar_array_i32(const int32_t a[], unsigned int n) @@ -770,7 +772,7 @@ SR_PRIV GVariant *std_gvar_thresholds(const double a[][2], unsigned int n) for (i = 0; i < n; i++) { range[0] = g_variant_new_double(a[i][0]); range[1] = g_variant_new_double(a[i][1]); - gvar = g_variant_new_tuple(range, 2); + gvar = g_variant_new_tuple(range, ARRAY_SIZE(range)); g_variant_builder_add_value(&gvb, gvar); }