kgl 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/CODE_OF_CONDUCT.md +0 -0
- data/README.md +16 -0
- data/lib/kgl/kmath.rb +141 -25
- data/lib/kgl/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 65df30d3f96fe5550514270600cc924f65c474889636576cf9be72c6a00879e6
|
|
4
|
+
data.tar.gz: b44e9faebbfa2a4460eb73473bce524c436cb51b632b3840374844790acf96c3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7681c208a10a33e12f18f4950071390f06b39f9acae485f7f3959f31b32f6902941bb5ea563dc5a0300ac49898e144da7286e1e2761cd0cf8065e896d214eae2
|
|
7
|
+
data.tar.gz: 27861552dd8c9d13cecaac96b29bd788a62e88c6910d7bbdff0efe2ae33e7c939c8d2bb50dc36d8473c932d4fc452f41c09a41a57c3fb1b14272bee0c65a9ce6
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 0.0.7
|
|
4
|
+
- Fix missing the alias of `Math.ln` from `Math.log`.
|
|
5
|
+
- Refine `Float#to_r` and `Rational#approx_reduction`.
|
|
6
|
+
|
|
7
|
+
## 0.0.6 2026/04/13
|
|
8
|
+
- Add `Rational#approx_reduction` and refine `Float#to_r` by using this method.
|
|
9
|
+
|
|
3
10
|
## 0.0.5 2026/04/12
|
|
4
11
|
- `Float#to_r` and `Float#to_r_exact` raise `FloatDomainError` when `self` is not finite.
|
|
5
12
|
|
data/CODE_OF_CONDUCT.md
CHANGED
|
File without changes
|
data/README.md
CHANGED
|
@@ -32,6 +32,22 @@ Returns `Rational` value of `self`.
|
|
|
32
32
|
Returns exact `Rational` value of `self`, that may not be desirable for usual usage.
|
|
33
33
|
For instance, (0.1).to_r_exact returns (3602879701896397/36028797018963968) while (0.1).to_r returns (1/10).
|
|
34
34
|
|
|
35
|
+
### `Rational#approx_reduction(all=false)`
|
|
36
|
+
Without the following conditions, this returns `Rational` approximation of `self` with smaller denominator, by using Stern-Brocot tree.
|
|
37
|
+
If `all` is true, this returns an `Array` includes all the path to `self` of the Stern-Brocot tree.
|
|
38
|
+
|
|
39
|
+
When `self==(0/1)` and `all` is false, this returns `self`.
|
|
40
|
+
|
|
41
|
+
When `self==(0/1)` and `all` is true, this returns a pair of negative and positive `Rational` with least denominator which is converted to `0.0` by `Rational#to_f`. For IEEE 754 compliant environments, the value would be -/+ `Rational(1, (((1<<67)-0x4008)<<1008))`.
|
|
42
|
+
|
|
43
|
+
When `self.denominator==1` and `all` is false, this returns a `Rational` value with least absolute numerator which is converted to `self.to_f` by `Rational#to_f`.
|
|
44
|
+
|
|
45
|
+
When `self.denominator==1` and `all` is true, this returns a pair of `Rational`s with least and greatest numerator which are converted to `self.to_f` by `Rational#to_f`.
|
|
46
|
+
|
|
47
|
+
When `self.numerator.abs==1` and `all` is false, this returns a `Rational` value with least denominator which is converted to `selft.to_f` by `Rational#to_f`.
|
|
48
|
+
|
|
49
|
+
When `self.numerator.abs==1` and `all` is true, this returns a pair of `Rational`s with least and greatest denominator which are converted to `self.to_f` by `Rational#to_f`.
|
|
50
|
+
|
|
35
51
|
### `Integer#to_msm(fps=59.94)`
|
|
36
52
|
Returns `"[minutes]:[seconds].[milliseconds]"` which corresponds to `self` frames.
|
|
37
53
|
The submilliseconds will be rounded. `fps` is frames per second.
|
data/lib/kgl/kmath.rb
CHANGED
|
@@ -1,46 +1,162 @@
|
|
|
1
|
+
class << Math
|
|
2
|
+
alias ln log
|
|
3
|
+
end
|
|
1
4
|
module Math
|
|
2
5
|
alias ln log
|
|
3
6
|
module_function
|
|
4
7
|
def log(base, anti_logarithm=base.tap{base=nil})
|
|
5
8
|
if base.nil?
|
|
6
|
-
ln(
|
|
9
|
+
ln(anti_logarithm)
|
|
7
10
|
else
|
|
8
11
|
ln(anti_logarithm).quo(ln(base))
|
|
9
12
|
end
|
|
10
13
|
end
|
|
11
14
|
def lg(anti_logarithm)
|
|
12
|
-
log(2, anti_logarithm)
|
|
15
|
+
log(2.0, anti_logarithm)
|
|
13
16
|
end
|
|
14
17
|
end
|
|
15
18
|
|
|
16
|
-
class
|
|
17
|
-
def
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
19
|
+
class Rational
|
|
20
|
+
def approx_reduction(all=false)
|
|
21
|
+
if self.numerator == 0
|
|
22
|
+
return self unless all
|
|
23
|
+
d = 0
|
|
24
|
+
d += 1 while Rational(1, 1<<d).to_f != 0.0
|
|
25
|
+
n = 1<<d
|
|
26
|
+
(d-1).downto(0) do |i|
|
|
27
|
+
e = (1<<i)
|
|
28
|
+
n -= e if Rational(1, n-e).to_f == 0.0
|
|
29
|
+
end
|
|
30
|
+
return [Rational(-1, n), Rational(1, n)]
|
|
31
|
+
elsif self.denominator == 1
|
|
32
|
+
s = self < 0 ? -1 : 1
|
|
33
|
+
d, n = 0, self.numerator.abs
|
|
34
|
+
f = n.to_f
|
|
35
|
+
d += 1 while (n-(1<<d)).to_f == f
|
|
36
|
+
(d-1).downto(0) do |i|
|
|
37
|
+
e = (1<<i)
|
|
38
|
+
n -= e if (n-e).to_f == f
|
|
39
|
+
end
|
|
40
|
+
r = Rational(n*s, 1)
|
|
41
|
+
return r unless all
|
|
42
|
+
d, n = 0, self.numerator.abs
|
|
43
|
+
d += 1 while (n+(1<<d)).to_f == f
|
|
44
|
+
(d-1).downto(0) do |i|
|
|
45
|
+
e = (1<<i)
|
|
46
|
+
n += e if (n+e).to_f == f
|
|
47
|
+
end
|
|
48
|
+
if n == r.numerator.abs
|
|
49
|
+
return [r]
|
|
50
|
+
else
|
|
51
|
+
return [r, Rational(n*s, 1)]
|
|
52
|
+
end
|
|
53
|
+
elsif self.numerator.abs == 1
|
|
54
|
+
d, n = 0, self.denominator
|
|
55
|
+
f = self.to_f.abs
|
|
56
|
+
if f == 0.0
|
|
57
|
+
unless all
|
|
58
|
+
return Rational(0, 1).approx_reduction(true)[self.numerator<0 ? 0 : 1]
|
|
59
|
+
else
|
|
60
|
+
return Rational(0, 1).approx_reduction(true)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
d += 1 while Rational(1, n-(1<<d)).to_f == f
|
|
64
|
+
(d-1).downto(0) do |i|
|
|
65
|
+
e = (1<<i)
|
|
66
|
+
n -= e if Rational(1, n-e).to_f == f
|
|
67
|
+
end
|
|
68
|
+
r = Rational(self.numerator, n)
|
|
69
|
+
return r unless all
|
|
70
|
+
d, n = 0, self.denominator
|
|
71
|
+
d += 1 while Rational(1, n+(1<<d)).to_f == f
|
|
72
|
+
(d-1).downto(0) do |i|
|
|
73
|
+
e = (1<<i)
|
|
74
|
+
n += e if Rational(1, n+e).to_f == f
|
|
75
|
+
end
|
|
76
|
+
if n == r.denominator
|
|
77
|
+
return [r]
|
|
78
|
+
else
|
|
79
|
+
return [r, Rational(self.numerator, n)]
|
|
80
|
+
end
|
|
30
81
|
else
|
|
31
|
-
|
|
82
|
+
s = self < 0 ? -1 : 1
|
|
83
|
+
q = self.abs
|
|
84
|
+
if q.denominator < q.numerator
|
|
85
|
+
l = [q.numerator.div(q.denominator), 1]
|
|
86
|
+
h = [l[0]+1, 1]
|
|
87
|
+
else
|
|
88
|
+
h = [1, q.denominator.div(q.numerator)]
|
|
89
|
+
l = [1, h[1]+1]
|
|
90
|
+
end
|
|
91
|
+
res = [Rational(*l)]
|
|
92
|
+
r = Rational(*h)
|
|
93
|
+
foo, bar = (r-q).abs, (res[0]-q).abs
|
|
94
|
+
res = [r] if foo < bar || ( foo == bar && h.min < l.min )
|
|
95
|
+
i = 0
|
|
96
|
+
loop do
|
|
97
|
+
m = [l[0]+h[0], l[1]+h[1]]
|
|
98
|
+
r = Rational(*m)
|
|
99
|
+
if q.denominator <= r.denominator
|
|
100
|
+
return all ? res : res[-1]
|
|
101
|
+
elsif r < q
|
|
102
|
+
l = m
|
|
103
|
+
else
|
|
104
|
+
h = m
|
|
105
|
+
end
|
|
106
|
+
res << r*s
|
|
107
|
+
end
|
|
32
108
|
end
|
|
33
|
-
return Rational(numerator, denominator)
|
|
34
109
|
end
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
class Float
|
|
113
|
+
if method_defined?(:to_r)
|
|
114
|
+
alias to_r_exact to_r
|
|
115
|
+
else
|
|
116
|
+
def to_r_exact
|
|
117
|
+
raise FloatDomainError, self.inspect unless self.finite?
|
|
118
|
+
if RADIX == 2
|
|
119
|
+
md = MANT_DIG
|
|
120
|
+
else
|
|
121
|
+
md = (MANT_DIG*Math.lg(RADIX)).floor
|
|
122
|
+
end
|
|
123
|
+
s, e = Math.frexp(self)
|
|
124
|
+
if e < md
|
|
125
|
+
Rational(Math.ldexp(s, md).to_i, 1<<(md-e))
|
|
126
|
+
else
|
|
127
|
+
Rational(self.to_i, 1)
|
|
128
|
+
end
|
|
38
129
|
end
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
130
|
+
end
|
|
131
|
+
def to_r
|
|
132
|
+
a = self.abs
|
|
133
|
+
q = a.to_r_exact
|
|
134
|
+
s = (self < 0.0) ? -1 : 1
|
|
135
|
+
if q.denominator < q.numerator
|
|
136
|
+
l = [q.numerator.div(q.denominator), 1]
|
|
137
|
+
r = Rational(*l)
|
|
138
|
+
return r*s if r.to_f == a
|
|
139
|
+
h = [l[0]+1, 1]
|
|
140
|
+
r = Rational(*h)
|
|
141
|
+
return r*s if r.to_f == a
|
|
42
142
|
else
|
|
43
|
-
|
|
143
|
+
h = [1, q.denominator.div(q.numerator)]
|
|
144
|
+
r = Rational(*h)
|
|
145
|
+
return r*s if r.to_f == a
|
|
146
|
+
l = [1, h[1]+1]
|
|
147
|
+
r = Rational(*l)
|
|
148
|
+
return r*s if r.to_f == a
|
|
149
|
+
end
|
|
150
|
+
loop do
|
|
151
|
+
m = [l[0]+h[0], l[1]+h[1]]
|
|
152
|
+
r = Rational(*m)
|
|
153
|
+
if r.to_f == a
|
|
154
|
+
return r*s
|
|
155
|
+
elsif r < q
|
|
156
|
+
l = m
|
|
157
|
+
else
|
|
158
|
+
h = m
|
|
159
|
+
end
|
|
44
160
|
end
|
|
45
161
|
end
|
|
46
162
|
end
|
data/lib/kgl/version.rb
CHANGED