Skip to content

Some floating point numbers are truncated significantly since version 7.2.0Β #2198

@Arduinux

Description

@Arduinux

Describe the bug
Some floating point numbers with two or more zeros after the decimal point only have four or three significant decimal digits.

Troubleshooter report
Here is the report generated by the ArduinoJson Troubleshooter:
[Paste the report here]

  1. The program uses ArduinoJson 7
  2. The issue happens at run time
  3. The issue concerns serialization
  4. Output is incomplete
  5. JsonDocument::overflowed() returns false
  6. The empty/missing value is neither an array, object, nor string
  7. The output is neither a char*, char[N], MqttClient, nor PubSubClient

Environment
Here is the environment that I used:

  • Microcontroller: ESP32
  • Core/runtime: ESP32 core for Arduino v2.0.17 & v3.3.0]
  • IDE: 2.3.6

Reproduction
Here is a small snippet that reproduces the issue.

#ifndef ARDUINO_ESP32_DEV
#  error - Board mismatch. "ESP32 Dev Module" required.
#endif /* ARDUINO_ESP32_DEV */

#define ARDUINOJSON_USE_DOUBLE 1
#include <ArduinoJson.h>

void setup() {
  Serial.begin(115200);

  delay(500);

  Serial.println();
  Serial.println("================================================================================");
  Serial.println();
  Serial.print("ArduinoJson-Version: ");
  Serial.println(ARDUINOJSON_VERSION);

  double doubleA = 0.001403982;
  double doubleB = 0.001043521;
  double doubleC = 0.033841712;
  double doubleD = 0.025639649;
  double doubleE = 0.000292723;

  JsonDocument testDouble;
  testDouble["testA"] = doubleA;
  testDouble["testB"] = doubleB;
  testDouble["testC"] = doubleC;
  testDouble["testD"] = doubleD;
  testDouble["testE"] = doubleE;

  String strTestDouble;
  serializeJson(testDouble, strTestDouble);
  Serial.println();
  Serial.println(strTestDouble);

  JsonDocument testJson;
  deserializeJson(testJson, strTestDouble);

  Serial.println();
  Serial.println(testJson["testA"].as<double>(), 9);
  Serial.println(testJson["testB"].as<double>(), 9);
  Serial.println(testJson["testC"].as<double>(), 9);
  Serial.println(testJson["testD"].as<double>(), 9);
  Serial.println(testJson["testE"].as<double>(), 9);

  String strTestJson;
  Serial.println();
  serializeJson(testJson, strTestJson);
  Serial.println(strTestJson);
}

Compiler output
No relevant output.

Program output
If relevant, include the repro program output.

Expected output:

e.g.:
{"testA":0.001403982,"testB":0.001043521,"testC":0.033841712,"testD":0.025639649,"testE":0.000292723}

0.001403982
0.001043521
0.033841712
0.025639649
0.000292723

{"testA":0.001403982,"testB":0.001043521,"testC":0.033841712,"testD":0.025639649,"testE":0.000292723}

Actual output:

ArduinoJson-Version: 7.4.2

{"testA":0.001403982,"testB":0.001043521,"testC":0.033841712,"testD":0.025639649,"testE":0.000292723}

0.001403982
0.001043521
0.033841712
0.025639649
0.000292723

{"testA":0.001404,"testB":0.001044,"testC":0.033841712,"testD":0.025639649,"testE":0.000293}
================================================================================

ArduinoJson-Version: 7.2.0

{"testA":0.001403982,"testB":0.001043521,"testC":0.033841712,"testD":0.025639649,"testE":0.000292723}

0.001403982
0.001043521
0.033841712
0.025639649
0.000292723

{"testA":0.001404,"testB":0.001044,"testC":0.033841712,"testD":0.025639649,"testE":0.000293}

================================================================================

ArduinoJson-Version: 7.1.0

{"testA":0.001403982,"testB":0.001043521,"testC":0.033841712,"testD":0.025639649,"testE":0.000292723}

0.001403982
0.001043521
0.033841712
0.025639649
0.000292723

{"testA":0.001403982,"testB":0.001043521,"testC":0.033841712,"testD":0.025639649,"testE":0.000292723}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugv7ArduinoJson 7

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions