Double floating precision operations issue

kkravchuk

New member
Board: Teensy 4.1
Environment: Platformio
Framework: Arduino-Teensy

platformio.ini:

Code:
[teensy41]
platform               = https://github.com/tsandmann/platform-teensy.git#b92da4fa983a06230c46815f89b6ad788cdde023
board                  = teensy41
framework              = arduino
lib_deps               = 
  https://github.com/tsandmann/freertos-teensy#379d9b5ad82f8c1be995f95add3fd32bb3add2c1
  ssilverman/QNEthernet@^0.19.0
  https://github.com/sparkfun/SparkFun_u-blox_GNSS_Arduino_Library.git#3416a3140fb74c3b80fc680140d029182fcd03ad
  https://github.com/sparkfun/SparkFun_LSM6DS3_Arduino_Library.git
build_flags = 
  -DUSING_ARDUINO_DRIVERS
  -DARDUINO_TEENSY41

NOTE: the code uses a modified arduino framework core to work with the ported FreeRTOS (https://github.com/tsandmann/platform-teensy.git#b92da4fa983a06230c46815f89b6ad788cdde023). This has an updated the G++ compiler.

Problem:
The teensy board seems to have trouble with math operations on double precision floating point variables. The value that is returned is different starting at around 7th significant digit, which makes me wonder if only 32-bit floating point operation is happening.
The result of the code below is compared to the output of computations in Wolfram Alpha.

Code:
#inclde "Arduino.h"
#include <math.h>

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  double operand = 0.693749222;
  double result = sin(operand);
  Serial.println(result, 15);
  delay(1000);
}

The output of the code above produces the value of 0.639424294929401.

However, if you compare it to the Wolfram Alpha output for the same computation, you see the following:
0.63942427418458347537687

Once again, here is a comparison:
0.639424294929401
0.63942427418458347537687

As you can tell, the difference begins at the 8th significant digit. However, in some cases the difference begins at 7th digit, but never less.

Does anyone have an idea as to what may be happening?
 
Arduino API Serial.print() uses 32 bit float. If you give it a 64 bit double, compiler automatically converts to 32 bit float before calling print().

For 64 bit result, use Serial.printf(). Like this:

Code:
void setup() {
  Serial.begin(115200);
}

void loop() {
  double operand = 0.693749222;
  double result = sin(operand);
  Serial.printf("%.15f\n", result);
  delay(1000);
}
 
Hey Paul, thank you for a quick reply.
Unfortunately, the solution above produces the same result.
Are you able to recreate the same issue on your local teensy?

FYI the toolchain that is used in my platformio project is arm-cortexm-linux of version 12.2.0-4+sha.2330882
 
Last edited:
Out of curiosity, could you try adding “-fno-single-precision-constant” to your build flags? (“build_flags” option in platformio.ini.)
 
Are you able to recreate the same issue on your local teensy?

I ran it here on Teensy 4.0, using Arduino IDE 2.1.1 and Teensyduino 1.58.1.

screenshot.png

As you can see in this screenshot, it's printing 0.639424274184583.


FYI the toolchain that is used in my platformio project is arm-cortexm-linux of version 12.2.0-4+sha.2330882

Maybe you could give Arduino IDE a quick test?
 
I ran it here on Teensy 4.0, using Arduino IDE 2.1.1 and Teensyduino 1.58.1.

As you can see in this screenshot, it's printing 0.639424274184583. Maybe you could give Arduino IDE a quick test?

Tested with Arduino 1.8.19 and TeensyDuino 1.59b2. Same result as Paul, shown below.

Code:
0.639424274184583
 
I ran it here on Teensy 4.0, using Arduino IDE 2.1.1 and Teensyduino 1.58.1.

View attachment 31617

As you can see in this screenshot, it's printing 0.639424274184583.




Maybe you could give Arduino IDE a quick test?



Hey there! Okay, so all I had to do was to update the custom arduino framework (the one by Timo that works with his freertos port). Double precision works now :cool:
 
Back
Top