Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting significant figures in Python?

Is there a way in Python to count the significant figures in a double/float/etc? I'm not seeing an easy way to do this, but I'd expect it to be in the library.

Thanks in advance.

like image 471
user456584 Avatar asked Nov 12 '11 00:11

user456584


2 Answers

You may be interested in an arbitrary precision floating point library such as this one:

http://code.google.com/p/mpmath/

like image 96
Mr.Wizard Avatar answered Sep 29 '22 22:09

Mr.Wizard


I found a solution to this post in another question:

Python counting significant digits

The idea here is that you pass the float as a String to the methods, and then the method uses regular expressions to count the number of significant digits by splitting strings where "e" is (for float string in scientific format) and where the dot is (for normal float strings).

It seems to work well up to 8 significant digits, but the behavior is not guaranteed after the 9th.

Still, I believe this is better than the

"Significant digits are just not that big a deal and get little support in computer languages"

response. It may not be easy, but you can definitely do it, even if not perfectly.

like image 39
Flame_Phoenix Avatar answered Sep 29 '22 22:09

Flame_Phoenix