Page 1 of 1

Compilation warning on chprintf.c

Posted: Tue Oct 21, 2014 9:54 pm
by Korken
Hi,

I just noticed that I currently get an error when compiling chprintf.c:
Compiling main.c
../ChibiOS-RT/os/various/chprintf.c:77:24: warning: overflow in implicit constant conversion [-Woverflow]
100000000, 1000000000, 10000000000, 100000000000,
^
../ChibiOS-RT/os/various/chprintf.c:77:24: warning: overflow in implicit constant conversion [-Woverflow]
../ChibiOS-RT/os/various/chprintf.c:78:24: warning: overflow in implicit constant conversion [-Woverflow]
1000000000000, 10000000000000};

This is this line:

Code: Select all

static long pow10[] = {10, 100, 1000, 10000, 100000, 1000000, 10000000,
                       100000000, 1000000000, 10000000000, 100000000000,
                       1000000000000, 10000000000000};
Which also will give rise to errors in the ftoa() function.

Should it be defined as an 64-bit integer? As long will default to a 32-bit number.


Untested fix (no compilation errors):

Code: Select all

static int64_t pow10[] = {10, 100, 1000, 10000, 100000, 1000000, 10000000,
                          100000000, 1000000000, 10000000000, 100000000000,
                          1000000000000, 10000000000000};

static char *ftoa(char *p, double num, unsigned long precision) {
  int64_t l, temp;

  if (precision == 0)
    precision = FLOAT_PRECISION;
  temp = pow10[precision - 1];

  l = (int64_t)num;
  p = long_to_string_with_divisor(p, l, 10, 0);
  *p++ = '.';
  l = (int64_t)((num - l) * temp);
  return long_to_string_with_divisor(p, l, 10, temp / 10);
}


Best Regards
Emil

Re: Compilation warning on chprintf.c

Posted: Tue Oct 21, 2014 10:00 pm
by Giovanni
I begin to think that adding float support to chprintf() has been an error. I don't want to bring in 64bits math so probably the best change is to limit the precision to what is achievable with 32bits.

Giovanni

Re: Compilation warning on chprintf.c

Posted: Wed Oct 22, 2014 10:10 pm
by colin
Well floating point support is nice as an option that you can enable at compile-time at least via #ifdef. But it might make sense to disable it by default.

Re: Compilation warning on chprintf.c

Posted: Sun Oct 26, 2014 11:29 am
by Giovanni
I fixed the issue by limiting the precision, not sure if the fix is the best possible however.

Giovanni