Counting significant digits in Python?

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

Thanks in advance.

+2
source share
4 answers

No. Significant numbers are simply not a big deal and little support in computer languages. People doing real calculations need error bars that are much more accurate - real measurements say very accurate things, such as β€œit's 0.11 Β± 0.03 mm,” instead of talking about less accurate ones statements β€œit is 0.1 mm” or β€œit is 0.11 mm” which forces you to choose a force of ten, even if your inaccuracy does not actually fall to ten.

+4
source

You may be interested in a floating-point library of arbitrary precision, such as:

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

+2
source

Computers just don't work that way, at least unless they are programmed for it. Assumption is the amount you give them. If you create number 2/3 as 0.666666666666666667, then all operations treat it like that. This error in the least significant figure may eventually spread to big errors in subsequent calculations, but this is what good code should handle, using algorithms that minimize these problems when possible.

As I said, computers do what they are told. Thus, there are written packages that use the so-called interval arithmetic. Then the number can be described as an interval, so we could create 2/3 as an interval [0.6666666666666666,0,6666666666666667]. We can work at intervals, add, subtract, multiply, etc. These operations will often see the width of the intervals when we work with them.

However, the fact is, even if you use interval arithmetic tools, it is you who must know at the beginning the number of significant digits in your numbers. If you create the number as 2.2, saving it as double, then the computer will actually try to save the number as 2.200000000000000, and suppose that all the numbers are exactly correct. In fact, since floating point arithmetic is used, the number will actually be stored inside the binary number. Thus, 2.2 is likely to be effectively stored as a number:

2.20000000000000017763568394002504646778106689453125

because most decimal fractional numbers are not represented exactly in binary form. Again, care should be used in all software, but also always by the person who uses these tools to understand what their numbers really mean.

This last point is important. Many people refer to the number generated by the computer as truth, as transmitted by the computer god on a stone tablet. If the computer prints 1.4523656535725, they count every digit of what they see. In fact, common sense should be used here to know that perhaps this number was created from data that had only 3 significant digits, so you can only rely on the first few significant digits of that number. And, of course, that is why you are taught about this concept in school, they know what to trust and what not to trust. Remember, however, that computers tend to trust forever. You must apply a filter.

+1
source

I found a solution to this post in another question:

Python counting significant digits

The idea here is that you pass the float method as a String, and then the method uses regular expressions to count the number of significant digits by breaking the lines, where is "e" (for a floating-point string in scientific format) and where is the dot (for ordinary float lines).

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

However, I think this is better than

"Significant numbers are just not a big deal and little support in computer languages."

answer. It may not be easy, but you can do it, even if not perfect.

0
source

Source: https://habr.com/ru/post/1381375/


All Articles