As we all know, floating point numbers can't exactly represent most numbers. I'm not asking a question about the precision of floats or doubles.
In a program, floating point numbers "come from somewhere". Some might originate by promoting an integer, others as numeric literals.
int x = 3;
double xd = x;
float xf = 3.0f;
double xd2 = 3.0;
Of course, some floating point numbers come from calculations involving other numbers.
double yd = std::cos(4.0);
In my problem, I will sometimes read in floating point numbers from a text file, and other times, I will receive them from a complex function that I must treat as a black box. The creator of the text file may choose to enter as many significant figures as they like -- they might only use three, or perhaps eight.
I will then perform some computations using these numbers and I would like to know how many significant figures were implied when they were created.
For argument, consider that I am performing an adaptive piecewise least squares fit to the input points. I will continue splitting my piecewise segments until a certain tolerance is achieved. I want to (in part) base the tolerance on the significant figures of the input data -- don't fit to 10^-8 if the data are rounded to the nearest 10^-3.
Others (below) have asked similar questions (but not quite the same). For example, I'm not particularly concerned with a representation output to the user in a pretty form. I'm not particularly concerned with recovering the same floating point representation from an output text value.
How to calculate the number of significant decimal digits of a c++ double?
How can I test for how many significant figures a float has in C++?
I'd like to calculate the sig figs based purely on the value of the double itself. I don't want to do a bunch of text processing on the original data file.
In most cases, floating point numbers will end up with a large series of 0000000 or 99999999 in the middle of them. An intuitive problem statement is that I'm interested in figuring out where that repeating 0 or 9 sequence begins. However, I'd prefer to not do this with a looping rounded string conversion approach. I'm hoping for a fairly direct and efficient way to figure this out.
Perhaps something as simple at looking at the least significant 'on' bit and then figure out its magnitude?