flt 1.4.7 → 1.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a82d3975aca56adcdeefe93da5fed92f8adb4159
4
- data.tar.gz: 2155cf086a59dc59ce6f01e19b00b546c58a77e0
3
+ metadata.gz: 84bc5146381ff127868320f22bb421bc8d1ad16a
4
+ data.tar.gz: 66f6fc689cb2297638127a1217fe4ee3e19b8aa7
5
5
  SHA512:
6
- metadata.gz: d1b9044b8b0deafd52eac4f3b481bbd360e9fd70ed7fb70f7489ed7744cf40fce81218275b402c7547d57d7fabc4bc2cde078ed5faebcb21a814be559edd7ed8
7
- data.tar.gz: 07196b48dcb47e16a745f0488b2cddb8b60e91056de66d020a12bfb9e5cbd27a57c5f36b21b9a7f8cc69815caa04a8708c999df6cca70a5f337642c32000a0aa
6
+ metadata.gz: 10bc77b0f1ab349a36abcf4512aa9a16aecc886e4da95ec2c6edc180c1c00b74113f98d2673f1c2aaae3cf7856b1be8291be0e59f2e9082f541cceae8c5f2130
7
+ data.tar.gz: 1279653a5e9471834c3f48819db261bcb5cbf4e749ded3cc0d039a3e4bb3572eb20323b90fb0058739630b6bf8cd6eb36a6132713960da8f7b6c2919fca79122
@@ -1,3 +1,8 @@
1
+ == 1.5.0 2015-04-06
2
+
3
+ * New feature
4
+ - normalized attribute of context applies normalization to all results
5
+
1
6
  == 1.4.7 2015-04-05
2
7
 
3
8
  * Bugfix
@@ -422,6 +422,7 @@ class Num < Numeric
422
422
  # taken as emin and emax=1-emin. Such limits comply with IEEE 754-2008
423
423
  # * :capitals : (true or false) to use capitals in text representations
424
424
  # * :clamp : (true or false) enables clamping
425
+ # * :normalized : (true or false) normalizes all results
425
426
  #
426
427
  # See also the context constructor method Flt::Num.Context().
427
428
  def initialize(num_class, *options)
@@ -441,6 +442,7 @@ class Num < Numeric
441
442
  @coercible_type_handlers = num_class.base_coercible_types.dup
442
443
  @conversions = num_class.base_conversions.dup
443
444
  @angle = :rad # angular units: :rad (radians) / :deg (degrees) / :grad (gradians)
445
+ @normalized = false
444
446
  end
445
447
  assign options.first
446
448
 
@@ -515,7 +517,11 @@ class Num < Numeric
515
517
  @num_class.int_div_radix_power(x,n)
516
518
  end
517
519
 
518
- attr_accessor :rounding, :emin, :emax, :flags, :traps, :ignored_flags, :capitals, :clamp, :angle
520
+ attr_accessor :rounding, :emin, :emax, :flags, :traps, :ignored_flags, :capitals, :clamp, :angle, :normalized
521
+
522
+ def normalized?
523
+ normalized
524
+ end
519
525
 
520
526
  # TODO: consider the convenience of adding accessors of this kind:
521
527
  # def rounding(new_rounding=nil)
@@ -630,9 +636,10 @@ class Num < Numeric
630
636
  @emin = options[:emin] unless options[:emin].nil?
631
637
  @emax = options[:emax] unless options[:emax].nil?
632
638
  @capitals = options[:capitals ] unless options[:capitals ].nil?
633
- @clamp = options[:clamp ] unless options[:clamp ].nil?
634
- @exact = options[:exact ] unless options[:exact ].nil?
635
- @angle = options[:angle ] unless options[:angle ].nil?
639
+ @clamp = options[:clamp] unless options[:clamp].nil?
640
+ @exact = options[:exact] unless options[:exact].nil?
641
+ @angle = options[:angle] unless options[:angle].nil?
642
+ @normalized = options[:normalized] unless options[:normalized].nil?
636
643
  update_precision
637
644
  if options[:extra_precision] && !@exact
638
645
  @precision += options[:extra_precision]
@@ -660,6 +667,7 @@ class Num < Numeric
660
667
  @coercible_type_handlers = other.coercible_type_handlers.dup
661
668
  @conversions = other.conversions.dup
662
669
  @angle = other.angle
670
+ @normalized = other.normalized
663
671
  end
664
672
 
665
673
  def dup
@@ -1477,18 +1485,19 @@ class Num < Numeric
1477
1485
  # * :base is the numeric base of the input, 10 by default.
1478
1486
  def initialize(*args)
1479
1487
  options = args.pop if args.last.is_a?(Hash)
1480
- context = args.pop if args.size>0 && (args.last.kind_of?(ContextBase) || args.last.nil?)
1481
- context ||= options && options.delete(:context)
1488
+ options ||= {}
1489
+ context = args.pop if args.size > 0 && (args.last.kind_of?(ContextBase) || args.last.nil?)
1490
+ context ||= options.delete(:context)
1482
1491
  mode = args.pop if args.last.is_a?(Symbol) && ![:inf, :nan, :snan].include?(args.last)
1483
1492
  args = args.first if args.size==1 && args.first.is_a?(Array)
1484
- if args.empty? && options
1493
+ if args.empty? && !options.empty?
1485
1494
  args = [options.delete(:sign)||+1,
1486
1495
  options.delete(:coefficient) || 0,
1487
1496
  options.delete(:exponent) || 0]
1488
1497
  end
1489
- mode ||= options && options.delete(:mode)
1490
- base = (options && options.delete(:base))
1491
- context = options if context.nil? && options && !options.empty?
1498
+ mode ||= options.delete(:mode)
1499
+ base = options.delete(:base)
1500
+ context = options if context.nil? && !options.empty?
1492
1501
  context = define_context(context)
1493
1502
 
1494
1503
  case args.size
@@ -3593,6 +3602,26 @@ class Num < Numeric
3593
3602
  return ans
3594
3603
  end
3595
3604
 
3605
+ if context.normalized?
3606
+ exp = @exp
3607
+ coeff = @coeff
3608
+ if self_is_subnormal
3609
+ if exp > context.etiny
3610
+ coeff = num_class.int_mult_radix_power(coeff, exp - context.etiny)
3611
+ exp = context.etiny
3612
+ end
3613
+ else
3614
+ min_normal_coeff = context.minimum_normalized_coefficient
3615
+ while coeff < min_normal_coeff
3616
+ coeff = num_class.int_mult_radix_power(coeff, 1)
3617
+ exp -= 1
3618
+ end
3619
+ end
3620
+ if exp != @exp || coeff != @coeff
3621
+ return Num(@sign, coeff, exp)
3622
+ end
3623
+ end
3624
+
3596
3625
  if context.clamp? && @exp>etop
3597
3626
  context.exception Clamped
3598
3627
  self_padded = num_class.int_mult_radix_power(@coeff, @exp-etop)
@@ -3600,7 +3629,6 @@ class Num < Numeric
3600
3629
  end
3601
3630
 
3602
3631
  return Num(self)
3603
-
3604
3632
  end
3605
3633
 
3606
3634
  # adjust payload of a NaN to the context
@@ -1,3 +1,3 @@
1
1
  module Flt
2
- VERSION = "1.4.7"
2
+ VERSION = "1.5.0"
3
3
  end
@@ -0,0 +1,67 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'helper.rb'))
2
+
3
+
4
+ class TestNormalized < Test::Unit::TestCase
5
+
6
+ include Flt
7
+
8
+ def setup
9
+ initialize_context
10
+ end
11
+
12
+ def test_giac_floating_point
13
+ BinNum.context.precision = 48
14
+ BinNum.context.rounding = :down
15
+ BinNum.context.normalized = true
16
+ x = (BinNum(4)/3-1)*3 - 1
17
+ assert_equal [-1, 140737488355328, -93], x.split
18
+
19
+ x = BinNum(11)/15
20
+ assert_equal [1, 206414982921147, -48], x.split
21
+ x *= BinNum('1E308')
22
+ assert_equal [1, 229644291251027, 975], x.split
23
+ end
24
+
25
+ def test_normalized_context
26
+ refute DecNum.context.normalized?
27
+ DecNum.context(normalized: true) do
28
+ assert DecNum.context.normalized?
29
+ assert_equal [1, 1, -1], DecNum('0.1').split
30
+ assert_equal [1, 100000000, -9], (+DecNum('0.1')).split
31
+ assert_equal [1, 100000000, -9], (DecNum('0.1')+0).split
32
+ assert_equal [1, 100000000, -9], (DecNum('0.1')/1).split
33
+ end
34
+ refute DecNum.context.normalized?
35
+ assert_equal [1, 1, -1], DecNum('0.1').split
36
+ assert_equal [1, 1, -1], (+DecNum('0.1')).split
37
+ assert_equal [1, 1, -1], (DecNum('0.1')+0).split
38
+ assert_equal [1, 1, -1], (DecNum('0.1')/1).split
39
+ context = DecNum.context(normalized: true)
40
+ assert context.normalized?
41
+ assert_equal [1, 100000000, -9], context.plus(DecNum('0.1')).split
42
+ assert_equal [1, 100000000, -9], context.add(DecNum('0.1'), 0).split
43
+ assert_equal [1, 100000000, -9], context.divide(DecNum('0.1'), 1).split
44
+ refute DecNum.context.normalized?
45
+ assert_equal [1, 1, -1], DecNum('0.1').split
46
+ assert_equal [1, 1, -1], (+DecNum('0.1')).split
47
+ assert_equal [1, 1, -1], (DecNum('0.1')+0).split
48
+ assert_equal [1, 1, -1], (DecNum('0.1')/1).split
49
+ end
50
+
51
+ def test_precision
52
+ x = (DecNum(4)/3 - 1)*3 - 1 # -1e-8
53
+ assert_equal [-1, 1, -8], x.split
54
+
55
+ DecNum.context.normalized = true
56
+ x = (DecNum(4)/3 - 1)*3 - 1 # -1.00000000e-8
57
+ assert_equal [-1, 100000000, -16], x.split
58
+
59
+ x = (BinNum(4)/3 - 1)*3 - 1 # -0x1p-52
60
+ assert_equal [-1, 1, -52], x.split
61
+
62
+ BinNum.context.normalized = true
63
+ x = (BinNum(4)/3 - 1)*3 - 1 # -0x1.0000000000000p-52
64
+ assert_equal [-1, 4503599627370496, -104], x.split
65
+ end
66
+
67
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flt
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.7
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Javier Goizueta
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-05 00:00:00.000000000 Z
11
+ date: 2015-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -99,6 +99,7 @@ files:
99
99
  - test/test_formatter.rb
100
100
  - test/test_hex_format.rb
101
101
  - test/test_multithreading.rb
102
+ - test/test_normalized.rb
102
103
  - test/test_num_constructor.rb
103
104
  - test/test_odd_even.rb
104
105
  - test/test_rationalizer.rb
@@ -157,6 +158,7 @@ test_files:
157
158
  - test/test_formatter.rb
158
159
  - test/test_hex_format.rb
159
160
  - test/test_multithreading.rb
161
+ - test/test_normalized.rb
160
162
  - test/test_num_constructor.rb
161
163
  - test/test_odd_even.rb
162
164
  - test/test_rationalizer.rb