Reading in doubles from a text file

Hello,

I currently have a text file I call num.txt which contains

0.0016

So just one number and that's it.

when I run this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, _TCHAR* argv[])
{
	FILE * instFile;
	float g=0;
	double d=0;

	double m = 100;

	instFile = fopen("num.txt", "r");

	fscanf(instFile, "%g", &g);
	
	fclose(instFile);

	cout << g << endl;

	d = (double)g;

	printf("%.15f\n", g, d);

	return 0;

	
}


the screen outputs 2 different numbers one is .0016 which is the number it reads in as a float and the second is 0.001599999959581
which is apparently the double conversion of 0.0016

The problem I am having is that the program I am currently writing needs an insane amount of accuracy thus every number needs to be in a double form.

The only way I know how to read in numbers from a text file is by reading them in as floats, so....

I am wondering is there either a way of reading in numbers as doubles, or converting floats to doubles more accurately.

Thanks for the help
Chris
You can probably get a bit more accuracy with a long double, but you should recognise that all electronic calculating devices will have rounding errors. You may want to think closer on what accuracy you require and what errors you can expect from the native machine types.
I'm sorry for the stupid question, but

your saying that converting .0016 to .0016 is asking to much from C++?
In a sense, yes, but it applies to all languages since it is a result of the binary system used internally. The problem is in the conversion from decimal to binary. The number .0016 is representable in decimal with a terminating fraction, but in binary it is a repeating fraction. Since there is only so many bits to represent a number, it cannot be represented exactly (in binary). Converting to double will not change the situation.
But shouldn't the new answer after it is converted be within epsilon on .0016?

Which is 2.168404344971009e-019
I've apparently made a mistake. Using double does fix the situation:

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <fstream>

int main() {
    std::ifstream fin("num.txt");
    double d;
    fin >> d;
    std::cout << d << '\n';
}

Working on this more the best way that I found around this problem (tested only on .0016) was to read the line in as a string then using the atof command convert the string to a double.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>



using namespace std;

int main(int argc, _TCHAR* argv[])
{
	FILE * instFile;
	char  g [80];
	double d=0;


	instFile = fopen("num.txt", "r");

	fscanf(instFile, "%s", &g);
		
	fclose(instFile);

	printf("%s\n", g);
	d = atof(g);

	printf("%.15f\n", d);

	return 0;

	
}


Again this is only tested for .0016, but I hope it works in other scenarios.
I like your code Hammurabi, but the reason I am using fscanf is because I am reading in a highly formatted text file that contains many delimiters. Will fin give me the same flexibility when reading in these files, such as looking for delimiters, reading in single or multiple lines, etc?
1. How many significant figures do you require? If you want high precision, you should know how high you need it. Your calculations are only as accurate as your source data.

2. It doesn't really matter how you read the entries, but scanf is a tricky beast and is often more trouble than it's worth.

You can read a line and parse that too in C++ using stream, but if you can read your data file, it may not be worth changing it at this point.
kbw,

1) I am writing a code in MATLAB that verifies what the C++ code outputs. Some of the numbers are e-5 or e-6 in precision. So I only need a little precision, but the major problem is that I need the roundoff error in MATLAB and C++ to be the same, the easiest way for me to do this is get 15 digits of precision in C++.

2) I will look into stream commands for future use, but I do have a handle on fscanf, and other similar functions. I am not here to argue which functions are best, but I can say that I know which functions I am more comfortable with, but I am always looking for better tools to do my job.

Thanks for the help...

Chris
Topic archived. No new replies allowed.