Skip Menu |

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

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

People
Owner: Nobody in particular
Requestors: peter.john.acklam [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 1.96
Fixed in: 1.999701



Subject: Inconsistent handling of non-integers in Math::BigInt
Non-integer input x, when x in the range -1 < x < 1 are converted to zero, but other non-integers are converted to NaN. $ perl -MMath::BigInt -wle 'print Math::BigInt -> new("2.49")' NaN $ perl -MMath::BigInt -wle 'print Math::BigInt -> new("1.49")' NaN $ perl -MMath::BigInt -wle 'print Math::BigInt -> new("0.49")' 0 This behaviour is inconsistent. Either all non-integers should give a NaN, or be truncated to an integer.
During a conversion from a Math::BigFloat with a non-zero fraction to a Math::BigInt, a truncation is done: $ perl -MMath::BigFloat -wle 'print Math::BigFloat -> new(2.718) -> as_int' 2 I think it is most consistent if converting *any* number with a non-zero fraction to a Math::BigInt, would cause a truncation, so $ perl -MMath::BigFloat -wle 'print Math::BigInt -> new(2.718)' NaN should also give 2, not NaN. The current behaviour is too arbitrary. Sometimes a truncation to integer is done, sometimes a NaN is returned. I vote for always doing a truncation.
This is still an issue and is pretty serious, considering that BigInt's behavior in that manner is documented to do the truncation.
From: bitcard [...] volkerschatz.com
The code (lines 613 to 639) shows that the intention was to return NaN for non-integers. However, line 641: $self->{sign} = '+' if $$miv eq '0'; defeats this when the integer part is 0, hence the inconsistency. To keep the old behaviour, one would have to change it as follows: $self->{sign} = '+' if $$miv eq '0' && $self->{sign} ne $nan; To always do a truncation and not return NaN as suggested above, one has to remove the lines flagging the NaN: @@ -613,13 +613,8 @@ if ($$mfv ne '') # e <= 0 { # fraction and negative/zero E => NOI - if ($_trap_nan) - { - require Carp; Carp::croak("$wanted not an integer in $class"); - } #print "NOI 2 \$\$mfv '$$mfv'\n"; return $upgrade->new($wanted,$a,$p,$r) if defined $upgrade; - $self->{sign} = $nan; } elsif ($e < 0) { @@ -628,17 +623,12 @@ $e = abs($e); if ($$miv !~ s/0{$e}$//) # can strip so many zero's? { - if ($_trap_nan) - { - require Carp; Carp::croak("$wanted not an integer in $class"); - } #print "NOI 3\n"; return $upgrade->new($wanted,$a,$p,$r) if defined $upgrade; - $self->{sign} = $nan; } } } All Line numbers refer to version 1.9991, which my distribution ships but CPAN does not yet (?!).
Fixed in Math-BigInt-1.999701.