aboutsummaryrefslogtreecommitdiff
path: root/parallel_resistors
diff options
context:
space:
mode:
authorB. Watson <yalhcru@gmail.com>2015-04-08 03:18:53 -0400
committerB. Watson <yalhcru@gmail.com>2015-04-08 03:18:53 -0400
commit122f3c401f23f84799802c7b9667bda222646487 (patch)
treebc77cc44c516eac71b2d6490574fd32a5b5efd65 /parallel_resistors
downloadmisc-scripts-122f3c401f23f84799802c7b9667bda222646487.tar.gz
initial commit
Diffstat (limited to 'parallel_resistors')
-rwxr-xr-xparallel_resistors45
1 files changed, 45 insertions, 0 deletions
diff --git a/parallel_resistors b/parallel_resistors
new file mode 100755
index 0000000..18df62d
--- /dev/null
+++ b/parallel_resistors
@@ -0,0 +1,45 @@
+#!/usr/bin/perl -w
+
+if(@ARGV < 2) {
+ usage();
+ exit 0;
+}
+
+for(@ARGV) {
+ if(/^([\d.]+)k/i) {
+ $_ = $1 * 1000;
+ } elsif(/^([\d.]+)m/i) {
+ $_ = $1 * 1000000;
+ }
+
+ if(not $_+0) {
+ warn "Invalid resistance: $_\n";
+ usage();
+ exit 1;
+ }
+}
+
+$res += 1/$_ for @ARGV;
+printf "%.2f\n", 1/$res;
+#print 1/$res, "\n";
+
+sub usage {
+ my $b = $0;
+ $b =~ s/.*\///;
+ print <<EOF;
+Usage: $b r1 r2 [r3] [r4] [...]
+
+Calculates the total resistance of two or more resistances in parallel,
+according to the formula:
+
+1 / ( (1/r1) + (1/r2) + ... + (1/rN) )
+
+Resistance values can contain a decimal point, and may be given in ohms
+(with no suffix, e.g. 100, 470, 10000), or in units of 1000 ohms (suffix
+"K" or "k": 1K, 4.7k, 0.1k), or units of 1 million ohms (suffix "M" or
+"m": 1M, 4.7m, 0.1m).
+
+Result is always given in ohms.
+
+EOF
+}