реклама на сайте
подробности

 
 
> Передача float в printf, и преобразование к double
amaora
сообщение Mar 21 2015, 17:56
Сообщение #1


Местный
***

Группа: Участник
Сообщений: 421
Регистрация: 2-01-08
Пользователь №: 33 778



Добавил в свою реализацию 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 это так), но нет ли более красивого решения.

Спасибо.
Go to the top of the page
 
+Quote Post
 
Start new topic
Ответов
Сергей Борщ
сообщение Mar 22 2015, 10:26
Сообщение #2


Гуру
******

Группа: Модераторы
Сообщений: 8 455
Регистрация: 15-05-06
Из: Рига, Латвия
Пользователь №: 17 095



Цитата(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
.



--------------------
На любой вопрос даю любой ответ
"Write code that is guaranteed to work, not code that doesn’t seem to break" (C++ FAQ)
Go to the top of the page
 
+Quote Post



Reply to this topicStart new topic
1 чел. читают эту тему (гостей: 1, скрытых пользователей: 0)
Пользователей: 0

 


RSS Текстовая версия Сейчас: 28th July 2025 - 07:20
Рейтинг@Mail.ru


Страница сгенерированна за 0.01352 секунд с 7
ELECTRONIX ©2004-2016