Skip Menu |

This queue is for tickets about the Math-BaseCalc CPAN distribution.

Report information
The Basics
Id: 65114
Status: resolved
Priority: 0/
Queue: Math-BaseCalc

People
Owner: Nobody in particular
Requestors: me [...] eboxr.com
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 1.011
Fixed in: (no value)



Subject: Character dash '-' is not a valid digit
When you define a specific digit set, no matter how many, if you use '-' as a character... it will produce error using from base Here is a quick sample : --------------- my $calcX = new Math::BaseCalc(digits => [ '0', '-' ]); for my $source (0..10) { my $in_base_X = $calcX->to_base( $source ); my $in_base_10 = $calcX->from_base( $in_base_X ); print "source : $source ; from source to base X : ", $in_base_X, " ; from base X to base 10 : ", $in_base_10, "\n"; } --------------- Output is : source : 0 ; from source to base X : 0 ; from base X to base 10 : 0 source : 1 ; from source to base X : - ; from base X to base 10 : 0 source : 2 ; from source to base X : -0 ; from base X to base 10 : 0 source : 3 ; from source to base X : -- ; from base X to base 10 : 0 source : 4 ; from source to base X : -00 ; from base X to base 10 : 0 source : 5 ; from source to base X : -0- ; from base X to base 10 : -1 source : 6 ; from source to base X : --0 ; from base X to base 10 : 0 source : 7 ; from source to base X : --- ; from base X to base 10 : 0 source : 8 ; from source to base X : -000 ; from base X to base 10 : 0 source : 9 ; from source to base X : -00- ; from base X to base 10 : -1 source : 10 ; from source to base X : -0-0 ; from base X to base 10 : -2 we expect to find the same number as source in the convertion process from_base. If you use any other chars than '-', you will not have this problem.
From: me [...] eboxr.com
This error comes from the fact that '-' digit is reserved for negative number. I think we can detect that the digit set include this number or not then avoid treating negative number ( probably croak when a negative number has been detected ) Here is a simple patch and an additional test file : ---------------------------------------------- --- a/lib/Math/BaseCalc.pm +++ b/lib/Math/BaseCalc.pm @@ -3,11 +3,12 @@ package Math::BaseCalc; use strict; use Carp; use vars qw($VERSION); -$VERSION = '1.013'; +$VERSION = '1.014'; sub new { my ($pack, %opts) = @_; my $self = bless {}, $pack; + $self->{has_dash} = 0; $self->digits($opts{digits}); return $self; } @@ -17,6 +18,7 @@ sub digits { if (@_) { # Set the value + if (ref $_[0]) { $self->{digits} = [ @{ shift() } ]; } else { @@ -25,6 +27,7 @@ sub digits { croak "Unrecognized digit set '$name'" unless exists $digitsets{$name}; $self->{digits} = $digitsets{$name}; } + $self->{has_dash} = grep { $_ eq '-' } @{$self->{digits}}; # Build the translation table back to numbers @{$self->{trans}}{@{$self->{digits}}} = 0..$#{$self->{digits}}; @@ -47,7 +50,7 @@ sub _digitsets { sub from_base { my $self = shift; - return -1*$self->from_base(substr($_[0],1)) if $_[0] =~ /^-/; # Handle negative numbers + return -1*$self->from_base(substr($_[0],1)) if !$self->{has_dash} && $_[0] =~ /^-/; # Handle negative numbers my $str = shift; my $dignum = @{$self->{digits}};
Subject: 02_digit_dash.t
#! perl use strict; use warnings; use Test::More tests => 24; use_ok('Math::BaseCalc'); my $calc = new Math::BaseCalc(digits=>[0,1]); isa_ok($calc, "Math::BaseCalc"); my @calcs; push(@calcs, new Math::BaseCalc(digits => [ '0', '&' ])); push(@calcs, new Math::BaseCalc(digits => [ '0', '-' ])); for my $calcX ( @calcs ) { for my $source (0..10) { my $in_base_X = $calcX->to_base( $source ); my $in_base_10 = $calcX->from_base( $in_base_X ); is $in_base_10, $source, "from( to ( $source ) == $source "; } }
Thanks, I've committed these changes and added some docs about it.
Subject: Re: [rt.cpan.org #65114] Character dash '-' is not a valid digit
Date: Tue, 1 Feb 2011 19:23:24 +0100
To: bug-Math-BaseCalc [...] rt.cpan.org
From: Nicolas <me [...] eboxr.com>
thanks for your quick reply. have a nice week nicolas 2011/2/1 Ken Williams via RT <bug-Math-BaseCalc@rt.cpan.org> Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=65114 > > > Thanks, I've committed these changes and added some docs about it. >
I think this bug is fixed. Can this ticket be closed?
Yes, thanks.