perl 整数算术给出浮点答案

标签 perl floating-point integer integer-arithmetic

下面的代码模拟了实际的生产代码。双引号用作实际数据来自 XML 文件,使用 XML:Twig 解析:

#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;
use Devel::Peek;

my $linetotalinclusive = "8458.80" * 1_000_000;

$linetotalinclusive = $linetotalinclusive;

my $c = "7980.00" * 1_000_000;

my $data = $linetotalinclusive - $c;

print Dump $c;

print Dump $linetotalinclusive;

print "$linetotalinclusive - $c = $data \n";

给出以下结果:
SV = PVNV(0x22885f0) at 0x21984f8
  REFCNT = 1
  FLAGS = (PADMY,IOK,NOK,pIOK,pNOK)
  IV = 7980000000
  NV = 7980000000
  PV = 0
SV = PVNV(0x2288650) at 0x21984c8
  REFCNT = 1
  FLAGS = (PADMY,NOK,pIOK,pNOK)
  IV = 8458799999
  NV = 8458800000
  PV = 0

8458800000 - 7980000000 = 478799999.999999

在我的笔记本电脑和我们的生产服务器上运行时。 (以上来自我的笔记本电脑)
但是,当我在另一台生产机器上运行它时,它工作正常。use integer; 上面的代码使它工作。但我无法(轻松)对生产代码执行此操作。所以,我很想知道...
  • 为什么会发生上述情况。
  • perl 解释器缺少什么编译选项来修复它。

  • 更多信息:
    这是来自损坏的机器:
    This is perl 5, version 18, subversion 1 (v5.18.1) built for x86_64-linux-thread-multi
    perl -MPOSIX -le 'print LONG_MAX'
    9223372036854775807
    
    perl -V:[in]vsize
    ivsize='8';
    nvsize='8';
    

    这是来自工作的机器:
    This is perl, v5.8.9 built for x86_64-linux-ld
    
    perl -MPOSIX -le 'print LONG_MAX'
    9223372036854775807
    
    perl -V:[in]vsize
    ivsize='8';
    nvsize='16';
    

    这给出了预期的答案:
    Summary of my perl5 (revision 5 version 8 subversion 9) configuration:
      Platform:
        osname=linux, osvers=2.6.32-431.3.1.el6.x86_64, archname=x86_64-linux-ld
        uname='linux 553291-amon-sul2.firstb2b.net 2.6.32-431.3.1.el6.x86_64 #1 smp sat jan 4 02:04:49 est 2014 x86_64 x86_64 x86_64 gnulinux '
        config_args=''
        hint=recommended, useposix=true, d_sigaction=define
        usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
        useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
        use64bitint=define use64bitall=define uselongdouble=define
        usemymalloc=n, bincompat5005=undef
      Compiler:
        cc='cc', ccflags ='-fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm',
        optimize='-O2',
        cppflags='-fno-strict-aliasing -pipe -I/usr/local/include -I/usr/include/gdbm'
        ccversion='', gccversion='4.4.7 20120313 (Red Hat 4.4.7-4)', 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='long double', nvsize=16, Off_t='off_t', lseeksize=8
        alignbytes=16, prototype=define
      Linker and Libraries:
        ld='cc', ldflags =' -L/usr/local/lib'
        libpth=/usr/local/lib /lib /usr/lib /lib64 /usr/lib64 /usr/local/lib64
        libs=-lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc
        perllibs=-lnsl -ldl -lm -lcrypt -lutil -lc
        libc=, 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'
    
    
    Characteristics of this binary (from libperl): 
      Compile-time options: PERL_MALLOC_WRAP USE_64_BIT_ALL USE_64_BIT_INT
                            USE_FAST_STDIO USE_LARGE_FILES USE_LONG_DOUBLE
                            USE_PERLIO
    

    这个没有:
    Summary of my perl5 (revision 5 version 8 subversion 8) configuration:
      Platform:
        osname=linux, osvers=2.6.18-194.26.1.el5, archname=x86_64-linux-thread-multi
        uname='linux x86-002.build.bos.redhat.com 2.6.18-194.26.1.el5 #1 smp fri oct 29 14:21:16 edt 2010 x86_64 x86_64 x86_64 gnulinux '
        config_args='-des -Doptimize=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Dversion=5.8.8 -Dmyhostname=localhost -Dperladmin=root@localhost -Dcc=gcc -Dcf_by=Red Hat, Inc. -Dinstallprefix=/usr -Dprefix=/usr -Dlibpth=/usr/local/lib64 /lib64 /usr/lib64 -Dprivlib=/usr/lib/perl5/5.8.8 -Dsitelib=/usr/lib/perl5/site_perl/5.8.8 -Dvendorlib=/usr/lib/perl5/vendor_perl/5.8.8 -Darchlib=/usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi -Dsitearch=/usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi -Dvendorarch=/usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi -Darchname=x86_64-linux-thread-multi -Dvendorprefix=/usr -Dsiteprefix=/usr -Duseshrplib -Dusethreads -Duseithreads -Duselargefiles -Dd_dosuid -Dd_semctl_semun -Di_db -Ui_ndbm -Di_gdbm -Di_shadow -Di_syslog -Dman3ext=3pm -Duseperlio -Dinstallusrbinperl=n -Ubincompat5005 -Uversiononly -Dpager=/usr/bin/less -isr -Dd_gethostent_r_proto -Ud_endhostent_r_proto -Ud_sethostent_r_proto -Ud_endprotoent_r_proto -Ud_setprotoent_r_proto -Ud_endservent_r_proto -Ud_setservent_r_proto -Dinc_version_list=5.8.7 5.8.6 5.8.5 -Dscriptdir=/usr/bin'
        hint=recommended, useposix=true, d_sigaction=define
        usethreads=define use5005threads=undef useithreads=define usemultiplicity=define
        useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
        use64bitint=define use64bitall=define uselongdouble=undef
        usemymalloc=n, bincompat5005=undef
      Compiler:
        cc='gcc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe -Wdeclaration-after-statement -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm',
        optimize='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic',
        cppflags='-D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe -Wdeclaration-after-statement -I/usr/local/include -I/usr/include/gdbm'
        ccversion='', gccversion='4.1.2 20080704 (Red Hat 4.1.2-50)', 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 =''
        libpth=/usr/local/lib64 /lib64 /usr/lib64
        libs=-lresolv -lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lpthread -lc
        perllibs=-lresolv -lnsl -ldl -lm -lcrypt -lutil -lpthread -lc
        libc=, so=so, useshrplib=true, libperl=libperl.so
        gnulibc_version='2.5'
      Dynamic Linking:
        dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E -Wl,-rpath,/usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/CORE'
        cccdlflags='-fPIC', lddlflags='-shared -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'
    
    
    Characteristics of this binary (from libperl): 
      Compile-time options: MULTIPLICITY PERL_IMPLICIT_CONTEXT
                            PERL_MALLOC_WRAP USE_64_BIT_ALL USE_64_BIT_INT
                            USE_ITHREADS USE_LARGE_FILES USE_PERLIO
                            USE_REENTRANT_API
    

    最佳答案

    稍微简化一下,浮点数以二进制存储;也就是说,每个数字在内部存储为一定数量的尾数位(有效数字)(通常为 53)和一个指数,该指数显示与该尾数相乘的 2 的幂。

    大多数十进制数不能完全以这种格式表示。例如,8458.8 可能表示为 0b10000100001010110011001100110011001100110011001100110 * 2**-39。这是一个略小于 8458.8 的数字,但却是最接近的可表示数字。因为比较小,如果乘以100然后int,会得到845879,而不是845880。

    您将输入乘以 10 的大幂,如果数字被精确存储,则将给出一个整数。因为你知道它应该是一个整数,所以你应该在那个点取整;那么表示为比确切数字略小或略大的数字将是正确的:

    use strict;
    use warnings;
    
    my $linetotalinclusive = int( "8458.80" * 1_000_000 + .5 );
    
    $linetotalinclusive = $linetotalinclusive;
    
    my $c = int( "7980.00" * 1_000_000 + .5 );
    
    my $data = $linetotalinclusive - $c;
    
    print "$linetotalinclusive - $c = $data \n";
    

    或者,在您的计算之后四舍五入。

    使用 uselongdouble 编译 perl 的建议(如果您的机器支持)将使用 64 位精度而不是 53。这将影响特定数字是否表示为大于或小于确切值,但仍然会有一些数字每一种方式。

    关于perl 整数算术给出浮点答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42513578/

    相关文章:

    perl - 如何找到模块名称到不透明指针的 DynaLoader 映射?

    正则表达式匹配排除其他一些词

    c++ - 从 float 中删除小数部分但保留类型的有效方法

    floating-point - 哪些编程语言具有任意精度的浮点文字?

    java - 拆分包含两个 (x,y) 整数的字符串 - Java

    sql - 将整数转换为十六进制并将十六进制转换为整数

    perl - 如何在嵌入式系统上安装缺少的 perl 模块?

    xml - 如何提取 XML 并将其转换为 Perl 数据结构

    java - 在Java中,如何删除float中的所有0?

    python - 计算由空格分隔的未指定数量的整数并使用字典查找 python 中每个数字的出现次数