3

I have a file.txt with hundreds of numbers. They have many digits (max 20) after the point and I need to get them all without truncation, otherwise they introduce errors in the following computations. I made these numbers with matlab so it has a monstrous precision but now I must replicate this behaviour in my program.

I've done this way:

 fstream in;
 in.open(file.txt, ios::in);
 long double number;
 in>>number;

I also tried this

 in.precision(20);
 in>>number;

before each ">>" operation but it is vain

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
user3290180
  • 4,260
  • 9
  • 42
  • 77
  • You are out of luck on this - commonly, `long double` has only 18 digit precision ([link to a demo that shows you how to determine precision on your own system](http://ideone.com/vFq0yK)). – Sergey Kalinichenko Jun 25 '14 at 18:23
  • `precision()` is for printing, not reading. Your compiler might provide a nonstandard 128-bit floating point type, or you can use an arbitrary precision library. – T.C. Jun 25 '14 at 18:24
  • if i copy and past these numbers on my code all goes well but I can't do this with hundreds of numbers and files – user3290180 Jun 25 '14 at 18:26
  • 1
    A question: Are the matlab-values precise (they might be printed with 20 digits and loose precision earlier)? –  Jun 25 '14 at 18:26
  • Following @Dieter's line of thought, the default data type in Matlab is typically 64-bit double. – Nicu Stiurca Jun 25 '14 at 18:31
  • they don't loose precision, if I print them in my code assigning to a long double, all goes well so it must be a problem of functions – user3290180 Jun 25 '14 at 18:42

2 Answers2

4

std::numeric_limits::min std::numeric_limits::digits10 can tell you what your target's actual precision is for long double.

If you find that it's insufficient to represent your data, you probably want arbitrary precision. There are a couple of arbitrary precision number libraries you can use, none of which are standard in C++.

Brian Cain
  • 14,403
  • 3
  • 50
  • 88
  • long double: 3.3621e-4932 one of my numbers is 2.7239385667867091 how can I read all its digits? if I paste this number in my code, all goes well as it were matlab so it must be a problem of functions, not precision, isn't it? – user3290180 Jun 25 '14 at 18:52
  • 1
    `std::numeric_limits::min` tells you the smallest representable number, not the precision. `std::numeric_limits::digits10` tells you the precision (i.e., the maximum number of significant digits). – T.C. Jun 25 '14 at 20:03
1

The following works fine on my system (Win7, VS2012):

#include <fstream>
#include <iostream>

int main (void)
{
    std::ifstream file ("test.txt") ;

    long double d = 0 ;
    file >> d ;

    std::cout.precision (20) ;
    std::cout << d << "\n" ;


    return 0 ;
}

The text file:

2.7239385667867091

The output:

2.7239385667867091

If this doesn't work on your system, then you need to use a third-party number library.

jliv902
  • 1,648
  • 1
  • 12
  • 21
  • ok, I can get the same output, but the computations with these numbers are incorrect in the end. is there a difference between output precision and computation precision? – user3290180 Jun 25 '14 at 20:42
  • the computations are correct, if I put manually the numbers. I think all is due to this loading – user3290180 Jun 25 '14 at 20:54
  • @user3290180 Yes there is a possible difference between output precision and computation precision. Your debugger can tell you if there is a problem with the loading. Perhaps you should edit your question to show a small concise example of the computation not working. I have a feeling that looking at the assembly code will show you why one is right and the other isn't. – jliv902 Jun 25 '14 at 20:57