在 Arduino 中除以两个整数
Divide two integers in Arduino
我正在尝试将两个整数值相除并存储为浮点数。
void setup()
{
lcd.begin(16, 2);
int l1 = 5;
int l2 = 15;
float test = 0;
test = (float)l1 / (float)l2;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(test);
}
由于某种原因,我希望这是相当明显的,我似乎无法存储和显示正确的值。"test"变量始终设置为 0。
如何转换整数值?
它必须是您的LCD打印例程,因此您使用的铸件是正确的。
我在Arduino上使用串行打印而不是LCD进行了尝试。对于以下完整代码示例,预期结果显示在串行监视器(由菜单"工具"->"串行监视器"启动)中:
Start...
5
15
0.33
0.33333334922790
最后一个结果行确认它是一个 4 字节浮点数,具有 7-8 个有效数字。
完整的代码示例
/********************************************************************************
* Test out for Stack Overflow question "Divide two integers in Arduino", *
* <http://stackoverflow.com/questions/13792302/divide-two-integers-in-arduino> *
* *
********************************************************************************/
// The setup routine runs once when you press reset:
void setup() {
// Initialize serial communication at 9600 bits per second:
Serial.begin(9600);
//The question part, modified for serial print instead of LCD.
{
int l1 = 5;
int l2 = 15;
float test = 0;
test = (float)l1 / (float)l2;
Serial.println("Start...");
Serial.println("");
Serial.println(l1);
Serial.println(l2);
Serial.println(test);
Serial.println(test, 14);
}
} //setup()
void loop()
{
}
>lcd.print
不知道如何打印float
,所以你最终打印的是整数。
相关文章:
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 比较C++中两个整数的最有效和最干净的方法是什么?
- 比较两个整数在C++中与未知 int 类型的相等性
- 如何组合两个整数向量
- 在两个整数之间交换最右边的N位
- 打印出两个整数之间的偶数
- Python 等效代码,可像C++一样直接附加两个整数
- 移位运算符如何在查找两个整数中的不同位数?
- 运算符"|"如何计算两个整数?
- 如何找出在两个整数的乘积中设置了多少位(等于 1)
- 当键是两个整数时,C 地图或unordered_map
- C 将两个整数进入队列
- 比较 C++ 字符串打印中的两个整数数组
- 为什么双精度不存储两个整数的乘积
- C++将字符串输入拆分为两个整数
- C 中的内存模型:为什么在同一内存位置分配了结构中的两个整数
- 获取所有方式以两个整数的产品表示数字
- Systemc传输级建模从TLM_Generic_Payload提取两个整数
- 数据的所有组合都是产品的两个整数
- 编写一个功能,该函数将使用框架交换两个整数