Class: Types::RplNumeric

Inherits:
Object
  • Object
show all
Defined in:
lib/rpl/types/numeric.rb

Constant Summary collapse

@@precision =

rubocop:disable Style/ClassVars

12

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, base = 10) ⇒ RplNumeric

Returns a new instance of RplNumeric.

Raises:

  • (RplTypeError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rpl/types/numeric.rb', line 24

def initialize( value, base = 10 )
  raise RplTypeError unless self.class.can_parse?( value )

  @base = base

  case value
  when RplNumeric
    @value = value.value
    @base = value.base
  when BigDecimal
    @value = value
  when Integer
    @value = BigDecimal( value, @@precision )
  when Float
    @value = BigDecimal( value, @@precision )
  when String
    begin
      @value = BigDecimal( value )
    rescue ArgumentError
      case value
      when /^0x[0-9a-f]+$/
        @base = 16
        @value = /^0x(?<value>[0-9a-f]+)$/.match( value )['value'].to_i( @base )
      when /^0o[0-7]+$/
        @base = 8
        @value = /^0o(?<value>[0-7]+)$/.match( value )['value'].to_i( @base )
      when /^0b[0-1]+$/
        @base = 2
        @value = /^0b(?<value>[0-1]+)$/.match( value )['value'].to_i( @base )
      when ''
        @value = BigDecimal('+Infinity')
      when '-∞'
        @value = BigDecimal('-Infinity')
      else
        matches = /(?<base>[0-9]+)b(?<value>[0-9a-z]+)/.match( value )
        @base = matches['base'].to_i
        @value = matches['value'].to_i( @base )
      end
    end
  end
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



7
8
9
# File 'lib/rpl/types/numeric.rb', line 7

def base
  @base
end

#valueObject

Returns the value of attribute value.



7
8
9
# File 'lib/rpl/types/numeric.rb', line 7

def value
  @value
end

Class Method Details

.can_parse?(value) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
104
105
# File 'lib/rpl/types/numeric.rb', line 96

def self.can_parse?( value )
  [RplNumeric, BigDecimal, Integer, Float].include?( value.class ) or
    ( value.is_a?( String ) and ( value.match?(/^-?[0-9]*\.?[0-9]+$/) or
                                  value.match?(/^-?∞$/) or
                                  value.match?(/^0b[0-1]+$/) or
                                  value.match?(/^0o[0-7]+$/) or
                                  value.match?(/^0x[0-9a-f]+$/) or
                                  ( value.match?(/^[0-9]+b[0-9a-z]+$/) and
                                    value.split('_').first.to_i <= 36 ) ) )
end

.default_precisionObject



12
13
14
# File 'lib/rpl/types/numeric.rb', line 12

def self.default_precision
  @@precision = 12 # rubocop:disable Style/ClassVars
end

.precisionObject



16
17
18
# File 'lib/rpl/types/numeric.rb', line 16

def self.precision
  @@precision
end

.precision=(precision) ⇒ Object



20
21
22
# File 'lib/rpl/types/numeric.rb', line 20

def self.precision=( precision )
  @@precision = precision.to_i # rubocop:disable Style/ClassVars
end

Instance Method Details

#==(other) ⇒ Object



107
108
109
110
111
# File 'lib/rpl/types/numeric.rb', line 107

def ==( other )
  other.class == RplNumeric and
    other.base == base and
    other.value == value
end

#to_sObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rpl/types/numeric.rb', line 66

def to_s
  prefix = case @base
           when 2
             '0b'
           when 8
             '0o'
           when 10
             ''
           when 16
             '0x'
           else
             "0#{@base}_"
           end

  if @value.infinite?
    suffix = @value.infinite?.positive? ? '' : '-∞'
  elsif @value.nan?
    suffix = '<NaN>'
  else
    suffix = if @value.to_i == @value
               @value.to_i
             else
               @value.to_s('F')
             end
    suffix = @value.to_s( @base ) unless @base == 10
  end

  "#{prefix}#{suffix}"
end