Debian Codegraph @

round

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