Помощь - Поиск - Пользователи - Календарь
Полная версия этой страницы: Передача float в printf
Форум разработчиков электроники ELECTRONIX.ru > Сайт и форум > В помощь начинающему > Программирование
amaora
Добавил в свою реализацию printf возможность печатать float, но при компиляции обнаружил следующее:

Код
lib.c: In function ‘printf’:
lib.c:240:6: warning: ‘float’ is promoted to ‘double’ when passed through ‘...’
      fmt_float(va_arg(ap, float), n);
      ^
lib.c:240:6: note: (so you should pass ‘double’ not ‘float’ to ‘va_arg’)
lib.c:240:6: note: if this code is reached, the program will abort
  CC     sh.c
  CC     cml.c
  CC     pmc.c
  LD     fw
/tmp/fw/cml.o: In function `irq_load_time':
cml.c:(.text+0xb8): undefined reference to `__aeabi_f2d'


Как можно избежать преобразования float к double?

Можно сделать вид, что float это int (если размеры совпадают, а у меня на cm4f это так), но нет ли более красивого решения.

Спасибо.
Fat Robot
google "ftoa"
Сергей Борщ
Цитата(amaora @ Mar 21 2015, 19:56) *
Как можно избежать преобразования float к double?
Никак. Стандарт требует применения default argument promotions для аргументов функций с переменным числом параметров.

Цитата
The ellipsis notation in a function prototype declarator causes
argument type conversion to stop after the last declared parameter. The default argument
promotions are performed on trailing arguments.


Цитата
If the expression that denotes the called function has a type that does not include a
prototype, the integer promotions are performed on each argument, and arguments that
have type float are promoted to double
. These are called the default argument
promotions
.

amaora
Пока обошелся передачей указателя на float вместо самого значения. В большинстве случаев надо выводить значения переменных а не выражений от которых нельзя взять адрес.

По поводу стандарта, не понятно зачем так принято.
Для просмотра полной версии этой страницы, пожалуйста, пройдите по ссылке.
Invision Power Board © 2001-2025 Invision Power Services, Inc.