| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # Copyright (c) 2002 Cunningham & Cunningham, Inc. | 
| 2 |  |  |  |  |  |  | # Released under the terms of the GNU General Public License version 2 or later. | 
| 3 |  |  |  |  |  |  | # | 
| 4 |  |  |  |  |  |  | # Warning: not (yet) a general number usable in all calculations. | 
| 5 |  |  |  |  |  |  | # | 
| 6 |  |  |  |  |  |  | # Perl port by Martin Busik | 
| 7 |  |  |  |  |  |  | # | 
| 8 |  |  |  |  |  |  | package Test::C2FIT::ScientificDouble; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | sub new { | 
| 11 | 4 |  |  | 4 | 0 | 8 | my $pkg       = shift; | 
| 12 | 4 |  |  |  |  | 10 | my $value     = shift; | 
| 13 | 4 |  |  |  |  | 9 | my $precision = precision($value); | 
| 14 | 4 | 50 |  |  |  | 10 | $pkg = ref($pkg) if ref($pkg); | 
| 15 | 4 |  |  |  |  | 18 | my $self = bless { value => $value, precision => $precision }, $pkg; | 
| 16 | 4 |  |  |  |  | 11 | return $self; | 
| 17 |  |  |  |  |  |  | } | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | sub equals { | 
| 20 | 2 |  |  | 2 | 0 | 4 | my ( $self, $b ) = @_; | 
| 21 | 2 |  |  |  |  | 6 | return $self->compareTo($b) == 0; | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub toString { | 
| 25 | 0 |  |  | 0 | 0 | 0 | my $self = shift; | 
| 26 | 0 |  |  |  |  | 0 | return $self->{value}; | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | sub precision { | 
| 30 | 4 |  |  | 4 | 0 | 6 | my $value = shift; | 
| 31 | 4 |  |  |  |  | 7 | $value =~ s/^\s+//; | 
| 32 | 4 |  |  |  |  | 10 | $value =~ s/\s+$//; | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 4 |  |  |  |  | 7 | my $bound = tweak($value); | 
| 35 | 4 |  |  |  |  | 20 | return abs( $bound - $value ); | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | sub tweak { | 
| 39 | 4 |  |  | 4 | 0 | 6 | my $s   = shift; | 
| 40 | 4 |  |  |  |  | 8 | my $pos = index( lc($s), "e" ); | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 4 | 50 |  |  |  | 10 | if ( $pos >= 0 ) { | 
| 43 | 0 |  |  |  |  | 0 | return tweak( substr( $s, 0, $pos ) ) . substr( $s, $pos ); | 
| 44 |  |  |  |  |  |  | } | 
| 45 | 4 | 50 |  |  |  | 10 | if ( index( $s, "." ) >= 0 ) { | 
| 46 | 0 |  |  |  |  | 0 | return $s . "5"; | 
| 47 |  |  |  |  |  |  | } | 
| 48 | 4 |  |  |  |  | 10 | return $s . ".5"; | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub compareTo { | 
| 52 | 2 |  |  | 2 | 0 | 3 | my ( $self, $otherObj ) = @_; | 
| 53 | 2 |  |  |  |  | 7 | my $value = $self->{value}; | 
| 54 | 2 |  |  |  |  | 4 | my $other = $otherObj->{value}; | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 2 |  |  |  |  | 4 | my $diff = $value - $other; | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | # warn "COMPARE TO: $value $other $self->{precision}\n"; | 
| 59 | 2 | 50 |  |  |  | 7 | return -1 if $diff < -$self->{precision}; | 
| 60 | 2 | 50 |  |  |  | 6 | return 1  if $diff > $self->{precision}; | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | # java code without perl equivalent: | 
| 63 |  |  |  |  |  |  | #   if (Double.isNaN(value) && Double.isNaN(other)) return 0; | 
| 64 |  |  |  |  |  |  | #   if (Double.isNaN(value)) return 1; | 
| 65 |  |  |  |  |  |  | #   if (Double.isNaN(other)) return -1; | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 2 |  |  |  |  | 21 | return 0; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | 1; |