Calculus I for Programmers

How tightly to set the dial

How tightly to set the dial

A lathe cuts a rod to a length that depends on one dial. With the dial at setting x, the rod comes out 4 * x - 5 millimetres long. The workshop wants rods 7 mm long, which means the dial belongs at 3.

No dial is set exactly. The operator will be a little off, and the question the workshop actually needs answered is the other way round: given how much error in the finished rod they are willing to accept, how close to 3 does the dial have to be?

Write a function called dial_window that takes a tolerance epsilon, in millimetres of allowed error in the rod, and returns the largest dial error that is guaranteed to keep the rod within that tolerance. In other words, return the largest number d such that every dial setting within d of 3 produces a length within epsilon of 7.

Worked example

Input:  epsilon = 0.4
Output: 0.1

A dial error of d changes the length by 4 * d, since the length
is 4 * x - 5. To keep 4 * d under 0.4, the dial error must stay
under 0.1, and 0.1 is the largest such value.
Input:  epsilon = 1
Output: 0.25

Constraints

  • epsilon is greater than 0 and at most 100.
  • The answer is a single number, not a range.
  • Answers are compared with a tolerance, since this one is a decimal.
Code

Run your code to see how it does on the examples. Submit also runs the hidden cases.