float-formats 0.2.0 → 0.2.1

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.
data/TODO.txt ADDED
@@ -0,0 +1,18 @@
1
+ * replace Nio by Numerals
2
+ * next_after method
3
+ * research if the double rounding that's happening in nio_read_neutral may be a real problem
4
+ (the neutral is first rounded to decimal_digits_necessary, then to a FP datum)
5
+ * define small DSL to define formats
6
+ * Consider unifying the Format / Num nomenclature:
7
+ denormal vs. subnormal
8
+ min_normalized_value vs. minimum_normal
9
+ max_value vs. maximum_finite
10
+ min_value vs. minimum_nonzero
11
+ radix_min_exp(:scientific_significand) vs. emin
12
+ radix_max_exp(:scientific_significand) vs. emax
13
+ radix_min_exp(:integral_significand) vs. etiny
14
+ radix_max_exp(:integral_significand) vs. etop
15
+ significand vs. coefficient
16
+ minimum_normalized_integral_significand vs. minimum_normalized_coefficient
17
+ maximum_integral_significand vs. maximum_coefficient
18
+ radix_power -- int_radix_power
data/expand.rb ADDED
@@ -0,0 +1,175 @@
1
+ require 'irb/ruby-lex'
2
+ require 'stringio'
3
+
4
+ class MimickIRB < RubyLex
5
+ attr_accessor :started
6
+
7
+ class Continue < StandardError; end
8
+ class Empty < StandardError; end
9
+
10
+ def initialize
11
+ super
12
+ set_input(StringIO.new)
13
+ end
14
+
15
+ def run(str,line_no=nil)
16
+ obj = nil
17
+ @io << str
18
+ @io.rewind
19
+ unless l = lex
20
+ raise Empty if @line == ''
21
+ else
22
+ case l.strip
23
+ when "reset"
24
+ @line = ""
25
+ when "time"
26
+ @line = "puts %{You started \#{IRBalike.started.since} ago.}"
27
+ else
28
+ @line << l << "\n"
29
+ if @ltype or @continue or @indent > 0
30
+ raise Continue
31
+ end
32
+ end
33
+ end
34
+ unless @line.empty?
35
+ obj = eval @line, TOPLEVEL_BINDING
36
+ end
37
+ @line = ''
38
+ #@line_no = line_no if line_no
39
+ @exp_line_no = line_no || @line_no
40
+
41
+ @indent = 0
42
+ @indent_stack = []
43
+
44
+ $stdout.rewind
45
+ output = $stdout.read
46
+ $stdout.truncate(0)
47
+ $stdout.rewind
48
+ [output, obj]
49
+ rescue Object => e
50
+ case e when Empty, Continue
51
+ else @line = ""
52
+ end
53
+ raise e
54
+ ensure
55
+ set_input(StringIO.new)
56
+ end
57
+
58
+ end
59
+
60
+ # TO DO: output of blocks is gathered and shown at the end of the block
61
+ # when lines in a block have ->, they should be rememberd, and when
62
+ # output is generated when closing the block, each output line should
63
+ # be appended to the in-block -> lines before showing lines after the block.
64
+
65
+
66
+ class ExampleExpander
67
+ def initialize(sep="# -> ", align=51)
68
+ @sep = sep
69
+ @align = align
70
+ @irb = MimickIRB.new
71
+ @output = ""
72
+ end
73
+ def add_line(line,line_num=nil)
74
+ line = line.chomp
75
+ line = $1 if /(.+)#{@sep}.*/.match line
76
+ $stdout = StringIO.new
77
+ begin
78
+ out,obj = @irb.run(line, line_num)
79
+ @output << line_output(line,out)
80
+ rescue MimickIRB::Empty
81
+ @output << line_output(line)
82
+ rescue MimickIRB::Continue
83
+ @output << line_output(line)
84
+ rescue Object => e
85
+ #msg = "Exception : #{e.message}"
86
+ msg = "Exception : #{e.class}"
87
+ # msg = "#{e.class}: #{e.message}"
88
+ @output << line_output(line,msg)
89
+ STDERR.puts "#{msg}\n"
90
+ end
91
+ $stdout = STDOUT
92
+ end
93
+ def output
94
+ @output
95
+ end
96
+ def clear
97
+ @output = ""
98
+ end
99
+ protected
100
+ def line_output(line,output=nil)
101
+ if output
102
+ output = output.chomp
103
+ output = nil if output.strip.empty?
104
+ end
105
+ out = line.dup
106
+ if output
107
+ line_size = line.size
108
+ output.split("\n").each do |out_line|
109
+ out << " "*[0,(@align-line_size)].max + @sep + out_line
110
+ out << "\n"
111
+ line_size = 0
112
+ end
113
+ end
114
+ out
115
+ end
116
+ end
117
+
118
+
119
+ def expand_text(txt,non_code_block_prefix=nil) # text with indented blocks of code
120
+ exex = ExampleExpander.new
121
+ indent = nil
122
+
123
+ txt_out = ""
124
+
125
+ line_num = 0
126
+ accum = ""
127
+ skip_until_blank = false
128
+ disabled = false
129
+ txt.split("\n").each do |line|
130
+ line_num += 1
131
+ code = false
132
+ line.chomp!
133
+
134
+ if skip_until_blank
135
+ if line.strip.empty?
136
+ skip_until_blank = false
137
+ end
138
+ else
139
+
140
+ unless line.strip.empty? || disabled
141
+ line_indent = /^\s*/.match(line)[0]
142
+ indent ||= line_indent
143
+ indent = line_indent if line_indent.size < indent.size
144
+ if line[line_indent.size,1]=='*'
145
+ inner_indent = /^\s*/.match(line[line_indent.size+1..-1])[0]
146
+ indent += '*'+inner_indent
147
+ else
148
+ if line_indent.size > indent.size
149
+ code = true
150
+ end
151
+ end
152
+ end
153
+ if code
154
+ exex.add_line line, line_num
155
+ line = exex.output.chomp
156
+ exex.clear
157
+ else
158
+ disabled = true if line[0,7]=="EXPAND-"
159
+ disabled = false if line[0,7]=="EXPAND+"
160
+ skip_until_blank = true if line[0,1]==non_code_block_prefix
161
+ end
162
+ end
163
+ txt_out << line + "\n"
164
+ end
165
+ txt_out
166
+
167
+ end
168
+
169
+ require 'rubygems'
170
+
171
+ require File.dirname(__FILE__) + '/lib/float-formats'
172
+ require File.dirname(__FILE__) + '/lib/float-formats/native.rb'
173
+ include Flt
174
+
175
+ puts expand_text(File.read(ARGV.shift),"[")
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'float-formats/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "float-formats"
8
+ spec.version = Flt::Frmts::VERSION
9
+ spec.authors = ["Javier Goizueta"]
10
+ spec.email = ["jgoizueta@gmail.com"]
11
+ spec.summary = %q{Floating-Point Formats}
12
+ spec.description = %q{Floating-Point Formats}
13
+ spec.homepage = "https://github.com/jgoizueta/float-formats"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'flt', ">= 1.1.2"
22
+ spec.add_dependency 'nio', ">= 0.2.4"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+ end
@@ -112,6 +112,7 @@ class Bytes < DelegateClass(String)
112
112
  @bytes = bytes.pack("C*")
113
113
  else
114
114
  @bytes = bytes.to_str
115
+ @bytes.force_encoding("BINARY") if @bytes.respond_to?(:force_encoding)
115
116
  end
116
117
  @bytes.force_encoding(Encoding::BINARY) if RUBY_VERSION>="1.9.0"
117
118
  super @bytes
@@ -178,7 +179,7 @@ class Bytes < DelegateClass(String)
178
179
  __setobj__ @bytes
179
180
  self
180
181
  end
181
-
182
+
182
183
  def reverse_byte_bits
183
184
  dup.reverse_byte_bits!
184
185
  end
@@ -186,6 +187,7 @@ class Bytes < DelegateClass(String)
186
187
  # Reverse the order of the nibbles in each byte.
187
188
  def reverse_byte_nibbles!
188
189
  w = ""
190
+ w.force_encoding("BINARY") if w.respond_to?(:force_encoding)
189
191
  @bytes.each_byte do |b|
190
192
  w << ((b >> 4)|((b&0xF)<<4))
191
193
  end
@@ -200,6 +202,7 @@ class Bytes < DelegateClass(String)
200
202
  # reverse the order of bytes in 16-bit words
201
203
  def reverse_byte_pairs!
202
204
  w = ""
205
+ w.force_encoding("BINARY") if w.respond_to?(:force_encoding)
203
206
  (0...@bytes.size).step(2) do |i|
204
207
  w << @bytes[i+1]
205
208
  w << @bytes[i]
@@ -461,4 +464,4 @@ end
461
464
 
462
465
 
463
466
 
464
- end
467
+ end
@@ -53,13 +53,13 @@ class FormatBase
53
53
  (args.first.kind_of?(Integer) && args[1].kind_of?(Integer))
54
54
  sign,significand,exponent,normalize = args
55
55
  if normalize.nil?
56
- if [nil,true,false].include?(exponent)
56
+ if [nil,true,false].include?(exponent)
57
57
  normalize = exponent
58
58
  exponent = significand
59
59
  significand = sign.abs
60
- sign = sign<0 ? -1 : +1
60
+ sign = sign<0 ? -1 : +1
61
61
  end
62
- end
62
+ end
63
63
  @sign,@significand,@exponent = self.class.canonicalized(sign,significand,exponent,normalize)
64
64
  else
65
65
  v = form_class.nan
@@ -84,7 +84,7 @@ class FormatBase
84
84
  raise "Too many arguments for FormatBase constructor" if args.size>1
85
85
  when Symbol
86
86
  if args.first.to_s[0..3]!='from'
87
- args = ["from_#{args.first}".to_sym] + args[1..-1]
87
+ args = ["from_#{args.first}".to_sym] + args[1..-1]
88
88
  end
89
89
  v = form_class.send(*args)
90
90
  end
@@ -826,19 +826,19 @@ class FormatBase
826
826
  return_value s,f,e
827
827
 
828
828
  end
829
-
829
+
830
830
  def self.from(*args)
831
831
  new(*args)
832
832
  end
833
-
833
+
834
834
  def self.from_bytes(b)
835
835
  return_value(*unpack(b))
836
836
  end
837
-
837
+
838
838
  def self.from_hex(hex)
839
839
  from_bytes Bytes.from_hex(hex)
840
840
  end
841
-
841
+
842
842
  def self.from_number(v, mode=:approx)
843
843
  if v.is_a?(Flt::Num) && v.num_class.radix==self.radix
844
844
  self.num(v)
@@ -847,11 +847,11 @@ class FormatBase
847
847
  nio_read(v.nio_write(fmt),fmt)
848
848
  end
849
849
  end
850
-
850
+
851
851
  def self.from_text(txt, fmt=Nio::Fmt.default) # ?
852
852
  nio_read(txt,fmt)
853
853
  end
854
-
854
+
855
855
  def self.join(sign,significand,exponent)
856
856
  self.new sign,significand,exponent
857
857
  end
@@ -871,7 +871,7 @@ class FormatBase
871
871
  end
872
872
  from_bytes v
873
873
  end
874
-
874
+
875
875
  # Defines a floating-point number from a text representation of the
876
876
  # encoded integral value in a given base.
877
877
  # Returns a Value.
@@ -909,7 +909,7 @@ class FormatBase
909
909
  end
910
910
  h
911
911
  end
912
-
912
+
913
913
  # Produce an encoded floating point value using hash of the internal field values.
914
914
  # Returns a Value.
915
915
  def self.pack_fields_hash(h)
@@ -1947,4 +1947,3 @@ end
1947
1947
 
1948
1948
 
1949
1949
  end
1950
-
@@ -1,9 +1,5 @@
1
1
  module Flt
2
- module FORMATS_VERSION #:nodoc:
3
- MAJOR = 0
4
- MINOR = 2
5
- TINY = 0
6
-
7
- STRING = [MAJOR, MINOR, TINY].join('.')
2
+ module Frmts
3
+ VERSION = '0.2.1'
8
4
  end
9
5
  end
@@ -1,11 +1,17 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'test_helper.rb'))
2
2
 
3
3
  include Flt
4
4
 
5
5
  class TestArithmetic < Test::Unit::TestCase
6
6
 
7
7
  def setup
8
- @all_types = Flt.constants.map{|c| Flt.class_eval(c.to_s)}.select{|c| (c < Flt::FormatBase)&& !(c.to_s.downcase.include?('format'))}
8
+ @all_types = Flt.constants.map { |constant|
9
+ Flt.const_get(constant)
10
+ }.select{ |cls|
11
+ cls.class == Class &&
12
+ cls < Flt::FormatBase &&
13
+ !cls.to_s.downcase.include?('format')
14
+ }
9
15
  end
10
16
 
11
17
  def test_precision
data/test/test_bytes.rb CHANGED
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'test_helper.rb'))
2
2
  require 'yaml'
3
3
 
4
4
  include Flt
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'test_helper.rb'))
2
2
  require 'yaml'
3
3
 
4
4
  include Flt
data/test/test_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'rubygems'
2
2
  require 'test/unit'
3
+ $: << "." unless $:.include?(".") # for Ruby 1.9.2
3
4
  $:.unshift File.dirname(__FILE__) + '/../lib'
4
5
  require File.dirname(__FILE__) + '/../lib/float-formats'
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'test_helper.rb'))
2
2
  require File.dirname(__FILE__) + '/../lib/float-formats/native.rb'
3
3
  require 'yaml'
4
4
 
metadata CHANGED
@@ -1,103 +1,95 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: float-formats
3
- version: !ruby/object:Gem::Version
4
- version: 0.2.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
5
  platform: ruby
6
- authors:
6
+ authors:
7
7
  - Javier Goizueta
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
-
12
- date: 2009-08-06 00:00:00 +02:00
13
- default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
11
+ date: 2015-03-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
16
14
  name: flt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.1.2
17
20
  type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
21
24
  - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 1.0.0
24
- version:
25
- - !ruby/object:Gem::Dependency
25
+ - !ruby/object:Gem::Version
26
+ version: 1.1.2
27
+ - !ruby/object:Gem::Dependency
26
28
  name: nio
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
30
- requirements:
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
31
  - - ">="
32
- - !ruby/object:Gem::Version
32
+ - !ruby/object:Gem::Version
33
33
  version: 0.2.4
34
- version:
35
- - !ruby/object:Gem::Dependency
36
- name: nio
37
34
  type: :runtime
38
- version_requirement:
39
- version_requirements: !ruby/object:Gem::Requirement
40
- requirements:
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
41
38
  - - ">="
42
- - !ruby/object:Gem::Version
43
- version: 0.2.0
44
- version:
45
- - !ruby/object:Gem::Dependency
46
- name: flt
47
- type: :runtime
48
- version_requirement:
49
- version_requirements: !ruby/object:Gem::Requirement
50
- requirements:
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.4
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
51
59
  - - ">="
52
- - !ruby/object:Gem::Version
53
- version: 1.0.0
54
- version:
55
- - !ruby/object:Gem::Dependency
56
- name: bones
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
57
62
  type: :development
58
- version_requirement:
59
- version_requirements: !ruby/object:Gem::Requirement
60
- requirements:
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
61
66
  - - ">="
62
- - !ruby/object:Gem::Version
63
- version: 2.1.1
64
- version:
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
65
69
  description: Floating-Point Formats
66
- email: javier@goizueta.info
70
+ email:
71
+ - jgoizueta@gmail.com
67
72
  executables: []
68
-
69
73
  extensions: []
70
-
71
- extra_rdoc_files:
72
- - History.txt
73
- - License.txt
74
- - README.txt
75
- files:
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - Gemfile.lock
76
79
  - History.txt
77
- - License.txt
78
- - Manifest.txt
79
- - README.txt
80
+ - LICENSE
81
+ - README.md
80
82
  - Rakefile
83
+ - TODO.txt
84
+ - expand.rb
85
+ - float-formats.gemspec
81
86
  - lib/float-formats.rb
82
87
  - lib/float-formats/bytes.rb
83
88
  - lib/float-formats/classes.rb
84
89
  - lib/float-formats/formats.rb
85
90
  - lib/float-formats/native.rb
86
91
  - lib/float-formats/version.rb
87
- - setup.rb
88
- - tasks/ann.rake
89
- - tasks/bones.rake
90
- - tasks/gem.rake
91
- - tasks/git.rake
92
- - tasks/manifest.rake
93
- - tasks/notes.rake
94
- - tasks/post_load.rake
95
- - tasks/rdoc.rake
96
- - tasks/rubyforge.rake
97
- - tasks/setup.rb
98
- - tasks/spec.rake
99
- - tasks/svn.rake
100
- - tasks/test.rake
92
+ - rdoc/Flt.html
101
93
  - test/gen_test_data.rb
102
94
  - test/test_arithmetic.rb
103
95
  - test/test_bytes.rb
@@ -105,46 +97,35 @@ files:
105
97
  - test/test_float_formats.rb
106
98
  - test/test_helper.rb
107
99
  - test/test_native-float.rb
108
- has_rdoc: true
109
- homepage: http://float-formats.rubyforge.org
110
- licenses: []
111
-
100
+ homepage: https://github.com/jgoizueta/float-formats
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
112
104
  post_install_message:
113
- rdoc_options:
114
- - --main
115
- - README.txt
116
- - --title
117
- - Float-Formats Documentation
118
- - --opname
119
- - index.html
120
- - --line-numbers
121
- - --inline-source
122
- - --main
123
- - README.txt
124
- require_paths:
105
+ rdoc_options: []
106
+ require_paths:
125
107
  - lib
126
- required_ruby_version: !ruby/object:Gem::Requirement
127
- requirements:
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
128
110
  - - ">="
129
- - !ruby/object:Gem::Version
130
- version: "0"
131
- version:
132
- required_rubygems_version: !ruby/object:Gem::Requirement
133
- requirements:
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
134
115
  - - ">="
135
- - !ruby/object:Gem::Version
136
- version: "0"
137
- version:
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
138
118
  requirements: []
139
-
140
- rubyforge_project: float-formats
141
- rubygems_version: 1.3.3
119
+ rubyforge_project:
120
+ rubygems_version: 2.2.2
142
121
  signing_key:
143
- specification_version: 3
122
+ specification_version: 4
144
123
  summary: Floating-Point Formats
145
- test_files:
124
+ test_files:
125
+ - test/gen_test_data.rb
146
126
  - test/test_arithmetic.rb
147
127
  - test/test_bytes.rb
128
+ - test/test_data.yaml
148
129
  - test/test_float_formats.rb
149
130
  - test/test_helper.rb
150
131
  - test/test_native-float.rb