Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Can I use long double without losing precision?: (2 Items)
   
Can I use long double without losing precision?  
std::istringstream is losing precision when converting a string to long double. 
Some people told me it is because an 80-bit double (long double) is not large enough to store the number. Is this issue 
related to QNX itself or some compiling flags are missing? 

To demonstrate the problem, I wrote this code:

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

int main(){
    long double convertedNumber;
    std::string numberString ("5.94865747678615882510631e+4931");

    //From string to long double
    std::istringstream iss(numberString);
    iss >> convertedNumber;

    std::cout<< std::setprecision(30) << numberString << "\n";
    std::cout<< std::setprecision(30) << convertedNumber << "\n";

    return 0;
}


The output is always:
5.94865747678615882510631e+4931
5.9486574767861588254e+4931

Compiled with:
QCC -V5.4.0,gcc_ntox86_64 main.cpp -o qnxTest -std=gnu++14
Re: Can I use long double without losing precision?  
long double is defined as 80 bits in size with a certain internal structure by some IEEE standard. So in my opinion, 
this is not a QNX related issue.

But you could easily check that by having your code compiled for a Linux box and observe the output.

Regards,
Al