blib/lib/Data/Convert/MicrochipTechnology/Float.pm

filter: Statementstmt 100% 29 / 29 Branchbran 100% 10 / 10 Conditioncond 60.0% 9 / 15 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 7 / 7 Podpod 100% 4 / 4 totaltotal 83.3% 60 / 72 CC 11 SCAR 14.8
CC
11
Cov
89%
float_from_array
7.2
convert
4.0
new
2.1
CRAP 4.4
↑ Summary
1 package Data::Convert::MicrochipTechnology::Float;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶ use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 $VERSION = '0.03';
7 }
8  
9 =head1 NAME
10  
11 Data::Convert::MicrochipTechnology::Float - Converts Microchip Technology 32-bit float to a number
12  
13 =head1 SYNOPSIS
14  
15 use Data::Convert::MicrochipTechnology::Float;
16 my $object = Data::Convert::MicrochipTechnology::Float->new();
17 my $float=$obj->convert("\0\0\0\0");
18 print "Float: $float\n";
19  
20 =head1 DESCRIPTION
21  
22 The format of the PIC 32-bit float is eeeeeeee smmmmmmm mmmmmmmm mmmmmmmm (4-bytes => 8-bit biased exponent, 1-bit sign, 23-bit significand)
23  
24 The number has value v: v = s * 2**e * m
25 s = +1 (positive numbers) when the sign bit is 0
26 s = -1 (negative numbers) when the sign bit is 1
27 e = Exp - 127 (the exponent is biased with 127)
28 m = 1.fraction in binary (the significand is the binary number 1 followed by the radix point followed by the binary bits of the fraction). Therefore, 1 = m < 2.
29  
30 =head1 USAGE
31  
32 =head1 CONSTRUCTOR
33  
34 =head2 new
35  
36 my $object = Data::Convert::MicrochipTechnology::Float->new();
37  
38 =cut
39  
40 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
41 1 my $this = shift();
42 1 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
43 1 my $self = {};
44 1 bless $self, $class;
45 #$self->initialize(@_);
46 1 return $self;
47 }
48  
49 =head1 METHODS
50  
51 =head2 convert
52  
53 my $float=$obj->convert("\0\0\0\0");
54 my @list=$obj->convert("\0\0\0\0", "\0\0\0\0");
55 my $listref=$obj->convert("\0\0\0\0", "\0\0\0\0");
56 my $float=$obj->convert([0,0,0,0]);
57 my @list=$obj->convert("\0\0\0\0", [0,0,0,0]);
58 my $listref=$obj->convert("\0\0\0\0", [0,0,0,0]);
59  
60 =cut
61  
62 14 ▶sub convert {
Subroutine convert: called CC=4 SCAR=13.9
Pod documented
63 14 my $self=shift();
64 14 ▶ my @list=map {ref($_) eq 'ARRAY' ?
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 4 14 18
65 $self->float_from_array(@$_) :
66 $self->float_from_string($_)} @_;
67 14 ▶ return scalar(@list) == 1 ? $list[0] : (wantarray ? @list : \@list);
Branch: 2 branches
BranchTrueFalseTotal
scalar @list == 1 ? : 10 4 14
wantarray ? : 3 1 4
68 }
69  
70 =head2 float_from_string
71  
72 my $float=$obj->float_from_string("\0\0\0\0");
73  
74 =cut
75  
76 15 ▶sub float_from_string {
Subroutine float_from_string: called CC=1 SCAR=0.0
Pod documented
77 15 my $self=shift();
78 15 my $string=shift();
79 15 return $self->float_from_array(unpack("CCCC", $string));
80 }
81  
82 =head2 float_from_array
83  
84 my $float=$obj->float_from_array(0, 0, 0, 0);
85  
86 =cut
87  
88 20 ▶sub float_from_array {
Subroutine float_from_array: called CC=7 SCAR=19.7
Pod documented
89 20 my $self=shift();
90 #die unless 4 == scalar(@_);
91 20 my ($b0, $b1, $b2, $b3)=@_;
92 20 ▶ if (0==$b0 and (0==$b1 or 128==$b1) and 0==$b2 and 0==$b3) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $b0 and 0 == $b1 || 128 == $b1 and 0 == $b2 and 0 == $b3) { } 7 13 20
Condition66%8/12
0 == $b0 and 0 == $b1 || 128 == $b1 and 0 == $b2 and 0 == $b3
!ll&&!rl&&r
1307
0 == $b0 and 0 == $b1 || 128 == $b1 and 0 == $b2
!ll&&!rl&&r
1307
0 == $b0 and 0 == $b1 || 128 == $b1
!ll&&!rl&&r
1307
0 == $b1 or 128 == $b1
l!l&&r!l&&!r
610
MC/DC20%1/5
0 == $b0 and 0 == $b1 || 128 == $b1 and 0 == $b2 and 0 == $b3
0 == $b0 0 == $b1 128 == $b1 0 == $b2 0 == $b3
Truth table37%3/8
A and B or C and D and E
A= 0 == $b0B= 0 == $b1C= 128 == $b1D= 0 == $b2E= 0 == $b3
ABCDEresult
0XXXX0
100XX0
11X0X0
1010X0
11X100
101100
11X111
101111
93 #eliminates rounding errors for +/- zero
94 7 return 0;
95 } else {
96 13 ▶ my $s = $b1 & 128 ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$b1 & 128 ? : 2 11 13
97 13 my $e = $b0 - 127;
98 13 my $m = 1 + ((($b1 & 127) * 256 + $b2) * 256 + $b3) / (2 ** 23);
99 13 my $v = $s * 2 ** $e * $m;
100 13 return $v;
101 }
102 }
103  
104 =head1 BUGS
105  
106 The math introduces floating point rounding errors.
107  
108 =head1 TODO
109  
110 Add a bit vector capability to eliminate any rounding errors.
111  
112 =head1 SUPPORT
113  
114 =head1 AUTHOR
115  
116 Michael R. Davis
117 CPAN ID: MRDVT
118 DavisNetworks.com
119 account=>perl,tld=>com,domain=>michaelrdavis
120 http://www.davisnetworks.com/
121  
122 =head1 COPYRIGHT
123  
124 This program is free software; you can redistribute
125 it and/or modify it under the same terms as Perl itself.
126  
127 The full text of the license can be found in the
128 LICENSE file included with this module.
129  
130 =head1 SEE ALSO
131  
132 L<Bit::Vector>
133  
134 =cut
135  
136 1 1;
↑ Summary