为什么Arduino不能处理高精度数字?

Why cant't Arduino deal with high precision numbers?

在我的项目中,我需要用高精度数字进行一些计算。但我注意到答案不正确。下面的代码显示了我的意思。

void setup() {
  Serial.begin(9600);
  double l = 32.48750458;
  double n = 32.48751068;
  double im = l - n;
  double im1 = im * 0.605;
  double cs = 32.48751068 + im;
  Serial.println(l, 8);
  Serial.println(n, 8);
  Serial.println(im1, 8);
  Serial.println(cs, 8); 
}

void loop() {
}

这段代码在串口监视器上的输出是:

32.48750305
32.48751068
-0.00000462
32.48750305

那么我如何在 Arduino 中处理这种精度?

在大多数 Arduino 上,double 数据类型不存在。他们最终都只是普通的浮动。 arduino 只是没有足够的力量来玩双精度数字。生成的代码不会为程序的其余部分留出空间。

浮点数并不是那么精确。他们得到大约 6 或 7 位数的精度。通常最好使用 unsigned long 并坚持使用定点数学,这样您可以获得至少 9 位数字并且没有任何近似值。