## Feet And Inches

### July 1, 2011

Drawing programs measure distances in ten-thousandths of an inch, like 73.0185, but carpenters work in feet, inches, and thirty-seconds of an inch, like 6 feet 1 and 1/32 inches.

Your task is to write a program that takes a measurement in decimal notation and returns the measurement in carpenter’s notation; readers unfamiliar with imperial measurements will want to know that there are twelve inches in a foot. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

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