-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstants.rb
More file actions
57 lines (48 loc) · 1.14 KB
/
constants.rb
File metadata and controls
57 lines (48 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require_relative 'numex'
require_relative 'continued_fraction'
module Math
TAU ||= 2*PI
end
module Constants
class Number < Numeric
include Numex
end
class << self
def number(cls=Number, &decl)
Class.new(cls, &decl).new
end
end
E = number do
attr_const inspect: 'e',
to_f: Math::E,
to_i: 2
def continued_fraction
ContinuedFraction.generate do |coeffs|
coeffs << 2 << 1
n = 2
loop do
coeffs << n << 1 << 1
n += 2
end
end
end
end
PI = number do
attr_const inspect: 'π',
to_f: Math::PI,
to_i: 3
end
TAU = number do
attr_const inspect: 'τ',
to_f: Math::TAU,
to_i: 6
end
PHI = number do
attr_const inspect: 'φ',
to_f: 1.6180339887498948482,
to_i: 1
def continued_fraction
ContinuedFraction.new([1].repeat)
end
end
end