]> sigrok.org Git - libsigrok.git/commitdiff
std: remove open coded array items count, make floats stand out
authorGerhard Sittig <redacted>
Sat, 22 Jan 2022 12:47:17 +0000 (13:47 +0100)
committerGerhard Sittig <redacted>
Sun, 6 Feb 2022 17:53:53 +0000 (18:53 +0100)
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.

src/std.c

index 2c4546d63f22f32c5d11d3ee91fe6ac5493ea55b..959d0005680c5d2ebad6abb1637ca28a9221f035 100644 (file)
--- 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);
        }