flt 1.1.1 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +8 -0
- data/README.txt +16 -0
- data/lib/flt/sugar.rb +10 -0
- data/lib/flt/support.rb +1 -1
- data/lib/flt/version.rb +1 -1
- data/test/helper.rb +1 -0
- data/test/test_dectest.rb +1 -1
- data/test/test_sugar.rb +39 -0
- metadata +29 -9
data/History.txt
CHANGED
data/README.txt
CHANGED
@@ -385,6 +385,22 @@ because it could cause conflicts with other extensions of these classes.
|
|
385
385
|
puts 0.1.split.inspect # -> [1, 7205759403792794, -56]
|
386
386
|
puts (-0.1).sign # -> -1
|
387
387
|
|
388
|
+
A shortcut notation for DecNum is defined in this file (based on an idea by coderrr[http://coderrr.wordpress.com]
|
389
|
+
which allows exact definitions with almost literal decimal syntax (note the underscore after the dot.)
|
390
|
+
|
391
|
+
puts 10._123456789123456789 # -> 10.123456789123456789
|
392
|
+
|
393
|
+
Additional underscores can be used to separate digits at any place except before the decimal point:
|
394
|
+
|
395
|
+
puts 100_000._000_001 # -> 100000.000001
|
396
|
+
puts 100_000._000_001.class # -> Flt::DecNum
|
397
|
+
|
398
|
+
But note that 100_000.000_001 is a valid Float (it's not a DecNum because there isn't an underscore just after the
|
399
|
+
decimal point):
|
400
|
+
|
401
|
+
puts 100_000.000_001 # -> 100000.000001
|
402
|
+
puts 100_000.000_001.class # -> Float
|
403
|
+
|
388
404
|
== Error analysis
|
389
405
|
|
390
406
|
The DecNum#ulp() method returns the value of a "unit in the last place" for a given number under
|
data/lib/flt/sugar.rb
CHANGED
@@ -100,3 +100,13 @@ module BigDecimal::Math
|
|
100
100
|
end
|
101
101
|
end
|
102
102
|
|
103
|
+
# Shortcut to define DecNums, e.g. 1._234567890123456789 produces Flt::DecNum('1.234567890123456789')
|
104
|
+
# Based on http://coderrr.wordpress.com/2009/12/22/get-arbitrarily-precise-bigdecimals-in-ruby-for-just-one-extra-character/
|
105
|
+
class Integer
|
106
|
+
def method_missing(m, *a, &b)
|
107
|
+
return Flt::DecNum("#{self}.#{$1.tr('_','')}") if m.to_s =~ /^_(\d[_\d]*)$/
|
108
|
+
super
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
|
data/lib/flt/support.rb
CHANGED
@@ -483,7 +483,7 @@ module Flt
|
|
483
483
|
float = true
|
484
484
|
context = BinNum::FloatContext
|
485
485
|
end
|
486
|
-
x = context.num_class.context(context) do |
|
486
|
+
x = context.num_class.context(context) do |local_context|
|
487
487
|
local_context.rounding = round_mode
|
488
488
|
Num.convert_exact(Num[eb].Num(sign, f, e), local_context.num_class, local_context)
|
489
489
|
end
|
data/lib/flt/version.rb
CHANGED
data/test/helper.rb
CHANGED
data/test/test_dectest.rb
CHANGED
@@ -148,7 +148,7 @@ class TestBasic < Test::Unit::TestCase
|
|
148
148
|
if funct
|
149
149
|
# do test
|
150
150
|
msg = " Test #{id}: #{funct}(#{valstemp.join(',')}) = #{ans}"
|
151
|
-
|
151
|
+
File.open('dectests.txt','a'){|f| f.puts msg}
|
152
152
|
expected = result = result_flags = nil
|
153
153
|
DecNum.local_context do |context|
|
154
154
|
context.flags.clear!
|
data/test/test_sugar.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__),'helper.rb'))
|
2
|
+
require File.dirname(__FILE__) + '/../lib/flt/sugar'
|
3
|
+
|
4
|
+
class TestSugar < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_pseudo_literals
|
7
|
+
assert_equal Flt::DecNum, 10._3223.class
|
8
|
+
assert_equal Flt::DecNum('10.3223'), 10._3223
|
9
|
+
assert_equal Flt::DecNum('3.01234567890123456789012345678901234567890123456789'),
|
10
|
+
3._01234567890123456789012345678901234567890123456789
|
11
|
+
assert_equal Flt::DecNum('-3.01234567890123456789012345678901234567890123456789'),
|
12
|
+
-3._01234567890123456789012345678901234567890123456789
|
13
|
+
assert_equal Flt::DecNum('123456789123.01234567890123456789012345678901234567890123456789'),
|
14
|
+
123456789123._01234567890123456789012345678901234567890123456789
|
15
|
+
assert_equal Flt::DecNum('-123456789123.01234567890123456789012345678901234567890123456789'),
|
16
|
+
-123456789123._01234567890123456789012345678901234567890123456789
|
17
|
+
assert_raise(NoMethodError){3._x}
|
18
|
+
assert_raise(NoMethodError){3._3233x}
|
19
|
+
assert_raise(NoMethodError){3._3233x34333}
|
20
|
+
assert_raise(NoMethodError){3.__}
|
21
|
+
assert_raise(NoMethodError){3._}
|
22
|
+
assert_equal Flt::DecNum, 10._32_23.class
|
23
|
+
assert_equal Flt::DecNum('10.3223'), 10._32_23
|
24
|
+
assert_equal Flt::DecNum('3.01234567890123456789012345678901234567890123456789'),
|
25
|
+
3._012345_678901_234567_890123_456789_012345_678901_234567_89
|
26
|
+
assert_equal Flt::DecNum('-3.01234567890123456789012345678901234567890123456789'),
|
27
|
+
-3._012_345_678_901_234_567_890_123_456_789_012_345_678_901_234_567_89
|
28
|
+
assert_equal Flt::DecNum('123456789123.01234567890123456789012345678901234567890123456789'),
|
29
|
+
123456789123._0123456789_0123456789_0123456789_0123456789_0123456789
|
30
|
+
assert_equal Flt::DecNum('-123456789123.01234567890123456789012345678901234567890123456789'),
|
31
|
+
-123456789123._0123456789_0123456789_0123456789_0123456789_0123456789
|
32
|
+
assert_equal Flt::DecNum('10.0'), 10._0
|
33
|
+
assert_equal Flt::DecNum('10.000'), 10._000
|
34
|
+
assert_equal Flt::DecNum('10.000000'), 10._000000
|
35
|
+
assert_equal Flt::DecNum('10.000000'), 10._000_000
|
36
|
+
assert_equal Flt::DecNum('100000.000001'), 100_000._000_001
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
- 2
|
10
|
+
version: 1.1.2
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Javier Goizueta
|
@@ -9,19 +15,25 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
18
|
+
date: 2010-06-01 00:00:00 +02:00
|
13
19
|
default_executable:
|
14
20
|
dependencies:
|
15
21
|
- !ruby/object:Gem::Dependency
|
16
22
|
name: bones
|
17
|
-
|
18
|
-
|
19
|
-
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
20
26
|
requirements:
|
21
27
|
- - ">="
|
22
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 9
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 1
|
33
|
+
- 1
|
23
34
|
version: 2.1.1
|
24
|
-
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
25
37
|
description: Floating Point Numbers
|
26
38
|
email: javier@goizueta.info
|
27
39
|
executables: []
|
@@ -84,6 +96,7 @@ files:
|
|
84
96
|
- test/test_num_constructor.rb
|
85
97
|
- test/test_odd_even.rb
|
86
98
|
- test/test_round.rb
|
99
|
+
- test/test_sugar.rb
|
87
100
|
- test/test_to_int.rb
|
88
101
|
- test/test_to_rf.rb
|
89
102
|
- test/test_tol.rb
|
@@ -108,21 +121,27 @@ rdoc_options:
|
|
108
121
|
require_paths:
|
109
122
|
- lib
|
110
123
|
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
none: false
|
111
125
|
requirements:
|
112
126
|
- - ">="
|
113
127
|
- !ruby/object:Gem::Version
|
128
|
+
hash: 3
|
129
|
+
segments:
|
130
|
+
- 0
|
114
131
|
version: "0"
|
115
|
-
version:
|
116
132
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
none: false
|
117
134
|
requirements:
|
118
135
|
- - ">="
|
119
136
|
- !ruby/object:Gem::Version
|
137
|
+
hash: 3
|
138
|
+
segments:
|
139
|
+
- 0
|
120
140
|
version: "0"
|
121
|
-
version:
|
122
141
|
requirements: []
|
123
142
|
|
124
143
|
rubyforge_project: flt
|
125
|
-
rubygems_version: 1.3.
|
144
|
+
rubygems_version: 1.3.7
|
126
145
|
signing_key:
|
127
146
|
specification_version: 3
|
128
147
|
summary: Floating Point Numbers
|
@@ -142,6 +161,7 @@ test_files:
|
|
142
161
|
- test/test_num_constructor.rb
|
143
162
|
- test/test_odd_even.rb
|
144
163
|
- test/test_round.rb
|
164
|
+
- test/test_sugar.rb
|
145
165
|
- test/test_to_int.rb
|
146
166
|
- test/test_to_rf.rb
|
147
167
|
- test/test_tol.rb
|