|
Programming C, bash, Python, Perl, PHP, Java, you name it. |
|
Thread Tools | Display Modes |
|
|||
sh script to convert inches to mm and cm
I found a few interesting DIY wood working projects on the interwebs and because the measurements were in inches I wrote a simple sh script to convert these to millimeters and centimeters.
Code:
#!/bin/sh # use 'bc(1)', the multiprecision calculator to convert inches to mm and cm INCH='25.4' # millimeter (mm) PRECISION=4 # nr of fractional digits VALUE=1 cat <<END You can enter values like '1 1/4' or '2 3/8' inch as '1+1/4' and '2+3/8' ----------------------- Nr of inches: 1/8 1/8 inch = 3.1750 mm or .3175 cm Nr of inches: 1+1/4 1+1/4 inch = 31.7500 mm or 3.1750 cm ----------------------- Press CNTRL-C to exit. END while true ; do printf "\nNr of inches: " read VALUE MM=$( echo "scale = ${PRECISION} ; ( ${VALUE} ) * ${INCH}" | bc ) CM=$( echo "scale = ${PRECISION} ; ${MM} / 10" | bc ) echo "${VALUE} inch = ${MM} mm or ${CM} cm" done Code:
$ ./inch2milli.sh You can enter values like '1 1/4' or '2 3/8' inch as '1+1/4' and '2+3/8' ----------------------- Nr of inches: 1/8 1/8 inch = 3.1750 mm or .3175 cm Nr of inches: 1+1/4 1+1/4 inch = 31.7500 mm or 3.1750 cm ----------------------- Press CNTRL-C to exit. Nr of inches: 2 2 inch = 50.8 mm or 5.0800 cm Nr of inches: 4 4 inch = 101.6 mm or 10.1600 cm Nr of inches: 1/16 1/16 inch = 1.5875 mm or .1587 cm Nr of inches: 3/4 3/4 inch = 19.0500 mm or 1.9050 cm Nr of inches: ^C$
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
|||
Interesting, but rather complicated ;-).
bc(1) uses a simple C-like language for arithmetic expressions. After parsing/pre-processing then feeds it to dc(1), a reverse-Polish notation unlimited precision utility. I find it rather easy to use. Another way is to use it from the command line like: Code:
$ bc -e 'scale=4 ; (1/8) * 25.4' -e 'quit' 3.1750 $ echo 'scale=4 ; (1+1/4) * 25.4' | bc 31.7500
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
||||
bc and dc are certainly practical and powerful tools. They can do things that would go far beyond what is realistically feasible with shell arithmetic. Or for simpler tasks, like inches --> cm, they can be used to get results quickly (assuming one knows how to use them). For a project like yours, that's important, after all I'm sure what matters most is cutting or drilling the wood at the right size, and not getting lost in arcane arithmetic.
I vaguely recalled there were such calculation tools in Unix systems --- I think they've even been mentioned on daemonforums before --- but couldn't have remembered their names if asked. So this thread is a helpful reminder about them for one day when I might need to use them. The shell approach just seemed like a natural question in the nature of minimalism, and for me at least is the kind of puzzle that's a lot of fun and hard to stop playing with once the question is posed. |
|
|||
Interestingly, these two scripts are not identical. They produce different outputs.
Try using the number 5/9 as your inches input. IdOp's script will produce: Code:
>> 0 + 5 / 9 in = 1.4111 cm Code:
5/9 inch = 14.1097 mm or 1.4109 cm https://www.wolframalpha.com/input/?...9+inches+in+cm How do we reconcile this? This is because in J65nko's script, bc(1)'s scale factor is set to 4, which causes a premature truncation of the division. If you want bc(1) to output without the premature truncation, you should increase the scale factor beyond what you want for output (let's say 2x for safety), then use printf(1) to cap your output. Something like this will indeed print the correct 1.4111: Code:
$ echo 'scale=8 ; (5/9) * 2.54' | bc | xargs printf "%.4f\n" Code:
PRECISION=4 # nr of fractional digits MM=$( echo "scale = ${PRECISION} ; ( ${VALUE} ) * ${INCH}" | bc ) CM=$( echo "scale = ${PRECISION} ; ${MM} / 10" | bc ) Code:
PRECISION=8 # nr of fractional digits * 2 MM=$( echo "scale = ${PRECISION} ; ( ${VALUE} ) * ${INCH}" | bc | xargs printf "%.4f\n" ) CM=$( echo "scale = ${PRECISION} ; ${MM} / 10" | bc | xargs printf "%.4f\n" ) |
|
||||
That's interesting. It's conceivable that for any given safety factor, such as using 8 digits, there might be some narrow special case fraction inputs such that the rounded results are still different. (Just speculation on my part.) As for checking of my script's results, I just used a hand canculator, e.g.,
(5/9)* 2.54 = 1.41111111... and observed it seemed to round to 4 digits properly in the few cases I checked. |
|
|||
Ibara, thanks for your remarks.
The rounding error is because the script evolved from using only whole non-fractional input to fractional input for drilling holes , like 1/4, 1/6, 1/8, 1/16. In the metric system we have drill bit sizes ranging from 1, 1,5, 2, 3.5, 4, 4.5 mm and so on. When somebody recommends to use a 1/16" drill bit for a simple DIY drip irrigation system, I needed to see which of my metric drill bits is the best equivalent. Hence the 4 digit precision. Code:
$ echo 'scale = 4; 25.4 / 16' | bc 1.5875 Then I needed to convert PVC pipe diameters like 1-1/4". For my script to handle that I had to wrap the '1 + 1/4" inside parentheses to get the correct answer: Code:
$ echo 'scale=4 ; 25.4 / 4 ' | bc 6.3500 $ echo 'scale=4 ; 6.35 + 25.4 ' | bc 31.75 # wrong! $ echo 'scale=4; 1 + 1 / 4 * 25.4' | bc 7.3500 # correct $ echo 'scale=4; (1 + 1 / 4) * 25.4' | bc 31.7500 To prevent the 5/9 rounding issue, a multiplication of 5 * 25.4 and then a division by 9 would be another method: Code:
$ echo 'scale = 4 ; 5 * 25.4 / 9' | bc 14.1111
__________________
You don't need to be a genius to debug a pf.conf firewall ruleset, you just need the guts to run tcpdump |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Problem with convert | aleunix | OpenBSD Packages and Ports | 2 | 10th May 2012 01:52 PM |
How to convert "no-nat" to new OpenBSD 4.7 NAT/RDR syntax | J65nko | Guides | 0 | 3rd April 2011 12:14 AM |
How to convert MD5 hash into shadow format? | guitarscn | General software and network | 2 | 29th November 2010 12:54 PM |
Want to convert my server to raid | revzalot | OpenBSD Installation and Upgrading | 2 | 16th September 2009 07:56 PM |
Convert varchar () to datetime datatype in MYSQL | cksraj | Programming | 1 | 22nd June 2009 12:53 PM |