sollya 0.1.0
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 +7 -0
- data/.yardopts +10 -0
- data/LICENSE +674 -0
- data/README.md +6 -0
- data/ext/extconf.rb +21 -0
- data/ext/mpfr_rb.c +4408 -0
- data/ext/sollya_rb.c +3423 -0
- data/lib/sollya/version.rb +4 -0
- data/lib/sollya.rb +110 -0
- data/sollya.gemspec +26 -0
- metadata +48 -0
data/lib/sollya.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
require_relative './sollya/sollya'
|
2
|
+
require_relative './sollya/version'
|
3
|
+
|
4
|
+
class MPFR
|
5
|
+
def +@
|
6
|
+
self
|
7
|
+
end
|
8
|
+
|
9
|
+
def -@
|
10
|
+
r = MPFR.new(prec: prec)
|
11
|
+
r.neg(self)
|
12
|
+
r
|
13
|
+
end
|
14
|
+
|
15
|
+
def +(other)
|
16
|
+
return to_r + other if other.is_a?(Rational)
|
17
|
+
other = other.to_mpfr
|
18
|
+
r = MPFR.new(prec: [prec, other.prec].max)
|
19
|
+
r.add(self, other)
|
20
|
+
r
|
21
|
+
end
|
22
|
+
|
23
|
+
def -(other)
|
24
|
+
return to_r - other if other.is_a?(Rational)
|
25
|
+
other = other.to_mpfr
|
26
|
+
r = MPFR.new(prec: [prec, other.prec].max)
|
27
|
+
r.sub(self, other)
|
28
|
+
r
|
29
|
+
end
|
30
|
+
|
31
|
+
def *(other)
|
32
|
+
return to_r * other if other.is_a?(Rational)
|
33
|
+
other = other.to_mpfr
|
34
|
+
r = MPFR.new(prec: [prec, other.prec].max)
|
35
|
+
r.mul(self, other)
|
36
|
+
r
|
37
|
+
end
|
38
|
+
|
39
|
+
def /(other)
|
40
|
+
return to_r / other if other.is_a?(Rational)
|
41
|
+
other = other.to_mpfr
|
42
|
+
r = MPFR.new(prec: [prec, other.prec].max)
|
43
|
+
r.div(self, other)
|
44
|
+
r
|
45
|
+
end
|
46
|
+
|
47
|
+
def %(other)
|
48
|
+
return to_r % other if other.is_a?(Rational)
|
49
|
+
other = other.to_mpfr
|
50
|
+
r = MPFR.new(prec: [prec, other.prec].max)
|
51
|
+
r.fmod(self, other)
|
52
|
+
r
|
53
|
+
end
|
54
|
+
|
55
|
+
def **(other)
|
56
|
+
other = other.to_mpfr
|
57
|
+
r = MPFR.new(prec: [prec, other.prec].max)
|
58
|
+
r.pow(self, other)
|
59
|
+
r
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class Float
|
64
|
+
# Convert the _float64_ value to a C99 hexadecimal string
|
65
|
+
# @param pad [Boolean] if set to _true_ and the number is positive, a space will be placed bebore the '0x'
|
66
|
+
# @return [String]
|
67
|
+
def to_hex(pad: false)
|
68
|
+
m, e = Math.frexp(abs)
|
69
|
+
e -= 1
|
70
|
+
s = m > 0 ? (m * 2**53).to_i.to_s(16) : '0'*14
|
71
|
+
"#{negative? ? '-' : (pad ? ' ' : '')}0x#{s[0]}.#{s[1..]}p#{e.negative? ? '-' : '+'}#{e.abs}"
|
72
|
+
end
|
73
|
+
|
74
|
+
# Convert the _float64_ value to a _float32_ following the `round` direction, then convert the _float32_ to a hexadecimal string in C99 style
|
75
|
+
# @param pad [Boolean] if set to _true_ and the number is positive, a space will be placed bebore the '0x'
|
76
|
+
# @param round [Symbol] _float64_ to _float32_ rounding direction, either `:nearest`, `:up`, `:down`, or `:zero`
|
77
|
+
# @return [String]
|
78
|
+
def to_hexf(pad: false, round: :nearest)
|
79
|
+
m, e = Math.frexp(abs)
|
80
|
+
e -= 1
|
81
|
+
m = (m * 2**53).to_i
|
82
|
+
rest = m & 0x000000001fffffff
|
83
|
+
m = (m & 0x001fffffe0000000) >> 29
|
84
|
+
addulp = case round
|
85
|
+
when :nearest, :rndn
|
86
|
+
r = (rest & 0x10000000) != 0
|
87
|
+
s = (rest & 0x0fffffff) != 0
|
88
|
+
r and (s or (m & 1) == 1)
|
89
|
+
when :up, :rndu
|
90
|
+
not(negative? or rest.zero?)
|
91
|
+
when :down, :rndd
|
92
|
+
negative? and not(rest.zero?)
|
93
|
+
when :zero, :rndz
|
94
|
+
false
|
95
|
+
else
|
96
|
+
raise TypeError, "round musb be :nearest, :up, :down, or :zero, not #{round}"
|
97
|
+
end
|
98
|
+
if addulp then
|
99
|
+
m += 1
|
100
|
+
unless (m & 0x1000000).zero? then
|
101
|
+
m >>= 1
|
102
|
+
e += 1
|
103
|
+
end
|
104
|
+
end
|
105
|
+
s = m > 0 ? (m << 1).to_s(16) : '0'*6
|
106
|
+
"#{negative? ? '-' : (pad ? ' ' : '')}0x#{s[0]}.#{s[1..]}p#{e.negative? ? '-' : '+'}#{e.abs}f"
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
data/sollya.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.expand_path('lib/sollya/version.rb', __dir__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = 'sollya'
|
5
|
+
s.version = Sollya::VERSION
|
6
|
+
s.date = Time.now.strftime '%Y-%m-%d'
|
7
|
+
s.summary = 'Ruby bindings to the Sollya and MPFR libraries.'
|
8
|
+
s.license = 'GPL-3.0-or-later'
|
9
|
+
s.authors = ['Théotime Bollengier']
|
10
|
+
s.email = 'theotime.bollengier@ensta-bretagne.fr'
|
11
|
+
s.homepage = 'https://gitlab.ensta-bretagne.fr/bollenth/sollya.rb'
|
12
|
+
s.extensions = ['ext/extconf.rb']
|
13
|
+
s.files = [
|
14
|
+
'LICENSE',
|
15
|
+
'README.md',
|
16
|
+
'sollya.gemspec',
|
17
|
+
'ext/mpfr_rb.c',
|
18
|
+
'ext/sollya_rb.c',
|
19
|
+
'ext/extconf.rb',
|
20
|
+
'lib/sollya.rb',
|
21
|
+
'lib/sollya/version.rb',
|
22
|
+
'.yardopts'
|
23
|
+
]
|
24
|
+
s.required_ruby_version = '>= 2.7'
|
25
|
+
end
|
26
|
+
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sollya
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Théotime Bollengier
|
8
|
+
bindir: bin
|
9
|
+
cert_chain: []
|
10
|
+
date: 2025-02-07 00:00:00.000000000 Z
|
11
|
+
dependencies: []
|
12
|
+
email: theotime.bollengier@ensta-bretagne.fr
|
13
|
+
executables: []
|
14
|
+
extensions:
|
15
|
+
- ext/extconf.rb
|
16
|
+
extra_rdoc_files: []
|
17
|
+
files:
|
18
|
+
- ".yardopts"
|
19
|
+
- LICENSE
|
20
|
+
- README.md
|
21
|
+
- ext/extconf.rb
|
22
|
+
- ext/mpfr_rb.c
|
23
|
+
- ext/sollya_rb.c
|
24
|
+
- lib/sollya.rb
|
25
|
+
- lib/sollya/version.rb
|
26
|
+
- sollya.gemspec
|
27
|
+
homepage: https://gitlab.ensta-bretagne.fr/bollenth/sollya.rb
|
28
|
+
licenses:
|
29
|
+
- GPL-3.0-or-later
|
30
|
+
metadata: {}
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '2.7'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubygems_version: 3.6.2
|
46
|
+
specification_version: 4
|
47
|
+
summary: Ruby bindings to the Sollya and MPFR libraries.
|
48
|
+
test_files: []
|