numerals formatting in sync with rpn-2.4

This commit is contained in:
Gwenhael Le Moine 2022-03-02 14:02:28 +01:00
parent 92bd286f71
commit e27f829ff3
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
3 changed files with 38 additions and 31 deletions

View file

@ -37,30 +37,28 @@ module Types
when Float when Float
@value = BigDecimal( value, @@precision ) @value = BigDecimal( value, @@precision )
when String when String
case value begin
when '∞' @value = BigDecimal( value )
@value = BigDecimal('+Infinity') rescue ArgumentError
when '-∞' case value
@value = BigDecimal('-Infinity') when /^0x[0-9a-f]+$/
else @base = 16
underscore_position = value.index('_') @value = /^0x(?<value>[0-9a-f]+)$/.match( value )['value'].to_i( @base )
when /^0o[0-7]+$/
if value[0] == '0' && ( %w[b o x].include?( value[1] ) || !underscore_position.nil? ) @base = 8
if value[1] == 'x' @value = /^0o(?<value>[0-7]+)$/.match( value )['value'].to_i( @base )
@base = 16 when /^0b[0-1]+$/
elsif value[1] == 'b' @base = 2
@base = 2 @value = /^0b(?<value>[0-1]+)$/.match( value )['value'].to_i( @base )
elsif value[1] == 'o' when '∞'
@base = 8 @value = BigDecimal('+Infinity')
value = value[2..-1] when '-∞'
elsif !underscore_position.nil? @value = BigDecimal('-Infinity')
@base = value[1..(underscore_position - 1)].to_i else
value = value[(underscore_position + 1)..-1] matches = /(?<base>[0-9]+)b(?<value>[0-9a-z]+)/.match( value )
end @base = matches['base'].to_i
@value = matches['value'].to_i( @base )
end end
value = value.to_i( @base ) unless @base == 10
@value = BigDecimal( value, @@precision )
end end
end end
end end
@ -102,7 +100,7 @@ module Types
value.match?(/^0b[0-1]+$/) or value.match?(/^0b[0-1]+$/) or
value.match?(/^0o[0-7]+$/) or value.match?(/^0o[0-7]+$/) or
value.match?(/^0x[0-9a-f]+$/) or value.match?(/^0x[0-9a-f]+$/) or
( value.match?(/^0[0-9]+_[0-9a-z]+$/) and ( value.match?(/^[0-9]+b[0-9a-z]+$/) and
value.split('_').first.to_i <= 36 ) ) ) value.split('_').first.to_i <= 36 ) ) )
end end

View file

@ -17,16 +17,25 @@ class TestParser < MiniTest::Test
result = Parser.parse( '0b101' ) result = Parser.parse( '0b101' )
assert_equal 1, result.size assert_equal 1, result.size
assert_equal RplNumeric, result.first.class assert_equal RplNumeric, result.first.class
assert_equal 2, result.first.base
assert_equal BigDecimal( 5 ), result.first.value assert_equal BigDecimal( 5 ), result.first.value
result = Parser.parse( '0o57' ) result = Parser.parse( '0o57' )
assert_equal 1, result.size assert_equal 1, result.size
assert_equal RplNumeric, result.first.class assert_equal RplNumeric, result.first.class
assert_equal 8, result.first.base
assert_equal BigDecimal( 47 ), result.first.value assert_equal BigDecimal( 47 ), result.first.value
result = Parser.parse( '03_10' ) result = Parser.parse( '0xfa' )
assert_equal 1, result.size assert_equal 1, result.size
assert_equal RplNumeric, result.first.class assert_equal RplNumeric, result.first.class
assert_equal 16, result.first.base
assert_equal BigDecimal( 250 ), result.first.value
result = Parser.parse( '3b10' )
assert_equal 1, result.size
assert_equal RplNumeric, result.first.class
assert_equal 3, result.first.base
assert_equal BigDecimal( 3 ), result.first.value assert_equal BigDecimal( 3 ), result.first.value
end end

View file

@ -103,12 +103,12 @@ class TestTypes < MiniTest::Test
assert_equal true, RplNumeric.can_parse?( '0b101' ) assert_equal true, RplNumeric.can_parse?( '0b101' )
assert_equal true, RplNumeric.can_parse?( '0xfed' ) assert_equal true, RplNumeric.can_parse?( '0xfed' )
assert_equal true, RplNumeric.can_parse?( '0o57' ) assert_equal true, RplNumeric.can_parse?( '0o57' )
assert_equal true, RplNumeric.can_parse?( '013_ba7' ) assert_equal true, RplNumeric.can_parse?( '13bba7' )
assert_equal false, RplNumeric.can_parse?( '0b' ) assert_equal false, RplNumeric.can_parse?( '0b' )
assert_equal false, RplNumeric.can_parse?( '0x' ) assert_equal false, RplNumeric.can_parse?( '0x' )
assert_equal false, RplNumeric.can_parse?( '0o' ) assert_equal false, RplNumeric.can_parse?( '0o' )
assert_equal false, RplNumeric.can_parse?( '013_' ) assert_equal false, RplNumeric.can_parse?( '13b' )
assert_equal false, RplNumeric.can_parse?( '037_ba7' ) assert_equal false, RplNumeric.can_parse?( '37bba7' )
assert_equal false, RplNumeric.can_parse?( 'fed' ) assert_equal false, RplNumeric.can_parse?( 'fed' )
assert_equal false, RplNumeric.can_parse?( 'ba7' ) assert_equal false, RplNumeric.can_parse?( 'ba7' )
assert_equal false, RplNumeric.can_parse?( '0aba7' ) assert_equal false, RplNumeric.can_parse?( '0aba7' )
@ -129,7 +129,7 @@ class TestTypes < MiniTest::Test
assert_equal RplNumeric, Types.new_object( RplNumeric, '0b101' ).class assert_equal RplNumeric, Types.new_object( RplNumeric, '0b101' ).class
assert_equal RplNumeric, Types.new_object( RplNumeric, '0xfed' ).class assert_equal RplNumeric, Types.new_object( RplNumeric, '0xfed' ).class
assert_equal RplNumeric, Types.new_object( RplNumeric, '0o57' ).class assert_equal RplNumeric, Types.new_object( RplNumeric, '0o57' ).class
assert_equal RplNumeric, Types.new_object( RplNumeric, '013_ba7' ).class assert_equal RplNumeric, Types.new_object( RplNumeric, '13bba7' ).class
assert_equal BigDecimal(1), Types.new_object( RplNumeric, 1 ).value assert_equal BigDecimal(1), Types.new_object( RplNumeric, 1 ).value
assert_equal BigDecimal(1.2, 12), Types.new_object( RplNumeric, 1.2 ).value assert_equal BigDecimal(1.2, 12), Types.new_object( RplNumeric, 1.2 ).value
@ -145,13 +145,13 @@ class TestTypes < MiniTest::Test
assert_equal BigDecimal(5), Types.new_object( RplNumeric, '0b101' ).value assert_equal BigDecimal(5), Types.new_object( RplNumeric, '0b101' ).value
assert_equal BigDecimal(4077), Types.new_object( RplNumeric, '0xfed' ).value assert_equal BigDecimal(4077), Types.new_object( RplNumeric, '0xfed' ).value
assert_equal BigDecimal(47), Types.new_object( RplNumeric, '0o57' ).value assert_equal BigDecimal(47), Types.new_object( RplNumeric, '0o57' ).value
assert_equal BigDecimal(1996), Types.new_object( RplNumeric, '013_ba7' ).value assert_equal BigDecimal(1996), Types.new_object( RplNumeric, '13bba7' ).value
assert_equal 10, Types.new_object( RplNumeric, '-.456' ).base assert_equal 10, Types.new_object( RplNumeric, '-.456' ).base
assert_equal 2, Types.new_object( RplNumeric, '0b101' ).base assert_equal 2, Types.new_object( RplNumeric, '0b101' ).base
assert_equal 16, Types.new_object( RplNumeric, '0xfed' ).base assert_equal 16, Types.new_object( RplNumeric, '0xfed' ).base
assert_equal 8, Types.new_object( RplNumeric, '0o57' ).base assert_equal 8, Types.new_object( RplNumeric, '0o57' ).base
assert_equal 13, Types.new_object( RplNumeric, '013_ba7' ).base assert_equal 13, Types.new_object( RplNumeric, '13bba7' ).base
end end
def test_list def test_list