Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bigint addition incorrectly results in NaN #12805

Open
p5pRT opened this issue Feb 21, 2013 · 4 comments
Open

bigint addition incorrectly results in NaN #12805

p5pRT opened this issue Feb 21, 2013 · 4 comments

Comments

@p5pRT
Copy link

p5pRT commented Feb 21, 2013

Migrated from rt.perl.org#116889 (status was 'open')

Searchable as RT116889$

@p5pRT
Copy link
Author

p5pRT commented Feb 21, 2013

From jay@rgrs.com

The test case below demonstrates an addition error with
bigint. This problem is in all versions of perl on FreeBSD
from 5.8.0 to 5.16.1. I have also replicated it on various
perl versions on Linux including 5.16.1

use bigint;
use Foo;

my $bar = Foo​::bar(5);
print $bar, "\n"; # expected is 15, actual is NaN
exit;

## Place the following package code in a separate file.
package Foo;

sub bar {
  my ($i) = @​_;
  my $bar = 10 + 0.5 + $i;
  $bar;
}

1;


Flags​:
  category=core
  severity=low


Site configuration information for perl 5.16.1​:

Configured by ec2-user at Sun Oct 7 03​:00​:15 UTC 2012.

Summary of my perl5 (revision 5 version 16 subversion 1) configuration​:

  Platform​:
  osname=linux, osvers=3.2.21-1.32.6.amzn1.x86_64, archname=x86_64-linux
  uname='linux ip-10-195-77-105 3.2.21-1.32.6.amzn1.x86_64 #1 smp sat jun
23 02​:32​:15 utc 2012 x86_64 x86_64 x86_64 gnulinux '
  config_args='-Dcc=gcc -Dprefix=/usr/local -des'
  hint=recommended, useposix=true, d_sigaction=define
  useithreads=undef, usemultiplicity=undef
  useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
  use64bitint=define, use64bitall=define, uselongdouble=undef
  usemymalloc=n, bincompat5005=undef
  Compiler​:
  cc='gcc', ccflags ='-fno-strict-aliasing -pipe -fstack-protector
-I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
  optimize='-O2',
  cppflags='-fno-strict-aliasing -pipe -fstack-protector
-I/usr/local/include'
  ccversion='', gccversion='4.4.6 20110731 (Red Hat 4.4.6-3)',
gccosandvers=''
  intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678
  d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
  ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t',
lseeksize=8
  alignbytes=8, prototype=define
  Linker and Libraries​:
  ld='gcc', ldflags =' -fstack-protector -L/usr/local/lib'
  libpth=/usr/local/lib /lib/../lib64 /usr/lib/../lib64 /lib /usr/lib
/lib64 /usr/lib64 /usr/local/lib64
  libs=-lnsl -ldl -lm -lcrypt -lutil -lc
  perllibs=-lnsl -ldl -lm -lcrypt -lutil -lc
  libc=/lib/libc-2.12.so, so=so, useshrplib=false, libperl=libperl.a
  gnulibc_version='2.12'
  Dynamic Linking​:
  dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
  cccdlflags='-fPIC', lddlflags='-shared -O2 -L/usr/local/lib
-fstack-protector'

Locally applied patches​:


@​INC for perl 5.16.1​:
  /usr/local/lib/perl5/site_perl/5.16.1/x86_64-linux
  /usr/local/lib/perl5/site_perl/5.16.1
  /usr/local/lib/perl5/5.16.1/x86_64-linux
  /usr/local/lib/perl5/5.16.1
  .


Environment for perl 5.16.1​:
  HOME=/home/ec2-user
  LANG=en_US.UTF-8
  LANGUAGE (unset)
  LD_LIBRARY_PATH (unset)
  LOGDIR (unset)

PATH=/usr/local/bin​:/bin​:/usr/bin​:/usr/local/sbin​:/usr/sbin​:/sbin​:/opt/aws/bin​:/home/ec2-user/bin​:/opt/aws/bin
  PERL_BADLANG (unset)
  SHELL=/bin/bash

@p5pRT
Copy link
Author

p5pRT commented Mar 3, 2013

From @arc

Jay Rogers <perlbug-followup@​perl.org> wrote​:

The test case below demonstrates an addition error with
bigint. This problem is in all versions of perl on FreeBSD
from 5.8.0 to 5.16.1. I have also replicated it on various
perl versions on Linux including 5.16.1

Thanks for your report. This is a reduction of your test case​:

$ perl -MMath​::BigInt -wE 'say Math​::BigInt->new(5) + 10.5'
NaN

It's not clear to me that there's a bug here that can be fixed. The
code that does the addition (here, as in your code) isn't in the scope
of a `use bigint` declaration, so nothing has the opportunity to
truncate 10.5 to an integer (as would happen in the presence of such a
declaration). Further, the Math​::BigInt documentation specifies that
Math​::BigInt->new(10.5) (triggered here by the overloaded addition on
the object containing 5) should return NaN, and that NaN values should
propagate through arithmetic.

If you want to be able to pass big-number objects to code that expects
to do non-integer arithmetic on them, perhaps `use bignum` or `use
bigrat` would meet your needs better than `use bigint`.

--
Aaron Crane ** http​://aaroncrane.co.uk/

@p5pRT
Copy link
Author

p5pRT commented Mar 3, 2013

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Mar 5, 2013

From jay@rgrs.com

Aaron,

Thanks so much for your work to improve perl.

I do not agree with your reduction of the bug.

The documentation of bigint describes it as
"Transparent BigInteger support for Perl". The
problem arises when some code that does a "use
bigint" invokes a module that did not do a "use
bigint". This causes the module to fail.

If this problem is not fixed, then the bigint
documentation should be changed to acknowledge
that it is not transparent and that its use may
cause CPAN modules to fail.

--
Jay Rogers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants