CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Why it uses float instead of integer?

 



porton
Novice

Dec 5, 2015, 7:48 AM

Post #1 of 4 (5842 views)
Why it uses float instead of integer? Can't Post


Code
$ perl -Mstrict -Mwarnings -e 'my $x=2**62; print $x, "\n"' 
4.61168601842739e+18


My Perl runs at 32 bit Debian Linux, but is compiled with 64 bit integers support.

Why 64 bit integers do not work in this case? and it instead uses float.

Is this a bug?

perl5 (revision 5 version 20 subversion 2)


Chris Charley
User

Dec 5, 2015, 11:31 AM

Post #2 of 4 (5832 views)
Re: [porton] Why it uses float instead of integer? [In reply to] Can't Post

Use bignum.


Code
 
C:\Old_Data\perlp>perl -v

This is perl 5, version 14, subversion 1 (v5.14.1) built for MSWin32-x64-multi-thread
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2011, Larry Wall

Binary build 1401 [294969] provided by ActiveState http://www.ActiveState.com
Built Jun 16 2011 17:28:49

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.


C:\Old_Data\perlp>perl -Mbignum -E "print 2**62"
4611686018427387904
C:\Old_Data\perlp>



BillKSmith
Veteran

Dec 5, 2015, 11:56 AM

Post #3 of 4 (5828 views)
Re: [porton] Why it uses float instead of integer? [In reply to] Can't Post

Refer to the section "Integer Arithmetic" in perldoc perlop and all of perdoc integer. The pragma "use integer" affects the result of specified operators. It makes no promise on how this is implemented.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Dec 6, 2015, 9:56 AM

Post #4 of 4 (5806 views)
Re: [porton] Why it uses float instead of integer? [In reply to] Can't Post

I think that it is a limitation of the print function, but that Perl still processes it internally as an integer. On my Cygwin-64 platform:

Code
$ perl -Mstrict -Mwarnings -e 'my $x=2**62; print $x, "\n"' 
4.61168601842739e+18

Same output as you. But if I substract 4.6116e18 from $x, I get an integer output:

Code
$  perl -Mstrict -Mwarnings -e 'my $x=2**62; print $x - 4.6116e18, "\n"' 
86018427387904

and the digits look correct.

And, by the way, the printf function gets the integer output right:

Code
$ perl -Mstrict -Mwarnings -e 'my $x=2**62; printf "%d\n", $x' 
4611686018427387904


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives