Skip Menu |
 

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

Report information
The Basics
Id: 62052
Status: resolved
Priority: 0/
Queue: Math-BigInt

People
Owner: Nobody in particular
Requestors: Test [...] globis.net
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: 1.9994



Subject: BigInt documentation is a tad misleading for brsft
Date: Mon, 11 Oct 2010 19:04:00 +0200
To: bug-Math-BigInt [...] rt.cpan.org
From: Test <Test [...] globis.net>
Download (untitled) / with headers
text/plain 1.4k
Package: Math::BigInt Distribution: straight from cpan. Verison: 1.89 Thanks for the code. I'm grateful. Honest. But I did encounter a wee disappointment. The html documentation seems to state: brsft() <http://search.cpan.org/%7Eflora/Math-BigInt-1.96/lib/Math/BigInt.pm#___top> $x->brsft($y,$n); Shifts $x right by $y in base $n. Default is base 2, used are usually 10 and 2, but others work, too. But looking in the pod documentation in the distrution package: line 3183 of the synopsis states $x->brsft($y); # right shift in base 2 # returns (quo,rem) or quo if in scalar context However, the real working code does not seem to be at all context sensitive i.e. it never returns rem (comes back as undef) I find this is a bit of a shame for simple situations where you want to take the first and second nibble e.g. like stripping out the first and second 64 bits of an IPv6 address to be stored in a MySQL database which can't handle 128 bits in one integer. I ended up using: sub IP_to_db { # make sure the input really is a bigint my $bint = Math::BigInt->new(shift); my $top_nibble=Math::BigInt->new(1); $top_nibble->blsft(64); # left shift 64 bits my $nibble_a; my $nibble_b; ($nibble_a,$nibble_b)= $bint->bdiv($top_nibble); # split into 2*64 bits for MySQL which is probably a lot less efficient than my $nibble_a; my $nibble_b; ($nibble_a,$nibble_b)= $bint->brsft(64); (depending on the internal representation of course) redards,
Download (untitled) / with headers
text/plain 239b
This is clearly a bug in the documentation. There is nothing in the code for brsft() and blsft() indicating that these methods were ever supposed to return both the quotient and the remainder. I have attached a patch that corrects the POD.
Subject: Math-BigInt-id-62052.txt
--- lib/Math/BigInt.pm.orig 2010-10-12 08:23:49.218113600 +0200 +++ lib/Math/BigInt.pm 2010-10-12 08:44:19.507182400 +0200 @@ -3180,10 +3180,8 @@ $x->bpow($y); # power of arguments (x ** y) $x->blsft($y); # left shift in base 2 $x->brsft($y); # right shift in base 2 - # returns (quo,rem) or quo if in scalar context $x->blsft($y,$n); # left shift by $y places in base $n $x->brsft($y,$n); # right shift by $y places in base $n - # returns (quo,rem) or quo if in scalar context $x->band($y); # bitwise and $x->bior($y); # bitwise inclusive or
Subject: Re: [rt.cpan.org #62052] BigInt documentation is a tad misleading for brsft
Date: Tue, 12 Oct 2010 09:24:30 +0200
To: bug-Math-BigInt [...] rt.cpan.org
From: Test <Test [...] globis.net>
Download (untitled) / with headers
text/plain 1.2k
Peter John Acklam via RT wrote: Show quoted text
> <URL: https://rt.cpan.org/Ticket/Display.html?id=62052> > > This is clearly a bug in the documentation. There is nothing in the code > for brsft() and blsft() indicating that these methods were ever supposed > to return both the quotient and the remainder. I have attached a patch > that corrects the POD. >
Fair enough. I accept that explanation and shall continue to use my alternative code. Just FYI: in the rather ancient World of integer arithmetic and 8 bit microprocessors, it was common when performing operations like logical shift right or arithmetic shift left to preserve the bits that "fell off the end" during the shift so you could use them in another result see e.g. 6502 LSR & ASL opcodes where the additional bit drops into the "carry bit" which you can then shift into another register, thus extending maths beyond your original 8 bits. That exactly corresponds to what I was trying to do (except for 64 bits) although I accept that the general point of the bigint lib is that normally you shouldn't have to perform such primitive operations. Shame MySQL still has the hard 64 bit limit on integers. Maybe I/someone should be adding this to the Net::IP library, as I suspect it will be quite a common requirement.
Download (untitled) / with headers
text/plain 986b
On Tue Oct 12 03:24:49 2010, Test@globis.net wrote: Show quoted text
> Fair enough. I accept that explanation and shall continue to use my > alternative code. (...)
Thanks for the information. It turns out that both the code and the documentation for Math::BigFloat suggest that the behaviour you want corresponds to that of Math::BigFloat, that the return values of brsft() in list context is indeed the quotient and the remainder. Firstly, it seems inconsistent to me that Math::BigInt and Math::BigFloat behaves differently. I think either both of them or none of them ought to return the quotient and remainder in list context. Secondly, it seems that the behaviour of Math::BigFloat doesn't match the documentation: $ perl -I. -MMath::BigFloat -wle '$x = Math::BigFloat -> new(2345); ($q, $r) = $x -> brsft(2, 10); print for $x, $q, $r' 23.45 23.45 45 While the input is shifted two places, the output in $q is clearly not the quotient. Now, if only Math-BigInt had an active maintainer ...
Subject: Re: [rt.cpan.org #62052] BigInt documentation is a tad misleading for brsft
Date: Tue, 12 Oct 2010 12:20:57 +0200
To: bug-Math-BigInt [...] rt.cpan.org
From: Florian Ragwitz <rafl [...] debian.org>
Download (untitled) / with headers
text/plain 748b
"Peter John Acklam via RT" <bug-Math-BigInt@rt.cpan.org> writes: Show quoted text
> Now, if only Math-BigInt had an active maintainer ...
FWIW, I'm right here, listening to the discussion about this and the various other tickets, and the last release of Math-BigInt happened 2 weeks ago. While I don't have the resources to attend to each issue immediately, I do try to work on the various Math-Big*/bignum RT queues, as you might've noticed from $many to 1 open issue within the last few weeks. I'm sorry if that isn't as active as you might wish, but I am trying my best under the given constraints. Any help, especially those in the form of patches, but also simply reporting issues, or following the RT queue and discussing issues, is *hugely* appreciated.
Download (untitled)
application/pgp-signature 197b

Message body not shown because it is not plain text.

Download (untitled) / with headers
text/plain 484b
On Tue Oct 12 06:22:06 2010, FLORA wrote: Show quoted text
> "Peter John Acklam via RT" <bug-Math-BigInt@rt.cpan.org> writes: >
> > Now, if only Math-BigInt had an active maintainer ...
> > FWIW, I'm right here (...)
I'm very sorry, I thought TELS was the only maintainer. I contacted him, but I got a reply telling me he had no time to maintain this distribution. I see now that there are several other maintainers. And I fully understand that you can't reply immediately. Again, sorry about that.
On Tue Oct 12 06:03:46 2010, pjacklam wrote: Show quoted text
> Secondly, it seems that the behaviour of Math::BigFloat doesn't match > the documentation: > > $ perl -I. -MMath::BigFloat -wle '$x = Math::BigFloat -> new(2345); ($q, > $r) = $x -> brsft(2, 10); print for $x, $q, $r' > 23.45 > 23.45 > 45 > > While the input is shifted two places, the output in $q is clearly not > the quotient.
After re-thinking this, $q = 23.45 is of course the quotient, assuming that you are dividing 2345 by 10**2. However, when one is talking about quotient and remainder ($q and $r, say) after dividing a numerator by a denominator ($n and $d, say), it usually means that one gets a $q and $r satisfying $n = $q * $d + $r. That is not the case with Math::BigFloat's brsft(). So, while the behaviour of Math::BigFloat might not be wrong, I think it is a little - what shall I say? - unexpected. I think the most useful and intuitive would be if Math::BigFloat -> new(2345) -> brsft(2, 10) returned 23.45 in scalar context, and (23, 45) in list context.
CC: undisclosed-recipients:;
Subject: Re: [rt.cpan.org #62052] BigInt documentation is a tad misleading for brsft
Date: Tue, 12 Oct 2010 12:41:08 +0200
To: bug-Math-BigInt [...] rt.cpan.org
From: Florian Ragwitz <rafl [...] debian.org>
Download (untitled) / with headers
text/plain 757b
"Peter John Acklam via RT" <bug-Math-BigInt@rt.cpan.org> writes: Show quoted text
> On Tue Oct 12 06:22:06 2010, FLORA wrote:
>> "Peter John Acklam via RT" <bug-Math-BigInt@rt.cpan.org> writes: >>
>> > Now, if only Math-BigInt had an active maintainer ...
>> >> FWIW, I'm right here (...)
> > I'm very sorry, I thought TELS was the only maintainer. I contacted > him, but I got a reply telling me he had no time to maintain this > distribution. I see now that there are several other maintainers. And > I fully understand that you can't reply immediately. Again, sorry > about that.
Don't worry. I really appreciate your comments providing extra information on the various Big* tickets. This sort of thing makes going through all of a little bit this later *much* easier.
Download (untitled)
application/pgp-signature 197b

Message body not shown because it is not plain text.

Subject: Re: [rt.cpan.org #62052] BigInt documentation is a tad misleading for brsft
Date: Tue, 12 Oct 2010 10:21:31 -0700
To: bug-Math-BigInt [...] rt.cpan.org
From: Jonathan Leto <jonathan [...] leto.net>
Download (untitled) / with headers
text/plain 502b
Howdy, Show quoted text
>> On Tue Oct 12 06:22:06 2010, FLORA wrote:
>>> "Peter John Acklam via RT" <bug-Math-BigInt@rt.cpan.org> writes: >>>
>>> > Now, if only Math-BigInt had an active maintainer ...
>>> >>> FWIW, I'm right here (...)
I am here too, for some definition of "active." I tried to take over for TELS, but then realized I don't have as much time as I would like to work on it. I commend Florian for actually making things happen lately. Duke -- Jonathan "Duke" Leto jonathan@leto.net http://leto.net
RT-Send-CC: jonathan [...] leto.net, rafl [...] debian.org
Download (untitled) / with headers
text/plain 172b
This appears to still be an issue. Show quoted text
>perl -I. -MMath::BigFloat -wle "$x = Math::BigFloat -> new(2345); ($q,
$r) = $x -> brsft(2, 10); print for $x, $q, $r" 23.45 23.45 45
Download (untitled) / with headers
text/plain 722b
The subject is inaccurate. The documentation for brsft() in both BigInt and BigFloat are fine. The bug is in bdiv(): The expression ($q, $r) = Math::BigFloat -> new($a) -> brsft($b, $c)' is computed as ($q, $r) = Math::BigFloat -> new($a) -> bdiv($b -> bpow($c))' so the example above, $ perl -Idist/Math-BigInt/lib/ -MMath::BigFloat -wle '$x = Math::BigFloat -> new(2345); ($q, $r) = $x -> brsft(2, 10); print for $x, $q, $r' 23.45 23.45 45 can be simplified into $ perl -Idist/Math-BigInt/lib/ -MMath::BigFloat -wle '($q, $r) = Math::BigFloat -> new(2345) -> bdiv(100); print for $q, $r' 23.45 45 which illustrates the same problem. In list context, this should give 23 and 45. So the bug is actually in bdiv().
Fixed in version 1.9994.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.