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
@value = BigDecimal( value, @@precision )
when String
case value
when '∞'
@value = BigDecimal('+Infinity')
when '-∞'
@value = BigDecimal('-Infinity')
else
underscore_position = value.index('_')
if value[0] == '0' && ( %w[b o x].include?( value[1] ) || !underscore_position.nil? )
if value[1] == 'x'
@base = 16
elsif value[1] == 'b'
@base = 2
elsif value[1] == 'o'
@base = 8
value = value[2..-1]
elsif !underscore_position.nil?
@base = value[1..(underscore_position - 1)].to_i
value = value[(underscore_position + 1)..-1]
end
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
value = value.to_i( @base ) unless @base == 10
@value = BigDecimal( value, @@precision )
end
end
end
@ -102,7 +100,7 @@ module Types
value.match?(/^0b[0-1]+$/) or
value.match?(/^0o[0-7]+$/) 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 ) ) )
end

View file

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

View file

@ -103,12 +103,12 @@ class TestTypes < MiniTest::Test
assert_equal true, RplNumeric.can_parse?( '0b101' )
assert_equal true, RplNumeric.can_parse?( '0xfed' )
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?( '0x' )
assert_equal false, RplNumeric.can_parse?( '0o' )
assert_equal false, RplNumeric.can_parse?( '013_' )
assert_equal false, RplNumeric.can_parse?( '037_ba7' )
assert_equal false, RplNumeric.can_parse?( '13b' )
assert_equal false, RplNumeric.can_parse?( '37bba7' )
assert_equal false, RplNumeric.can_parse?( 'fed' )
assert_equal false, RplNumeric.can_parse?( 'ba7' )
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, '0xfed' ).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.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(4077), Types.new_object( RplNumeric, '0xfed' ).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 2, Types.new_object( RplNumeric, '0b101' ).base
assert_equal 16, Types.new_object( RplNumeric, '0xfed' ).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
def test_list