Class: Types::RplNumeric
- Inherits:
-
Object
- Object
- Types::RplNumeric
- Defined in:
- lib/rpl/types/numeric.rb
Constant Summary collapse
- @@precision =
rubocop:disable Style/ClassVars
12
Instance Attribute Summary collapse
-
#base ⇒ Object
Returns the value of attribute base.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
- .can_parse?(value) ⇒ Boolean
- .default_precision ⇒ Object
- .precision ⇒ Object
- .precision=(precision) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(value, base = 10) ⇒ RplNumeric
constructor
A new instance of RplNumeric.
- #to_s ⇒ Object
Constructor Details
#initialize(value, base = 10) ⇒ RplNumeric
Returns a new instance of RplNumeric.
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 |
# 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, Float @value = BigDecimal( value, @@precision ) when String begin @value = BigDecimal( value, @@precision ) rescue ArgumentError case value when /^0x[0-9a-f]+$/ @base = 16 @value = BigDecimal( /^0x(?<value>[0-9a-f]+)$/.match( value )['value'].to_i( @base ), @@precision ) when /^0o[0-7]+$/ @base = 8 @value = BigDecimal( /^0o(?<value>[0-7]+)$/.match( value )['value'].to_i( @base ), @@precision ) when /^0b[0-1]+$/ @base = 2 @value = BigDecimal( /^0b(?<value>[0-1]+)$/.match( value )['value'].to_i( @base ), @@precision ) 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 = BigDecimal( matches['value'].to_i( @base ), @@precision ) end end end end |
Instance Attribute Details
#base ⇒ Object
Returns the value of attribute base.
7 8 9 |
# File 'lib/rpl/types/numeric.rb', line 7 def base @base end |
#value ⇒ Object
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
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rpl/types/numeric.rb', line 94 def self.can_parse?( value ) [RplNumeric, BigDecimal, Integer, Float].include?( value.class ) || ( value.is_a?( String ) && ( value.match?(/^-?[0-9]*\.?[0-9]+$/) || value.match?(/^-?∞$/) || value.match?(/^0b[0-1]+$/) || value.match?(/^0o[0-7]+$/) || value.match?(/^0x[0-9a-f]+$/) || ( value.match?(/^[0-9]+b[0-9a-z]+$/) && value.split('_').first.to_i <= 36 ) ) ) end |
.default_precision ⇒ Object
12 13 14 |
# File 'lib/rpl/types/numeric.rb', line 12 def self.default_precision @@precision = 12 # rubocop:disable Style/ClassVars end |
.precision ⇒ Object
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
105 106 107 108 109 |
# File 'lib/rpl/types/numeric.rb', line 105 def ==( other ) other.class == RplNumeric && other.base == base && other.value == value end |
#to_s ⇒ Object
65 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 |
# File 'lib/rpl/types/numeric.rb', line 65 def to_s prefix = case @base when 2 '0b' when 8 '0o' when 10 '' when 16 '0x' else "#{@base}b" end suffix = if @value.infinite? @value.infinite?.positive? ? '∞' : '-∞' elsif @value.nan? '<NaN>' elsif @base != 10 @value.to_i.to_s( @base ) elsif @value.frac.zero? @value.to_i else @value.to_s( 'F' ) end "#{prefix}#{suffix}" end |