line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Number::Range::Regex::Util::inf |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Copyright 2012 Brian Szymanski. All rights reserved. This module is |
4
|
|
|
|
|
|
|
# free software; you can redistribute it and/or modify it under the same |
5
|
|
|
|
|
|
|
# terms as Perl itself. |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package Number::Range::Regex::Util::inf; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# why don't we use perl's "support" for inf? |
10
|
|
|
|
|
|
|
# 1) behaves in various different ways between 5.6.X where X <= 1 |
11
|
|
|
|
|
|
|
# 5.6.Y where Y >= 2, 5.8.X where X <= 7, and 5.8.Y where Y >= 8 |
12
|
|
|
|
|
|
|
# 2) it's annoying - you can't implement a function inf() because of |
13
|
|
|
|
|
|
|
# perl's desire to look like a shell script. because of this, |
14
|
|
|
|
|
|
|
# -inf is interpreted as a bareword so you can say dumb(-foo => bar); |
15
|
|
|
|
|
|
|
# but +inf and inf generate errors about barewords. you can't simply |
16
|
|
|
|
|
|
|
# "fix" that by adding a sub inf { return 'inf' }; because that |
17
|
|
|
|
|
|
|
# generates warnings about -inf being ambiguous between the literal |
18
|
|
|
|
|
|
|
# '-inf' and -&inf(); in caller context. |
19
|
|
|
|
|
|
|
# 3) it is only supported if the underlying libc supports it |
20
|
|
|
|
|
|
|
# 4) it depends on the underlying libc's definition of the string |
21
|
|
|
|
|
|
|
# version of infinity, which on win32 is '1.#INF', solaris |
22
|
|
|
|
|
|
|
# 'Infinity', and libc 'inf' |
23
|
|
|
|
|
|
|
|
24
|
15
|
|
|
15
|
|
1024
|
use strict; |
|
15
|
|
|
|
|
24
|
|
|
15
|
|
|
|
|
865
|
|
25
|
15
|
|
|
15
|
|
78
|
use vars qw ( @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION ); |
|
15
|
|
|
|
|
28
|
|
|
15
|
|
|
|
|
1859
|
|
26
|
|
|
|
|
|
|
eval { require warnings; }; #it's ok if we can't load warnings |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
require Exporter; |
29
|
15
|
|
|
15
|
|
85
|
use base 'Exporter'; |
|
15
|
|
|
|
|
28
|
|
|
15
|
|
|
|
|
3581
|
|
30
|
|
|
|
|
|
|
@ISA = qw( Exporter ); |
31
|
|
|
|
|
|
|
@EXPORT = qw ( pos_inf neg_inf ); |
32
|
|
|
|
|
|
|
@EXPORT_OK = qw ( inf_type _cmp _is_negative _pad ); |
33
|
|
|
|
|
|
|
%EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_OK ] ); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$VERSION = '0.32'; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
use overload '<=>' => \&_cmp, # also defines <, <=, ==, !=, >=, > |
38
|
|
|
|
|
|
|
'+' => \&_add, # with neg, also defines non-unary - |
39
|
|
|
|
|
|
|
'neg' => \&_neg, # unary minus |
40
|
|
|
|
|
|
|
'eq' => \&_eq, # string equality check, always returns false |
41
|
15
|
|
|
15
|
|
93
|
'""' => sub { my $self = shift; return $$self }; |
|
15
|
|
|
20687
|
|
369
|
|
|
15
|
|
|
|
|
291
|
|
|
20687
|
|
|
|
|
28817
|
|
|
20687
|
|
|
|
|
48509
|
|
42
|
|
|
|
|
|
|
|
43
|
16881
|
|
|
16881
|
0
|
25286
|
sub pos_inf { my $v = '+inf'; return bless \$v, __PACKAGE__; } |
|
16881
|
|
|
|
|
73617
|
|
44
|
711
|
|
|
711
|
0
|
1412
|
sub neg_inf { my $v = '-inf'; return bless \$v, __PACKAGE__; } |
|
711
|
|
|
|
|
6288
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# returns -1 if this is neg_inf, 0 if this is non-infinite, 1 if pos_inf |
47
|
|
|
|
|
|
|
sub inf_type { |
48
|
50207
|
|
|
50207
|
0
|
115946
|
my ($val) = @_; |
49
|
50207
|
|
|
|
|
103894
|
my $str_val = "$val"; |
50
|
50207
|
100
|
|
|
|
273189
|
return $str_val eq '-inf' ? -1 : $str_val eq '+inf' ? 1 : 0; |
|
|
100
|
|
|
|
|
|
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub _neg { |
54
|
24
|
|
|
24
|
|
34
|
my ($val) = @_; |
55
|
24
|
100
|
|
|
|
49
|
return pos_inf if inf_type($val) == -1; |
56
|
12
|
|
|
|
|
46
|
return neg_inf; # inf_type($val) == 1 # if we're not -inf, we're +inf |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# we can't do numberic comparisons because of non-base-10 support, |
60
|
|
|
|
|
|
|
# and we don't want to stringify when we can avoid it |
61
|
|
|
|
|
|
|
sub _is_negative { |
62
|
10473
|
|
|
10473
|
|
15266
|
my ($val) = @_; |
63
|
10473
|
|
|
|
|
19435
|
my $inf_type = inf_type($val); |
64
|
10473
|
100
|
|
|
|
66273
|
return $inf_type ? $inf_type == -1 : $val =~ /^-/; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# usage: _pad( $value, $num_extra_leading_zeroes ); |
68
|
|
|
|
|
|
|
sub _pad { |
69
|
142
|
|
|
142
|
|
258
|
my ($val, $extra) = @_; |
70
|
142
|
50
|
|
|
|
305
|
return $val if inf_type($val); |
71
|
142
|
100
|
|
|
|
820
|
return $val =~ s/^-// ? '-'.(0 x $extra).$val : (0 x $extra).$val; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# for our purposes, -inf!=-inf, and +inf!=+inf |
75
|
392
|
|
|
392
|
|
1286
|
sub _eq { return } |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub _add { |
78
|
52
|
|
|
52
|
|
91
|
my ($a, $b, $swapped) = @_; |
79
|
|
|
|
|
|
|
# note: the case of 2 non-infinite numbers never gets here, so the |
80
|
|
|
|
|
|
|
# below is accurate |
81
|
52
|
50
|
|
|
|
97
|
die "neg_inf + pos_inf is undefined" if 0 == inf_type($a) + inf_type($b); |
82
|
|
|
|
|
|
|
# some infinite value k + any value that is not the opposite of itself = k |
83
|
52
|
|
|
|
|
144
|
return $a; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub _cmp { |
87
|
18627
|
|
|
18627
|
|
73879
|
my ($l, $r, $swapped) = @_; |
88
|
18627
|
100
|
|
|
|
59658
|
($l, $r) = ($r, $l) if $swapped; |
89
|
|
|
|
|
|
|
# note: the below would be wrong for the case of 2 non-infinite numbers, |
90
|
|
|
|
|
|
|
# (inf_type($l) == inf_type($r) == 0), but we never check the overloaded |
91
|
|
|
|
|
|
|
# _cmp() in that case, as both $l and $r are non-overloaded & non-infinite |
92
|
18627
|
|
|
|
|
58397
|
return inf_type($l) <=> inf_type($r); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |