LoginSignup
1
0

More than 5 years have passed since last update.

Perl6の累乗の書き方

Posted at

こんにちは:whale2:

Ruby, Python, PHP, そしてもちろんPerl5などと同様に、
Perl6 でも累乗は ** です。

use v6;
say 2 ** 0 ; #=> 1
say 2 ** 1 ; #=> 2
say 2 ** 2 ; #=> 4
say 2 ** 3 ; #=> 8
say 2 ** 4 ; #=> 16
say 2 ** 5 ; #=> 32
say 2 ** 6 ; #=> 64
say 2 ** 7 ; #=> 128
say 2 ** 8 ; #=> 256
say 2 ** 9 ; #=> 512

say 2 ** -0 ; #=> 1
say 2 ** -1 ; #=> 0.5
say 2 ** -2 ; #=> 0.25
say 2 ** -3 ; #=> 0.125
say 2 ** -4 ; #=> 0.0625
say 2 ** -5 ; #=> 0.03125
say 2 ** -6 ; #=> 0.015625
say 2 ** -7 ; #=> 0.007813
say 2 ** -8 ; #=> 0.003906
say 2 ** -9 ; #=> 0.001953

Perlのスローガンは"There's More Than One Way To Do It."1ということで、以下のような書き方もできます。

この発想は無かった
use v6;
say 2 ; #=> 1
say 2¹ ; #=> 2
say 2² ; #=> 4
say 2³ ; #=> 8
say 2 ; #=> 16
say 2 ; #=> 32
say 2 ; #=> 64
say 2 ; #=> 128
say 2 ; #=> 256
say 2 ; #=> 512

say 2⁻⁰ ; #=> 1
say 2⁻¹ ; #=> 0.5
say 2⁻² ; #=> 0.25
say 2⁻³ ; #=> 0.125
say 2⁻⁴ ; #=> 0.0625
say 2⁻⁵ ; #=> 0.03125
say 2⁻⁶ ; #=> 0.015625
say 2⁻⁷ ; #=> 0.007813
say 2⁻⁸ ; #=> 0.003906
say 2⁻⁹ ; #=> 0.001953

おわり

上付き文字の文字コードを覚える日がやってきたようだな。

参考と注釈


  1. 略してTMTOWTDI. 「やり方はひとつじゃない」 

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0