integer_log
here is what I meant.
I tested that number of digits function for positive integers. It worked well until this boundary:
n = 9999999999999999999999999
print(n, num_digits(n), len(str(n)))
n += 1
print(n, num_digits(n), len(str(n)))
Output:
9999999999999999999999999 25 25
10000000000000000000000000 25 26
Weâre still not sure what the OP wants the function to do.
I wonder, though, whether it is really important to be able to classify numbers into orders of magnitude with boundaries. It seems more important to be able to compare two numbers and decide whether they differ in order of magnitude.
On a logarithmic sale, ie my slide rule, 5 is closer to 10 than 1, so log(5) - log(1) > log(10) - log(5) or 0.7 - 0 > 1 - 0.7 == .7 > .3
Iâm sure we all have already recognized the following issue.
Letâs suppose we have two gold nuggets, one worth $3170 (US) and the other worth $31500 (US). Letâs also suppose that we are classifying numbers into orders of magnitude based on raising 10 to powers such as 0.5, 1.5, 2.5, and so on.
Based on the above, both of our gold nuggets have values with an order of magnitude of 3. See the calculations below.
Letâs convert those values to pound sterling. Again, see the values below. Now, the two gold nuggets have values that are in different orders of magnitude.
# Orders or magnitude: boundaries
for n in range(10):
print(f"{n:2} {10 ** (n + 0.5):15.2f}")
# Today 1 United States Dollar equals 0.85 Pound sterling.
# Gold nuggets: values in dollars.
small_gold_nugget = 3170
large_gold_nugget = 31500
print("Dollars:")
print(small_gold_nugget, large_gold_nugget)
# Convert values to pound sterling
small_gold_nugget *= 0.85
large_gold_nugget *= 0.85
print("Pound Sterling:")
print(small_gold_nugget, large_gold_nugget)
Output:
0 3.16
1 31.62
2 316.23
3 3162.28
4 31622.78
5 316227.77
6 3162277.66
7 31622776.60
8 316227766.02
9 3162277660.17
Dollars:
3170 31500
Pound Sterling:
2694.5 26775.0
Perhaps instead, we should discuss how to compare two or more numbers to decide whether they are all in the same order of magnitude, based on the ratios of their values. Obviously, if one number is 10 times the magnitude of another, they are in different orders of magnitude. But, what if one is 9.5 times the other? Where should we place the cutoff point? What if we have three of more numbers? How do we compare them as a group? Should we just consider the breadth of the range of values?
If youâre comparing just two numbers, divide them, and see what order of magnitude their ratio is. Use standard rules for defining that.
If youâre comparing more than two numbers, these kinds of weird edge cases no longer look as weird, because ANY form of bucketing system is going to have nearby values landing in adjacent buckets.
Let me stress this again: Orders of magnitude are NOT LOGARITHMS. A logarithm can be used for extremely precise calculations; an order of magnitude is an approximation. It will not always be precise. But it is extremely useful in practice.
Thanks for that and the additional accompanying information.
If, with some clarification, the OP lets us know what is being sought here, we will be able to discuss it âŚ