dikh писал(а):Добрый день.
Пытаюсь пересчитать температуру в давление с помощью следующего кода:
Код работает отлично когда а > 0, но когда a становится отрицательной величиной код генерирует “мусор”. Что нужно сделать чтобы макрос правильно рассчитывал?
Как уже было сказано ранее - неявное преобразование типов играет против вас в строке:
- Код: Выделить всё
j = a / 100
Попробуйте заменить эту конструкцию на
- Код: Выделить всё
j = a / 100.0
Так компилятор поймет, что необходимо результат деления записать как Float.
Вот из мануала текст:
By default, integer numbers (1, 2,3..etc) are considered having integer data type; therefore,
when division is carried out involving two integer numbers where the result should have
decimal point, the decimal part will be removed. To avoid this, add .0 (1.0, 2.0, 3.0...etc) behind
the dividend or the divisor to turn it into a floating point number calculation.
Examples:
A = 3 / 2 = 1 » 3 and 2 are both integers; therefore the result is an integer.
B = 3 / 2.0 = 1.5 » 3 is an integer whereas 2.0 is a floating point number, therefore the
result is a floating point number.
C = 3.0 / 2 = 1.5 » 3.0 is a floating point number whereas 2 is an integer, therefore the
result is a floating point number.
Ну и еще для гарантий в строке 13 напишите:
- Код: Выделить всё
a = ROUND(i * 100)