iio: iio_format_value(): Fix IIO_VAL_FRACTIONAL_LOG2 values between -1.0 and 0.0
authorLars-Peter Clausen <lars@metafoo.de>
Tue, 15 Dec 2020 19:17:42 +0000 (20:17 +0100)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Thu, 11 Mar 2021 20:47:00 +0000 (20:47 +0000)
When formatting a value using IIO_VAL_FRACTIONAL_LOG2 and the values is
between -1 and 0 the sign is omitted.

We need the same trick as for IIO_VAL_FRACTIONAL to make sure this gets
formatted correctly.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Link: https://lore.kernel.org/r/20201215191743.2725-2-lars@metafoo.de
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/industrialio-core.c

index 023040e..1b40136 100644 (file)
@@ -659,7 +659,10 @@ static ssize_t __iio_format_value(char *buf, size_t len, unsigned int type,
        case IIO_VAL_FRACTIONAL_LOG2:
                tmp2 = shift_right((s64)vals[0] * 1000000000LL, vals[1]);
                tmp0 = (int)div_s64_rem(tmp2, 1000000000LL, &tmp1);
-               return scnprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
+               if (tmp0 == 0 && tmp2 < 0)
+                       return snprintf(buf, len, "-0.%09u", abs(tmp1));
+               else
+                       return scnprintf(buf, len, "%d.%09u", tmp0, abs(tmp1));
        case IIO_VAL_INT_MULTIPLE:
        {
                int i;