| 1 | 71 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 71 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 71 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package BSON::Decimal128; | ||||||||||||||||||||||||||||
| 6 | # ABSTRACT: BSON type wrapper for Decimal128 | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 71 | ▶ | use version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 71 | our $VERSION = 'v1.12.2'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 71 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 71 | ▶ | use Math::BigInt; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 71 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | #pod =attr value | ||||||||||||||||||||||||||||
| 17 | #pod | ||||||||||||||||||||||||||||
| 18 | #pod The Decimal128 value represented as string. If not provided, it will be | ||||||||||||||||||||||||||||
| 19 | #pod generated from the C<bytes> attribute on demand. | ||||||||||||||||||||||||||||
| 20 | #pod | ||||||||||||||||||||||||||||
| 21 | #pod =cut | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 71 | has 'value' => ( | |||||||||||||||||||||||||||
| 24 | is => 'lazy', | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | #pod =attr bytes | ||||||||||||||||||||||||||||
| 28 | #pod | ||||||||||||||||||||||||||||
| 29 | #pod The Decimal128 value represented in L<Binary Integer | ||||||||||||||||||||||||||||
| 30 | #pod Decimal|https://en.wikipedia.org/wiki/Binary_Integer_Decimal> (BID) format. | ||||||||||||||||||||||||||||
| 31 | #pod If not provided, it will be generated from the C<value> attribute on | ||||||||||||||||||||||||||||
| 32 | #pod demand. | ||||||||||||||||||||||||||||
| 33 | #pod | ||||||||||||||||||||||||||||
| 34 | #pod =cut | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 71 | has 'bytes' => ( | |||||||||||||||||||||||||||
| 37 | is => 'lazy', | ||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 71 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 71 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 71 | PLIM => 34, # precision limit, i.e. max coefficient chars | |||||||||||||||||||||||||||
| 44 | EMAX => 6144, # for 9.999999999999999999999999999999999E+6144 | ||||||||||||||||||||||||||||
| 45 | EMIN => -6143, # for 1.000000000000000000000000000000000E-6143 | ||||||||||||||||||||||||||||
| 46 | AEMAX => 6111, # EMAX - (PLIM - 1); largest encodable exponent | ||||||||||||||||||||||||||||
| 47 | AEMIN => -6176, # EMIN - (PLIM - 1); smallest encodable exponent | ||||||||||||||||||||||||||||
| 48 | BIAS => 6176, # offset for encoding exponents | ||||||||||||||||||||||||||||
| 49 | 71 | }; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 71 | my $digits = qr/[0-9]+/; | |||||||||||||||||||||||||||
| 52 | 71 | my $decimal_re = qr{ | |||||||||||||||||||||||||||
| 53 | ( [-+]? ) # maybe a sign | ||||||||||||||||||||||||||||
| 54 | ( (?:$digits \. $digits? ) | (?: \.? $digits ) ) # decimal-part | ||||||||||||||||||||||||||||
| 55 | ( (?:e [-+]? $digits)? ) # maybe exponent | ||||||||||||||||||||||||||||
| 56 | }ix; | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 605 | ▶ | sub _build_value { | ||||||||||||||||||||||||||
|
Subroutine
_build_value: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | 605 | return _bid_to_string( $_[0]->{bytes} ); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1965 | ▶ | sub _build_bytes { | ||||||||||||||||||||||||||
|
Subroutine
_build_bytes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 1965 | return _string_to_bid( $_[0]->{value} ); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 3172 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | 3172 | my $self = shift; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | croak "One and only one of 'value' or 'bytes' must be provided" | ||||||||||||||||||||||||||||
| 70 | 3172 | ▶ | unless 1 == grep { exists $self->{$_} } qw/value bytes/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # must check for errors and canonicalize value if provided | ||||||||||||||||||||||||||||
| 73 | 3171 | ▶ | if (exists $self->{value}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 1965 | $self->{value} = _bid_to_string( $self->bytes ); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 3040 | return; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 2439 | ▶ | sub _bid_to_string { | ||||||||||||||||||||||||||
|
Subroutine
_bid_to_string: called CC=20 SCAR=30.0
| |||||||||||||||||||||||||||||
| 81 | 2439 | my $bid = shift; | |||||||||||||||||||||||||||
| 82 | 2439 | my $binary = unpack( "B*", scalar reverse($bid) ); | |||||||||||||||||||||||||||
| 83 | 2439 | my ( $coef, $e ); | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # sign bit | ||||||||||||||||||||||||||||
| 86 | 2439 | my $pos = !substr( $binary, 0, 1 ); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # detect special values from first 5 bits after sign bit | ||||||||||||||||||||||||||||
| 89 | 2439 | my $special = substr( $binary, 1, 5 ); | |||||||||||||||||||||||||||
| 90 | 2439 | ▶ | if ( $special eq "11111" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 29 | return "NaN"; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 2410 | ▶ | if ( $special eq "11110" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 66 | ▶ | return $pos ? "Infinity" : "-Infinity"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 2344 | ▶ | if ( substr( $binary, 1, 2 ) eq '11' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | # Bits: 1*sign 2*ignored 14*exponent 111*significand. | ||||||||||||||||||||||||||||
| 99 | # Implicit 0b100 prefix in significand. | ||||||||||||||||||||||||||||
| 100 | 3 | $coef = "" . Math::BigInt->new( "0b100" . substr( $binary, 17 ) ); | |||||||||||||||||||||||||||
| 101 | 3 | $e = unpack( "n", pack( "B*", "00" . substr( $binary, 3, 14 ) ) ) - BIAS; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||||||||||
| 104 | # Bits: 1*sign 14*exponent 113*significand | ||||||||||||||||||||||||||||
| 105 | 2341 | $coef = "" . Math::BigInt->new( "0b" . substr( $binary, 15 ) ); | |||||||||||||||||||||||||||
| 106 | 2341 | $e = unpack( "n", pack( "B*", "00" . substr( $binary, 1, 14 ) ) ) - BIAS; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # Out of range is treated as zero | ||||||||||||||||||||||||||||
| 110 | 2344 | ▶ | if ( length($coef) > PLIM ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 3 | $coef = "0"; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Shortcut on zero | ||||||||||||||||||||||||||||
| 115 | 2344 | ▶ | if ( $coef == 0 && $e == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$coef == 0 and $e == 0
MC/DC100%2/2
$coef == 0 and $e == 0
$coef == 0
$e == 0
Truth table100%3/3
A and B
A= $coef == 0B= $e == 0
| |||||||||||||||||||||||||||||
| 116 | 101 | ▶ | return $pos ? "0" : "-0"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # convert to scientific form ( e.g. 123E+4 -> 1.23E6 ) | ||||||||||||||||||||||||||||
| 120 | 2243 | my $adj_exp = $e + length($coef) - 1; | |||||||||||||||||||||||||||
| 121 | # warn "# XXX COEF: $coef; EXP: $e; AEXP: $adj_exp\n"; | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # exponential notation | ||||||||||||||||||||||||||||
| 124 | 2243 | ▶ | if ( $e > 0 || $adj_exp < -6 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$e > 0 or $adj_exp < -6
MC/DC100%2/2
$e > 0 or $adj_exp < -6
$e > 0
$adj_exp < -6
Truth table100%3/3
A or B
A= $e > 0B= $adj_exp < -6
| |||||||||||||||||||||||||||||
| 125 | # insert decimal if more than one digit | ||||||||||||||||||||||||||||
| 126 | 1244 | ▶ | if ( length($coef) > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 696 | substr( $coef, 1, 0, "." ); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | return ( | ||||||||||||||||||||||||||||
| 131 | 1244 | ▶ | ( $pos ? "" : "-" ) . $coef . "E" . ( $adj_exp >= 0 ? "+" : "" ) . $adj_exp ); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # not exponential notation (integers or small negative exponents) | ||||||||||||||||||||||||||||
| 135 | else { | ||||||||||||||||||||||||||||
| 136 | # e == 0 means integer | ||||||||||||||||||||||||||||
| 137 | 999 | ▶ | return $pos ? $coef : "-$coef" | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 138 | if $e == 0; | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # pad with leading zeroes if coefficient is too short | ||||||||||||||||||||||||||||
| 141 | 694 | ▶ | if ( length($coef) < abs($e) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 348 | substr( $coef, 0, 0, "0" x ( abs($e) - length($coef) ) ); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # maybe coefficient is exact length? | ||||||||||||||||||||||||||||
| 146 | 694 | ▶ | return $pos ? "0.$coef" : "-0.$coef" | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 147 | if length($coef) == abs($e); | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # otherwise length(coef) > abs($e), so insert dot after first digit | ||||||||||||||||||||||||||||
| 150 | 237 | substr( $coef, $e, 0, "." ); | |||||||||||||||||||||||||||
| 151 | 237 | ▶ | return $pos ? $coef : "-$coef"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | my ( $bidNaN, $bidPosInf, $bidNegInf ) = | ||||||||||||||||||||||||||||
| 156 | 71 | map { scalar reverse pack( "B*", $_ . ( "0" x 118 ) ) } qw/ 011111 011110 111110 /; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 119 | ▶ | sub _croak { croak("Couldn't parse '$_[0]' as valid Decimal128") } | ||||||||||||||||||||||||||
|
Subroutine
_croak: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 3 | ▶ | sub _erange { croak("Value '$_[0]' is out of range for Decimal128") } | ||||||||||||||||||||||||||
|
Subroutine
_erange: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 9 | ▶ | sub _erounding { croak("Value '$_[0]' can't be rounded to Decimal128") } | ||||||||||||||||||||||||||
|
Subroutine
_erounding: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 1965 | ▶ | sub _string_to_bid { | ||||||||||||||||||||||||||
|
Subroutine
_string_to_bid: called CC=29 SCAR=33.9
| |||||||||||||||||||||||||||||
| 165 | 1965 | my $s = shift; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | # Check special values | ||||||||||||||||||||||||||||
| 168 | 1965 | ▶ | return $bidNaN if $s =~ /\A -? NaN \z/ix; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 1946 | ▶ | return $bidPosInf if $s =~ /\A \+?Inf(?:inity)? \z/ix; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 1921 | ▶ | return $bidNegInf if $s =~ /\A -Inf(?:inity)? \z/ix; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | # Parse string | ||||||||||||||||||||||||||||
| 173 | 1896 | my ( $sign, $mant, $exp ) = $s =~ /\A $decimal_re \z/x; | |||||||||||||||||||||||||||
| 174 | 1896 | ▶ | $sign = "" unless defined $sign; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 1896 | ▶ | $exp = 0 unless defined $exp && length($exp); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $exp and length $exp
MC/DC100%2/2
defined $exp and length $exp
defined $exp
length $exp
Truth table100%3/3
A and B
A= defined $expB= length $exp
| |||||||||||||||||||||||||||||
| 176 | 1896 | $exp =~ s{^e}{}i; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # Throw error if unparseable | ||||||||||||||||||||||||||||
| 179 | 1896 | ▶ | _croak($s) unless length $exp && defined $mant; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
length $exp and defined $mant
MC/DC50%1/2
length $exp and defined $mant
length $exp
defined $mant
Truth table66%2/3
A and B
A= length $expB= defined $mant
| |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | # Extract sign bit | ||||||||||||||||||||||||||||
| 182 | 1777 | ▶ | my $neg = defined($sign) && $sign eq '-' ? "1" : "0"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $sign and $sign eq "-"
MC/DC50%1/2
defined $sign and $sign eq "-"
defined $sign
$sign eq "-"
Truth table66%2/3
A and B
A= defined $signB= $sign eq "-"
| |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | # Remove leading zeroes unless "0." | ||||||||||||||||||||||||||||
| 185 | 1777 | $mant =~ s{^(?:0(?!\.))+}{}; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | # Locate decimal, remove it and adjust the exponent | ||||||||||||||||||||||||||||
| 188 | 1777 | my $dot = index( $mant, "." ); | |||||||||||||||||||||||||||
| 189 | 1777 | $mant =~ s/\.//; | |||||||||||||||||||||||||||
| 190 | 1777 | ▶ | $exp += $dot - length($mant) if $dot >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | # Remove leading zeros from mantissa (after decimal point removed) | ||||||||||||||||||||||||||||
| 193 | 1777 | $mant =~ s/^0+//; | |||||||||||||||||||||||||||
| 194 | 1777 | ▶ | $mant = "0" unless length $mant; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | # Apply exact rounding if necessary | ||||||||||||||||||||||||||||
| 197 | 1777 | ▶ | if ( length($mant) > PLIM ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 198 | 17 | my $plim = PLIM; | |||||||||||||||||||||||||||
| 199 | 17 | $mant =~ s{(.{$plim})(0+)$}{$1}; | |||||||||||||||||||||||||||
| 200 | 17 | ▶ | $exp += length($2) if defined $2 && length $2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $2 and length $2
MC/DC50%1/2
defined $2 and length $2
defined $2
length $2
Truth table66%2/3
A and B
A= defined $2B= length $2
| |||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | elsif ( $exp < AEMIN ) { | ||||||||||||||||||||||||||||
| 203 | 11 | $mant =~ s{(.*[1-9])(0+)$}{$1}; | |||||||||||||||||||||||||||
| 204 | 11 | ▶ | $exp += length($2) if defined $2 && length $2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $2 and length $2
MC/DC50%1/2
defined $2 and length $2
defined $2
length $2
Truth table66%2/3
A and B
A= defined $2B= length $2
| |||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # Apply clamping if possible | ||||||||||||||||||||||||||||
| 208 | 1777 | ▶ | if ( $mant == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 209 | 482 | ▶ | if ( $exp > AEMAX ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 210 | 12 | $mant = "0"; | |||||||||||||||||||||||||||
| 211 | 12 | $exp = AEMAX; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | elsif ( $exp < AEMIN ) { | ||||||||||||||||||||||||||||
| 214 | 8 | $mant = "0"; | |||||||||||||||||||||||||||
| 215 | 8 | $exp = AEMIN; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | ▶ | elsif ( $exp > AEMAX && $exp - AEMAX <= PLIM - length($mant) ) { | |||||||||||||||||||||||||||
|
Condition100%3/3
$exp > 6111 and $exp - 6111 <= 34 - length($mant)
MC/DC100%2/2
$exp > 6111 and $exp - 6111 <= 34 - length($mant)
$exp > 6111
$exp - 6111 <= 34 - length($mant)
Truth table100%3/3
A and B
A= $exp > 6111B= $exp - 6111 <= 34 - length($mant)
| |||||||||||||||||||||||||||||
| 219 | 76 | $mant .= "0" x ( $exp - AEMAX ); | |||||||||||||||||||||||||||
| 220 | 76 | $exp = AEMAX; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | # Throw errors if result won't fit in Decimal128 | ||||||||||||||||||||||||||||
| 224 | 1777 | ▶ | _erounding($s) if length($mant) > PLIM; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | 1768 | ▶ | _erange($s) if $exp > AEMAX || $exp < AEMIN; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$exp > 6111 or $exp < -6176
MC/DC100%2/2
$exp > 6111 or $exp < -6176
$exp > 6111
$exp < -6176
Truth table100%3/3
A or B
A= $exp > 6111B= $exp < -6176
| |||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | # Get binary representation of coefficient | ||||||||||||||||||||||||||||
| 228 | 1765 | my $coef = Math::BigInt->new($mant)->as_bin; | |||||||||||||||||||||||||||
| 229 | 1765 | $coef =~ s/^0b//; | |||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | # Get 14-bit binary representation of biased exponent | ||||||||||||||||||||||||||||
| 232 | 1765 | my $biased_exp = unpack( "B*", pack( "n", $exp + BIAS ) ); | |||||||||||||||||||||||||||
| 233 | 1765 | substr( $biased_exp, 0, 2, "" ); | |||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | # Choose representation based on coefficient length | ||||||||||||||||||||||||||||
| 236 | 1765 | my $coef_len = length($coef); | |||||||||||||||||||||||||||
| 237 | 1765 | ▶ | if ( $coef_len <= 113 ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 238 | 1765 | substr( $coef, 0, 0, "0" x ( 113 - $coef_len ) ); | |||||||||||||||||||||||||||
| 239 | 1765 | return scalar reverse pack( "B*", $neg . $biased_exp . $coef ); | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | elsif ( $coef_len <= 114 ) { | ||||||||||||||||||||||||||||
| 242 | 0 | substr( $coef, 0, 3, "" ); | |||||||||||||||||||||||||||
| 243 | 0 | return scalar reverse pack( "B*", $neg . "11" . $biased_exp . $coef ); | |||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | else { | ||||||||||||||||||||||||||||
| 246 | 0 | _erange($s); | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | #pod =method TO_JSON | ||||||||||||||||||||||||||||
| 251 | #pod | ||||||||||||||||||||||||||||
| 252 | #pod Returns the value as a string. | ||||||||||||||||||||||||||||
| 253 | #pod | ||||||||||||||||||||||||||||
| 254 | #pod If the C<BSON_EXTJSON> option is true, it will instead | ||||||||||||||||||||||||||||
| 255 | #pod be compatible with MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst> | ||||||||||||||||||||||||||||
| 256 | #pod format, which represents it as a document as follows: | ||||||||||||||||||||||||||||
| 257 | #pod | ||||||||||||||||||||||||||||
| 258 | #pod {"$numberDecimal" : "2.23372036854775807E+57"} | ||||||||||||||||||||||||||||
| 259 | #pod | ||||||||||||||||||||||||||||
| 260 | #pod =cut | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 1522 | ▶ | sub TO_JSON { | ||||||||||||||||||||||||||
|
Subroutine
TO_JSON: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 263 | 1522 | ▶ | return "" . $_[0]->value unless $ENV{BSON_EXTJSON}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 1519 | return { '$numberDecimal' => "" . ($_[0]->value) }; | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 71 | ▶ | use overload ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 268 | 2 | ▶ | q{""} => sub { $_[0]->value }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 269 | 71 | fallback => 1, | |||||||||||||||||||||||||||
| 270 | 71 | ); | |||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 71 | 1; | |||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | =pod | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | =head1 NAME | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | BSON::Decimal128 - BSON type wrapper for Decimal128 | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | =head1 VERSION | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | version v1.12.2 | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | use BSON::Types ':all'; | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | # string representation | ||||||||||||||||||||||||||||
| 291 | $decimal = bson_decimal128( "1.23456789E+1000" ); | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | # binary representation in BID format | ||||||||||||||||||||||||||||
| 294 | $decimal = BSON::Decimal128->new( bytes => $bid ) | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | This module provides a BSON type wrapper for Decimal128 values. | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | It may be initialized with either a numeric value in string form, or | ||||||||||||||||||||||||||||
| 301 | with a binary Decimal128 representation (16 bytes), but not both. | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | Initialization from a string will throw an error if the string cannot be | ||||||||||||||||||||||||||||
| 304 | parsed as a Decimal128 or if the resulting number would not fit into 128 | ||||||||||||||||||||||||||||
| 305 | bits. If required, clamping or exact rounding will be applied to try to | ||||||||||||||||||||||||||||
| 306 | fit the value into 128 bits. | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | =head2 value | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | The Decimal128 value represented as string. If not provided, it will be | ||||||||||||||||||||||||||||
| 313 | generated from the C<bytes> attribute on demand. | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | =head2 bytes | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | The Decimal128 value represented in L<Binary Integer | ||||||||||||||||||||||||||||
| 318 | Decimal|https://en.wikipedia.org/wiki/Binary_Integer_Decimal> (BID) format. | ||||||||||||||||||||||||||||
| 319 | If not provided, it will be generated from the C<value> attribute on | ||||||||||||||||||||||||||||
| 320 | demand. | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | =head1 METHODS | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | =head2 TO_JSON | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | Returns the value as a string. | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | If the C<BSON_EXTJSON> option is true, it will instead | ||||||||||||||||||||||||||||
| 329 | be compatible with MongoDB's L<extended JSON|https://github.com/mongodb/specifications/blob/master/source/extended-json.rst> | ||||||||||||||||||||||||||||
| 330 | format, which represents it as a document as follows: | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | {"$numberDecimal" : "2.23372036854775807E+57"} | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | =for Pod::Coverage BUILD | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | =head1 OVERLOADING | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | The stringification operator (C<"">) is overloaded to return a (normalized) | ||||||||||||||||||||||||||||
| 339 | string representation. Fallback overloading is enabled. | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | =over 4 | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | =item * | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | David Golden <david@mongodb.com> | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =item * | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | Stefan G. <minimalist@lavabit.com> | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | =back | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | This software is Copyright (c) 2020 by Stefan G. and MongoDB, Inc. | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | This is free software, licensed under: | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | The Apache License, Version 2.0, January 2004 | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | =cut | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | __END__ | ||||||||||||||||||||||||||||