This article is a sequel to the article below. Can the number of digits be Math.log10 (x) .floor + 1?-Qiita
Now, how many decimal digits is $ 10 ^ {9942067} $ [^ ans]?
[^ ans]: Of course, it is 9942068 digits without calculation.
Ruby is easy.
irb(main):001:0> (10 ** 9942067).to_s.length
(irb):1: warning: in a**b, b may be too big
=> 8
e? Is it 8 digits? What do you mean?
Besides, there are some strange warnings. Or is this warning a hint? In short, is it too big to calculate properly? But it's not an error.
Would you like to take .to_s.length
and execute it?
No, wait, if a huge integer is displayed, the terminal is likely to become inoperable for a while.
Well, do it.
irb(main):001:0> 10 ** 9942067
(irb):1: warning: in a**b, b may be too big
=> Infinity
What? What is Float :: INFINITY? What if it becomes "positive infinity of floating point number" even though it is an integer power of an integer?
Is the answer "8" the number of characters in ʻInfinity`?
irb(main):002:0> Float::INFINITY.to_s
=> "Infinity"
After all.
By the way, $ 10 ^ {9942066} $, whose exponent is one smaller, can be calculated by 10 ** 9942066
.
This depends on the version of Ruby. This article is based on Ruby 2.7.1.
**
is said to be a specification that abandons the calculation, issues a warning, and returns Float :: INFINITY
when the result is likely to be an integer that is too large.
Therefore, when such a situation is likely to occur in a practical program, it seems necessary to check the calculation result with finite?
before using it.
If you really want to calculate $ 10 ^ {9942067} $, you can calculate it with 10 ** 9942066 * 10
.
Recommended Posts