| 1 | package Number::Spice; | ||||||||||||
| 2 | # $Id: Spice.pm,v 1.7 2000/09/22 15:32:04 verhaege Exp $ | ||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use vars qw($RE_NUMBER $RE_SPICE_SUFFIX $RE_SPICE_NUMBER @RE_SUFFIX_VAL | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | $VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS); | |||||||||||
| 6 | 1 | ▶ | use Exporter; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use base qw(Exporter); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | $VERSION = '0.011'; | |||||||||||
| 9 | 1 | @EXPORT = (); | |||||||||||
| 10 | 1 | @EXPORT_OK = qw( | |||||||||||
| 11 | $RE_NUMBER $RE_SPICE_SUFFIX $RE_SPICE_NUMBER | ||||||||||||
| 12 | pattern is_spice_number split_spice_number suffix_value | ||||||||||||
| 13 | spice_to_number number_to_spice normalize_spice_number | ||||||||||||
| 14 | ); | ||||||||||||
| 15 | 1 | %EXPORT_TAGS = ( | |||||||||||
| 16 | convert => [qw(spice_to_number number_to_spice normalize_spice_number)], | ||||||||||||
| 17 | re => [qw($RE_NUMBER $RE_SPICE_SUFFIX $RE_SPICE_NUMBER)], | ||||||||||||
| 18 | all => \@EXPORT_OK, | ||||||||||||
| 19 | ); | ||||||||||||
| 20 | |||||||||||||
| 21 | 1 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | # regular expression matching a plain number | ||||||||||||
| 23 | 1 | $RE_NUMBER = qr/ | |||||||||||
| 24 | (?<!\w) # start of a number, not the continuation of an identifier like 'cap12' | ||||||||||||
| 25 | [+-]? # optional sign | ||||||||||||
| 26 | (?:(?:\d+(?:\.\d*)?)|(?:\.\d+)) # mantissa | ||||||||||||
| 27 | (?:e[+-]?\d+)? # optional exponent | ||||||||||||
| 28 | /ix; | ||||||||||||
| 29 | |||||||||||||
| 30 | # regular expression matching a spice suffix (special care is taken not to match an exponent) | ||||||||||||
| 31 | 1 | $RE_SPICE_SUFFIX = qr/ | |||||||||||
| 32 | (?:[a-df-z][a-z]*) # any word not starting with E | ||||||||||||
| 33 | |(?:e[a-z]+) # OR an E followed by other characters | ||||||||||||
| 34 | /ix; | ||||||||||||
| 35 | |||||||||||||
| 36 | # regular expression matching a spice number | ||||||||||||
| 37 | 1 | $RE_SPICE_NUMBER = qr/${RE_NUMBER}${RE_SPICE_SUFFIX}?\b/; | |||||||||||
| 38 | |||||||||||||
| 39 | # list of known spice suffices (as regular expressions) with their numerical value | ||||||||||||
| 40 | 1 | @RE_SUFFIX_VAL = ( | |||||||||||
| 41 | [qr/^t/i, 1e12], # tera | ||||||||||||
| 42 | [qr/^g/i, 1e9], # giga | ||||||||||||
| 43 | [qr/^(?:x|(?:meg))/i, 1e6], # mega | ||||||||||||
| 44 | [qr/^k/i, 1e3], # kilo | ||||||||||||
| 45 | [qr/^m(?!il)/i, 1e-3], # milli | ||||||||||||
| 46 | [qr/^u/i, 1e-6], # micro | ||||||||||||
| 47 | [qr/^n/i, 1e-9], # nano | ||||||||||||
| 48 | [qr/^p/i, 1e-12], # pico | ||||||||||||
| 49 | [qr/^f/i, 1e-15], # femto | ||||||||||||
| 50 | [qr/^a/i, 1e-18], # atto | ||||||||||||
| 51 | [qr/^mil/i, 2.54e-5], # mil (1/1000 inch) | ||||||||||||
| 52 | ); | ||||||||||||
| 53 | } | ||||||||||||
| 54 | |||||||||||||
| 55 | 0 | ▶ | sub pattern { | ||||||||||
|
Subroutine
pattern: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 56 | 0 | return $RE_SPICE_NUMBER; | |||||||||||
| 57 | } | ||||||||||||
| 58 | |||||||||||||
| 59 | 1 | ▶ | sub is_spice_number { | ||||||||||
|
Subroutine
is_spice_number: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 60 | 1 | return $_[0] =~ /^\s*${RE_SPICE_NUMBER}\s*$/; # delimiting whitespace is allowed | |||||||||||
| 61 | } | ||||||||||||
| 62 | |||||||||||||
| 63 | 20 | ▶ | sub split_spice_number { | ||||||||||
|
Subroutine
split_spice_number: called CC=4 SCAR=16.1
Pod
documented
| |||||||||||||
| 64 | 20 | my $str = shift; | |||||||||||
| 65 | 20 | ▶ | if($str =~ /^\s*($RE_NUMBER)($RE_SPICE_SUFFIX?)\s*$/) { # the suffix is optional | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 66 | 20 | ▶ | return ($1,$2 || ''); | ||||||||||
|
Condition100%2/2
$2 || ''
MC/DC100%1/1
$2 || ''
$2
Truth table100%2/2
A
A= $2
| |||||||||||||
| 67 | } | ||||||||||||
| 68 | else { # not a spice number | ||||||||||||
| 69 | 0 | ▶ | return wantarray ? () : undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | } | ||||||||||||
| 71 | } | ||||||||||||
| 72 | |||||||||||||
| 73 | 20 | ▶ | sub suffix_value { | ||||||||||
|
Subroutine
suffix_value: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 74 | 20 | my $suffix = shift; | |||||||||||
| 75 | |||||||||||||
| 76 | # try all known suffices | ||||||||||||
| 77 | 20 | foreach(@RE_SUFFIX_VAL) { | |||||||||||
| 78 | 150 | ▶ | return $_->[1] if $suffix =~ $_->[0]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | # Not a recognized suffix. | ||||||||||||
| 82 | # It is standard spice policy to discard the suffix in this case, | ||||||||||||
| 83 | # which corresponds to a multiplication with 1.0. | ||||||||||||
| 84 | 6 | return 1; | |||||||||||
| 85 | } | ||||||||||||
| 86 | |||||||||||||
| 87 | 20 | ▶ | sub spice_to_number { | ||||||||||
|
Subroutine
spice_to_number: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||
| 88 | 20 | my $spice_number = shift; | |||||||||||
| 89 | 20 | ▶ | defined($spice_number) or | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | die "No argument given to spice_to_number()"; | ||||||||||||
| 91 | |||||||||||||
| 92 | 20 | ▶ | my ($number,$suffix) = split_spice_number($spice_number) or | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 93 | die "Not a spice number: `$spice_number'"; | ||||||||||||
| 94 | |||||||||||||
| 95 | 20 | return $number * suffix_value($suffix); | |||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | 19 | ▶ | sub number_to_spice { | ||||||||||
|
Subroutine
number_to_spice: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 99 | 19 | my $number = shift; | |||||||||||
| 100 | 19 | my $abs_number = abs($number); | |||||||||||
| 101 | 19 | my $suffix = ''; | |||||||||||
| 102 | |||||||||||||
| 103 | # find the appropriate suffix | ||||||||||||
| 104 | 19 | foreach( | |||||||||||
| 105 | [1e12, 't'], | ||||||||||||
| 106 | [1e9, 'g'], | ||||||||||||
| 107 | [1e6, 'meg'], | ||||||||||||
| 108 | [1e3, 'k'], | ||||||||||||
| 109 | [1e0, ''], # if not introduced, 3.14 would be converted to '3140m' !! | ||||||||||||
| 110 | [1e-3, 'm'], | ||||||||||||
| 111 | [1e-6, 'u'], | ||||||||||||
| 112 | [1e-9, 'n'], | ||||||||||||
| 113 | [1e-12, 'p'], | ||||||||||||
| 114 | [1e-15, 'f'], | ||||||||||||
| 115 | [1e-18, 'a'], | ||||||||||||
| 116 | ) { | ||||||||||||
| 117 | 125 | ▶ | if($abs_number >= $_->[0]) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 118 | 17 | $number /= $_->[0]; | |||||||||||
| 119 | 17 | $suffix = $_->[1]; | |||||||||||
| 120 | 17 | last; | |||||||||||
| 121 | } | ||||||||||||
| 122 | } | ||||||||||||
| 123 | # in case $abs_number < 1E-18, the suffix remains '' | ||||||||||||
| 124 | |||||||||||||
| 125 | # format the adjusted number and suffix into a string | ||||||||||||
| 126 | 19 | return sprintf("%g$suffix",$number); | |||||||||||
| 127 | } | ||||||||||||
| 128 | |||||||||||||
| 129 | 7 | ▶ | sub normalize_spice_number { | ||||||||||
|
Subroutine
normalize_spice_number: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 130 | 7 | return number_to_spice(spice_to_number($_[0])); | |||||||||||
| 131 | } | ||||||||||||
| 132 | |||||||||||||
| 133 | 1 | 1; | |||||||||||
| 134 | |||||||||||||
| 135 | __END__ | ||||||||||||