Ruby Numeric#**
Raise a growth factor to a power to repeat the same multiplication over several periods.
A simple example
Numeric#**
- Start with 2 and 3
- Do this Raise 2 to the third power
- Result 8
puts(2.0 ** 3.0)
8.0
Syntax
factor ** periods
Run these examples in Ruby
Save one example at a time as lesson.rb. Use Ruby 2.6 or later.
ruby lesson.rbProject repeated doubling
A print job branches into additional copies at each round. Use the starting count and the number of rounds to project the final batch before reserving paper.
factor = 2.0
periods = 5.0
puts format('%.3f', factor ** periods)
32.000
Five doublings turn one seedling tray into 32 trays.
Estimate copies in a branching print run
A print center branches each master copy into several copies in every round. Project the final count from the branching factor so the operator can budget paper before starting the job.
copies = 3.0
rounds = 4.0
puts format('%.3f', copies ** rounds)
81.000
Starting with one page and tripling its copies through four rounds produces 81 pages.
Count cells after repeated subdivision
A designer starts with one square on a poster. Each round replaces every square with four smaller squares, and the design goes through three rounds.
The factor four applies three times to the starting square.
What to watch for
For exact currency, use decimal arithmetic or integer minor units and a stated rounding policy.