Debian Codegraph @

round

raw ◀ ▶
4 Debian patches touch this file
1 #!/usr/bin/perl -w
2
•3 # numround: A program that rounds off numbers it encounters.
4 #
5 # Copyright (C) 2002-2004 Suso Banderas
6
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #
21 # You may contact the author at <[email protected]>.
22
23 #######################
24 # VARIABLES AND SETUP #
25 #######################
26
27 use Getopt::Std;
28 use strict;
29 use vars qw/ %opts $verbose /;
30 use POSIX; # for rounding functions.
•31 +$Getopt::Std::STANDARD_HELP_VERSION = 1;
32
33 getopts('cfn:hdV', \%opts);
34
35 my ($file, @number_array, $verbose);
36
37 if ($opts{'h'}) {
38 &help();
39 exit(0);
40 }
41
42 if ($opts{'d'}) {
43 $verbose = 3;
44 print STDERR "Debug mode\n";
45 } elsif ($opts{'V'}) {
46 $verbose = 2;
47 print STDERR "Verbose mode\n";
48 } elsif ($opts{'q'}) {
49 $verbose = 0; # Nothing except the final answer
50 } else {
51 $verbose = 1; # Normal warnings and such.
52 }
53
54 # If the -n option is set, use the factor from that, otherwise use a factor
55 # of 1 for standard rounding.
56 my $factor = $opts{'n'} || 1;
57
58 ################
59 # MAIN PROGRAM #
60 ################
61
62 # For file args
63 if (@ARGV) {
64 foreach $file (@ARGV) {
65 print STDERR "Reading from file $file.\n" if ($verbose >= 2);
66 open (ARGFILE, "$file") &&
67 process_filehandle(\*ARGFILE, \@number_array) ||
68 $verbose && warn "Couldn't open file $file for reading: $!\n";
69 close (ARGFILE);
70 }
71 # For STDIN
72 } else {
73 print STDERR "Reading from STDIN.\n" if ($verbose >= 2);
74 process_filehandle(\*STDIN, \@number_array);
75 }
76
77 #process_numbers(\@number_array);
78
79 exit(0);
80
81 ###############
82 # SUBROUTINES #
83 ###############
84
85 sub help {
86 print <<"EOF";
87 ---------------------------------------------------------
•88 numround : A program that rounds off numbers it encounters.
89 ---------------------------------------------------------
90 Usage:
91
•92 numround [options] <file>
93 | numround [options]
94 numround [options]
95
96 Options:
97 -c Force the number to be rounded up. Ceiling.
98 -f Force the number to be rounded down. Floor.
99 -n <n> Round numbers to the nearest factor of <n>.
100
101 -d Debug. For developers only.
102 -h Help: You're looking at it.
103 -V Increase verbosity.
104 EOF
105 }
106
•107 sub HELP_MESSAGE{
108 &help();
109 }
110
111 sub process_filehandle {
112 my $filehandle = shift;
113 my $number_array_ref = shift;
114
115 while (<$filehandle>) {
116 if (m/^\s*(\-?[0-9]*\.?[0-9]+)/) {
117 my $number = $1;
118 print STDERR "found number: $1\n" if ($verbose >= 3);
119 my $newnumber;
120 print STDERR "original number: $number\n" if ($verbose >= 2);
121 if ($opts{'c'}) {
•122 $newnumber = round_number($factor, $number, 1);
123 } elsif ($opts{'f'}) {
•124 $newnumber = round_number($factor, $number, -1);
125 } else {
•126 $newnumber = round_number($factor, $number, 0);
127 }
128 print "$newnumber\n";
129 }
130 }
131 return 1;
132 }
133
134 # The simple rounding function. In the next version, this function
135 # will be able to round to arbitrary factors of any number.
136 # The algorithm for this subroutine was inspired by the nearest subroutine
137 # in Geoffrey Rommel's Math::Round perl module. Thanks Geoffrey.
138 sub round_number {
139 my $factor = shift;
140 my $number = shift;
•141 my $direction = shift;
142 my $sign = ( $number >= 0 ? 1 : -1 );
143 my $rounded_number;
144 $factor = abs($factor);
•145 $number += $sign * 0.5 * $factor if $direction == 0;
146
•147 if ($sign >= 0 and $direction == 0 or $direction < 0) {
148 $rounded_number = $factor * floor($number / $factor);
149 } else {
•150 $rounded_number = $factor * ceil($number / $factor);
151 }
152 return $rounded_number;
153 }
154
155
156 sub ceiling_number {
157 my $number = shift;
158 my $ceiling_number = ceil($number);
159 return $ceiling_number;
160 }
161
162 sub floor_number {
163 my $number = shift;
164 my $floor_number = floor($number);
165 return $floor_number;
166 }
167
168 # Lay down some of that perl pod action.
169 =pod
170
171 =head1 NAME
172
•173 numround - A program that rounds off numbers it encounters.
174
175 =head1 SYNOPSIS
176
•177 B<numround> [-cdfhV] <FILE>
178
•179 | B<numround> [-cdfhV] (Input on STDIN from pipeline.)
180
•181 B<numround> [-cdfhV] (Input on STDIN. Use Ctrl-D to stop.)
182
183 =head1 DESCRIPTION
184
•185 B<numround>
186 will simply round decimal numbers to the nearest integer or to a factor of any
187 number using the -n option. You can also force it to round up (ceiling) or
188 round down(floor) using the -c and -f options.
189
190 =head1 OPTIONS
191
192 -c Force the number to be rounded up. Ceiling.
193 -f Force the number to be rounded down. Floor.
194 -n <n> Round numbers to the nearest factor of <n>.
195
•196 -h Print out some helpful information about numround.
197 -V Increase verbosity. This will print out numbers as it finds them.
198 -d Debug mode. For developers only.
199
200
201 =head1 BUGS
202
•203 B<numround> currently only rounds the first number on each line. Eventually this
204 will be changed as all the numeric utilities are developed.
205
•206 B<numround> will drop off the decimal places in decimal numbers. This may cause
207 some calculations to be in error, depending on how you are using the data.
208
209 =head1 SEE ALSO
210
•211 numaverage(1), numbound(1), numinterval(1), numnormalize(1), numgrep(1), numprocess(1), numsum(1), numrandom(1), numrange(1)
212
213 =head1 COPYRIGHT
214
•215 numround is part of the num-utils package, which is copyrighted by
216 Suso Banderas and released under the GPL license. Please read
217 the COPYING and LICENSE files that came with the num-utils package
218
219 Developers can read the GOALS file and contact me about providing
220 submitions or help for the project.
221
222 =head1 MORE INFO
223
•224 More info on numround can be found at:
225
226 =over 1
227
•228 =item B<http://suso.suso.org/xulu/Num-utils>
229
230 =back
231
232 =cut
233