Calculating the maximum precision, some doubts

Hey there. Didn't find anything similar searching the forum so I'm posting this new thread expecting some help adressing minor questions about the numeric_limits template class and the validity of my code.

First of all, numeric_limits is dependent on the machine where the program is running, and the OS too, am I right? I mean, e.g the numeric_limits<double>::digits10 is calculated based on the machine and OS, then returned as a value, or is it fixed on the compiler?

And for last, this is the code I made for calculating the smaller prec which satisfies 1 + prec > 1, using both float and double variables. Is is corrent, the way I did it? In my analysis, the loop for calculating the precision is correct, but I'm not quite sure about the change of precision in cout.

(some parts of the code are in portuguese. if necessary, ask and I will translate it for you. also, I don't know if I managed to make my doubts clear, so please ask if you don't understand something)

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cstdlib>
#include <limits>

//numeric_limits is a template class from where you can get information about fundamental types of data.
//here we are going to use 'digits10' (frmo dbl and flt) which corresponds to the number of digits in
//decimal base that can be represented without change
typedef std::numeric_limits<double> dbl;
typedef std::numeric_limits<float> flt;

using namespace std;

int main() 
{
	//prec (precision) represents the smaller number which let (1 + prec) > 1 be true
	float prec_simples = 1, teste_s = 1 + prec_simples;
	double prec_dupla = 1.0, teste_d = 1.0 + prec_dupla;
	int count = 0;

	cout << "Testando para tipo de dado float...";
	while (teste_s > 1.0) {
		prec_simples /= 2;
		teste_s = 1 + prec_simples;
	}

	//here we change the default precision of cout (which is 6 digits) to 'digits10', discussed earlier
	cout.precision(flt::digits10);
	cout << "\nPrecisao calculada em relacao ao numero 1: " << prec_simples * 2
		<< "\n\nPressione qualquer tecla para continuar o programa";
	getchar();

	count = 0;
	cout << "\n\nTestando para tipo de dado double...\n";
	while (teste_d > 1.0) {
		prec_dupla /= 2;
		teste_d = 1.0 + prec_dupla;
	}

	cout.precision(dbl::digits10);
	cout << "Precisao calculada em relacao ao numero 1: " << prec_dupla * 2;

	cout << "\n\nPressione qualquer tecla para sair";
	getchar();

	return 0;
}
> the numeric_limits<double>::digits10 is calculated based on the machine and OS, then returned as a value,
> or is it fixed on the compiler?

The compiler implementation is dependant on the machine (and the OS)

GCC 4.6.1 on UltraSPARC T2 Solaris is a different C++ implementation from GCC 4.6.1 on i386 FreeBSD. Numeric limits values are typically dependant on the hardware platform.


> the smaller prec which satisfies 1 + prec > 1

std::numeric_limits<FLOATING_POINT_TYPE>::epsilon()
http://en.cppreference.com/w/cpp/types/numeric_limits/epsilon
Thanks you for the answer and the link. Just for confirmation, you mean my code is going to correctly display the machines epsilon, but it's needless since there is already the numeric_limits::epsilon() which calculates it, am I right?
> my code is going to correctly display the machines epsilon

I think it would depend on the float_round_style.std::numeric_limits<double>::round_style

You might want to have a look at this, if you haven't seen it earlier:
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html#929


Many thanks for the paper, I studying it entirely.
Topic archived. No new replies allowed.