jdhedden has asked for the wisdom of the Perl Monks concerning the following question:
The following code illustrates:
It produces:#!/usr/bin/perl use strict; use warnings; package aaa; { use overload ( '""' => sub { return(1000000000000000) }, '0+' => sub { return(1000000000000000) }, 'fallback' => 1 ); sub new { return (bless({}, shift)); } } package bbb; { use overload ( '""' => sub { return(999999999999999) }, '0+' => sub { return(999999999999999) }, 'fallback' => 1 ); sub new { return (bless({}, shift)); } } package main; { print("Object overloaded as 16-digit integer\n"); my $a = aaa->new(); print("STRING: $a\n"); print('NUMBER: ', 0 + $a, "\n\n"); print("Object overloaded as 15-digit integer\n"); my $b = bbb->new(); print("STRING: $b\n"); print('NUMBER: ', 0 + $b, "\n\n"); print("Addition with 16-digit integer\n"); print('NUMBER: ', 0 + 1000000000000000, "\n"); }
Object overloaded as 16-digit integer STRING: 1000000000000000 NUMBER: 1e+15 Object overloaded as 15-digit integer STRING: 999999999999999 NUMBER: 999999999999999 Addition with 16-digit integer NUMBER: 1000000000000000It's the '1e+15' above that I object to.
Update:Using int(0 + $a) does restore the result back to an integer, but that doesn't address my desire to negate the floating-point conversion in the first place.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Numeric overloading with 64-bit Perl
by Moron (Curate) on Oct 06, 2005 at 08:12 UTC | |
by jdhedden (Deacon) on Oct 06, 2005 at 13:34 UTC | |
by Moron (Curate) on Oct 06, 2005 at 14:39 UTC | |
|
Re: Numeric overloading with 64-bit Perl
by tonyc (Hermit) on Oct 06, 2005 at 12:11 UTC | |
by jdhedden (Deacon) on Oct 06, 2005 at 13:42 UTC | |
by tonyc (Hermit) on Oct 07, 2005 at 00:51 UTC | |
by jdhedden (Deacon) on Oct 07, 2005 at 12:42 UTC |