1

How can I test how many significant figures a specified float has in c++? say if i write:

sigfigs(x);

x being the value of the float, it would set an integer value to y, the number of sigfigs

how can i write a void function this way

this has been bugging me for some time, any answers appreciated

btw mysticial this is asking for a code to find the amount of sig figs in a float, not how many there are like the one you linked to as a duplicate -.-

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Stanley
  • 11
  • 1
  • 3
  • 1
    http://stackoverflow.com/questions/12815179/number-of-significant-digits-for-a-floating-point-type – Richard Chambers Sep 06 '14 at 00:53
  • You can't. floats don't store this information. – user2357112 Sep 06 '14 at 01:48
  • Note that the significant figures are base 2 since IEEE floating point numbers are base 2. You can use the `frexp` function to extract the mantissa and then use that to determine the number of significant bits. Divide by lg(10) to get the number of significant digits in base 10. – Raymond Chen Sep 06 '14 at 02:58

2 Answers2

0

This is a bit tricky, because as you should already know, floating-point numbers are often not exact, but rather some approximation of a number. For example, 10.1 ends up as 10.09999.... A double has about 15 digits of precision, so 15 is the largest value your sigfigs() function could reasonably return. And it will need to be overloaded for double and float, because of course float has only half as many digits of precision:

int sigfigs(double x); // returns 1 to 15
int sigfigs(float x); // returns 1 to 7

Now there may be more clever mathematical ways to do this, but one idea is:

int sigfigs(double x) {
  int mag = log10(fabs(x));
  double div = pow(10, mag);
  char str[20];
  int wrote = snprintf(str, sizeof(str), "%.15g", x/div);
  return std::count_if(str, str + wrote, isdigit);
}

This is definitely missing some cases, but I think captures the idea: we first normalize large/small numbers so that we end up with something close to 1, then we print it with a suitable format string to allow the maximum usable precision to be displayed, then we count how many digits there are.

There are notable boundary-condition bugs at 0, 10, etc., which are left as an exercise to correct. Serendipitously, NAN produces 0 which is good; +/- infinity also produce 0.

One final note; this does not strictly conform to the usual definition of significant figures. In particular, trailing zeros after a decimal place are not accounted for, because there is no way to do so given only a double or float. For example, textual inputs of 10 and 10.00000 produce bitwise-identical results from atof(). Therefore, if you need a complete solution conforming to the academic definition of sigfigs, you will need to implement a user-defined type containing e.g. a double and an int for the sigfigs. You can then implement all the arithmetic operators to carry the sigfigs throughout your calculations, following the usual rules.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

Are you trying to determine the number of bits of precision in a floating point number or the number of significant figures in a variable? C and C++ do not generally specify the format to be used for float and double, but if you know the floating point format in which the number is stored and processed, you can determine the number of bits of precision. Most hardware these days uses IEEE 754 format. Looking through the definition would be a good place to start.

Number of significant figures is an entirely different question. Definition of significant figures includes a notion of how many figures are actually meaningful, as opposed to the number of figures available due to the floating point representation. For example, if you sample a voltage with a 12-bit A/D converter (and good enough analog design that all the bits are significant) then the data that you read will have 12 significant bits, and storing it in a format with higher precision does not increase the number of significant figures. For example, you store it in a 16-bit integer or a 32-bit IEEE 754 floating point number, depending on what you plan to do with the data. In either case you still only have 12 significant bits, even though a 32-bit float has a 24-bit mantissa.

Goldberg's What Every Computer Scientist Should Now About Floating-Point Arithmetic pretty thoroughly covers the issue if significant figures and floating-point arithmetic.

wrdieter
  • 2,836
  • 1
  • 21
  • 19