aboutsummaryrefslogtreecommitdiff
path: root/parallel_resistors
blob: 18df62d4f1f376500f020617589b052e0974bcf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
}