Feet And Inches
July 1, 2011
I’ve long had the habit of seeing people post code snippets or problem requests on the web and writing my own code, just as an exercise for myself, to solve their problem; that’s how this blog got started. When I saw this exercise a few days ago, I set out to write it myself. I thought it was too simple to make into an exercise, but then I had several little problems getting it to work right, and realized it would make a very good exercise. Here’s my solution:
(define (feet-and-inches n)
(if (zero? n) "0 feet 0 inches"
(let* ((n (+ n 1/64))
(feet (inexact->exact (floor (/ n 12))))
(inches (inexact->exact (floor (- n (* feet 12)))))
(32nds (/ (inexact->exact (floor (* (- n (* feet 12) inches) 32))) 32)))
(string-append
(if (zero? feet) "" (number->string feet))
(if (zero? feet) "" (if (= feet 1) " foot" " feet"))
(if (zero? inches) "" (if (positive? feet) " " ""))
(if (zero? inches) "" (number->string inches))
(if (zero? 32nds) ""
(if (and (zero? feet) (zero? inches)) ""
(if (zero? inches) " " " and ")))
(if (zero? 32nds) "" (number->string 32nds))
(if (and (zero? inches) (zero? 32nds)) ""
(if (or (zero? inches) (and (= inches 1) (zero? 32nds))) " inch" " inches"))))))
And here’s the test harness I used:
> (for-each
(lambda (n)
(display n) (display #\tab) (display (feet-and-inches n)) (newline))
'(0 0.2785 1.6895 11.9999 12.2785 71.9999 72 72.3492 72.9999 73 73.0135 73.0185 73.8218))
0 0 feet 0 inches
0.2785 9/32 inch
1.6895 1 and 11/16 inches
11.9999 1 foot
12.2785 1 foot 9/32 inch
71.9999 6 feet
72 6 feet
72.3492 6 feet 11/32 inch
72.9999 6 feet 1 inch
73 6 feet 1 inch
73.0135 6 feet 1 inch
73.0185 6 feet 1 and 1/32 inches
73.8218 6 feet 1 and 13/16 inches
You can run the program at http://programmingpraxis.codepad.org/XF5DR2Gc.
My Haskell solution (see http://bonsaicode.wordpress.com/2011/07/01/programming-praxis-feet-and-inches/ for a version with comments):
[…] today’s Programming Praxis exercise, our goal is to convert a decimal length value to the fractions used by […]
Here’s a Python solution:
Also here: http://paste.pocoo.org/show/426731/ and here: http://pastebin.com/McZHhzK0
My solution in Python:
My try in REXX:
I just used ‘ and ” to indicate feet and inches. Until seeing other’s solutions it hadn’t occured to me to spell them out.
Here’s my version modified to print out “feet” and “inches”.
Here’s one in C++
namespace Carpenter
{
class Program
{
static void Caprenter(double measureInches, out int feet, out int inches, out int thirtyseconds)
{
feet = (int)(measureInches / 12);
inches = (int)(measureInches % 12);
thirtyseconds = (int)((measureInches – (feet * 12) – inches) * 32.0);
}
static void Main()
{
double measureInches = 26.375;
int feet, inches, thirtyseconds;
Caprenter(measureInches, out feet, out inches, out thirtyseconds);
System.Console.WriteLine(“Feet {0}, Inches {1}, 1/32 {2}”, feet, inches, thirtyseconds);
}
}
}
This is written in Excel VBA; it is called using: =CarpenterNotation(A1) for the defaults, or by selecting the varous optional parameters.
It can optionally round to various fractions, or 16th’s are the default; typical values would be 8 for 8ths or 16 for 16ths of an inch.
It can optionally round up or down, otherwise standard rounding occurs.
It can optionally display a dash on the left or right side for negative numbers, or parenthesis is the default.
It can optionally display a tilde to represent when the number is now an approximation due to rounding, or not display any notation, or a double-tilde is the default approximation notation.
I made some changes. First I fixed a bug if zero appears in the denominator. I forgot to test for that, so I fixed it. And I made an option to decide if you want to display zero inches when there is feet; otherwise inches will always display if feet is zero.
Also, I decided to go with no approximation indication (FALSE value) or TRUE value to display approximation by using a tilde to show that the actual value is LESS than the rounded value displayed and I show the double tilde if the actual value is GREATER than what is displayed. This lets you know if the display is just a hair light or heavy simply by looking at the tilde’s.
So now TRUE/FALSE is used for the last 2 options:
Sorry, I wrote my notes backwards–I wish that I could edit it. For the tilde indication for my code above here is the rules:
if actual cell value > rounded display value then a tilde
if actual cell value < rounded display value then a double-tilde
saying the same thing in the reverse way:
if rounded display value actual cell value then a double-tilde
This is my final version if you’d like to have it–it’s public domain.
I added some error checking and found a bug if the value was just barely under 1 foot, 2 feet, etc, then it would round up but show 0/16″ Ooops, sorry. It’s fixed now.
I have tested this really well and it works solidly, I hope that you like it, sorry for the different versions, but the final product is nice.
In FORTH, still rounds down sometimes but mostly correct. Also I just print “inch(es)” as shortcut…
Execution:
here’s a python solution i came up with.. precision can be changed in line 6&7… change the 16 to 4, 8, 32, 64 etc.. (it’s in 16ths right now as that’s typically what carpenters use)
7 – 4 3/8