How to use Long float in Arduino

Hello Everyone

How can I use double var for this number "0.0893681318681320" ?
I mean something like this : double x = 0.0893681318681320"
I can not print them all I think there is a problem

Which Arduino are you using?

If it's an 8-bit AVR, then there are two problems: 1. double is the same as float (IEEE 754 single-precision), and 2. the included print functions (e.g. Serial.print) cannot print floating point numbers correctly. This is intentional, because printing floats is hard and requires a significant amount of memory.

Do you have a suggestion?

Use a 32-bit microcontroller, such as ARM based ones.

yes

Either use a third-party library that can handle double-precision floats in software, or get a microcontroller that is suitable for your project and that supports floating point numbers either in hardware, or has native double support in software.

For example, I tried it on my computer but it had the same problem

What did you try on your computer?

#include <iostream>
#include <iomanip>
#include <limits>

int main() {
    double x = 0.0893681318681320;
    std::cout << std::setprecision(std::numeric_limits<double>::digits10)
              << x << std::endl;
}
0.089368131868132
#include <stdio.h>
#include <string.h>
#include<stdbool.h> 
#include<string.h>
#include<stdlib.h>

#define MAX_LEN 256


int main(void)
{
    
    double num = 0.0893681318681320;

    printf("%f", num );

    return 0;
}

in C language

This is output => 0.089368

Arduino uses C++, not C.

That's expected, if you read the documentation you'll see that the default precision of printf is 6: https://en.cppreference.com/w/c/io/fprintf

STM32 is a line of microcontrollers. But yes, you can program an STM32 in C++.

Can I print them all in C ++ with Long double?
double x = 0.0893681318681320;
this your output 0.089368131868132

The Arduino-compatible Teensy boards implement ltype double.

They are very fast, reliable and well supported products.

First I want to test it on a computer, I can not print them all in C ++

#include <iostream>
#include <iomanip>
#include <limits>

int main() {
    long double x = 0.0893681318681320;
    std::cout << std::setprecision(std::numeric_limits<double>::digits10)
              << x << std::endl;
}

**0.089368131868132**

A double has fewer than 16 significant digits, printing more isn't really useful in most cases, but if you wanted, you could use std::setprecision(std::numeric_limits<double>::digits10 + 1) to print one more digit.

C++17 has the to_chars function, which prints the number with the minimum number of digits to be able to reconstruct the number exactly:

#include <iostream>
#include <charconv>
#include <string_view>

int main() {
    double x = 0.0893681318681320;
    char buf[32];
    auto [ptr, ec] = std::to_chars(std::begin(buf), std::end(buf), x);
    if (ec == std::errc()) {
        std::string_view res(buf, ptr - buf);
        std::cout << res << std::endl;
    }
}
0.089368131868132
1 Like

you are asking it to print it as %f float. ask it to print with more decimal places, there are all sorts of modifiers, lookup printf function for the hint

void setup() {
  // Teensy 3.2
Serial.begin(9600);
while(!Serial);
double p = 3.141592653589793L;  //must use "L" or compiler assumes float
Serial.println(p, 15);  //prints 3.141592653589793
}
void loop() {}
1 Like

Sorry it does not work