# Program Design for Scientific or Engineering Tasks

Computer Science I · Programming II · https://tryals.app/learn/computer-science-i/program-design-for-scientific-or-engineering-tasks

## Computing for Science

Scientific and engineering programs process heavy numbers, where correctness requires strict **numerical care**.

The first hazard is **floating-point** arithmetic. Computers store real decimals approximately, meaning `0.1 + 0.2` does not equal `0.3`.

| Comparison Type | Code Syntax | Safe for Floats? |
| --- | --- | --- |
| Exact Equality | `a == b` | **No** (fails due to rounding) |
| Tolerance Check | `|a - b| < \varepsilon` | **Yes** (checks if close) |

**Common Pitfall:** Never compare floats with `==`. Accumulating rounding errors mean computation order matters.

## Reliability & Units

Scientific code lives or dies by **units and dimensions**. Mixing meters with feet, or radians with degrees, causes real disaster. Always track units explicitly.

Structure your code for reliability:

* **Clean Data:** Use arrays or matrices for numerical work.
* **Range Validation:** Ensure inputs make physical sense (e.g., mass $\geq 0$, probability in $[0,1]$).
* **Sanity Check:** A quick check on the order of magnitude and sign to catch gross errors that logic misses.

Factor math into tested functions and cross-check results against known limiting behaviors.

## Practice questions

3 of this lesson's 11 practice questions, with answers. The full set is in the app.

### 1. A program computes the mass of a car and reports 1500000 kilograms. Select every check that would have caught this before the number was published.

A. An order-of-magnitude sanity check against a known car mass
B. A range check that a mass must be positive
C. A test that the program produced its output without crashing
D. A cross-check against an independent calculation
E. A comparison of the result against a published value within a tolerance

**Answer:** A. An order-of-magnitude sanity check against a known car mass; D. A cross-check against an independent calculation; E. A comparison of the result against a published value within a tolerance

**Why:** A thousandfold error passes a positivity check and any test that only asks whether the program ran. It is caught by comparing against outside knowledge: the right order of magnitude, an independent method, or a published value. This is why a sanity check is a distinct discipline from validation, one asks whether the answer is legal, the other whether it is believable.

Page: https://tryals.app/practice/computer-science-i/program-design-for-scientific-or-engineering-tasks/a-program-computes-the-mass-of-a-car-and-reports-1500000-kilograms

### 2. Scientific code is often written by researchers rather than software engineers, and produces published results. Which practice pays off most for that kind of code?

A. Testing against known analytic cases with closed-form answers
B. Extensive object-oriented design, which makes the code more maintainable
C. Detailed comments, which substitute for tests in research settings
D. Optimising for speed, since scientific code is computation-bound

**Answer:** A. Testing against known analytic cases with closed-form answers

**Why:** You cannot check the answer you are looking for, so you check the cases where the answer is known, a simplified geometry, a limiting case, a conserved quantity. Convergence studies do the same job. Retracted papers traced to spreadsheet and indexing errors are what the practice is defending against.

Page: https://tryals.app/practice/computer-science-i/program-design-for-scientific-or-engineering-tasks/scientific-code-is-often-written-by-researchers-rather-than-software

### 3. Order the steps of diagnosing and repairing a failing exact-equality test on floating-point values.

**Answer:**

1. Notice that a test fails although the two values print identically
2. Print the values to full precision and see that they differ in the last digits
3. Choose a tolerance small compared with the quantities being compared
4. Replace the equality test with a check that the absolute difference is below that tolerance
5. Re-run and confirm the test now passes for the right reason

**Why:** The full-precision printout is what turns a baffling failure into an understood one, and the tolerance must be sized to the quantities before the comparison can be written. Choosing a tolerance blindly, say 1e-15 on values of order a million, reproduces the original failure with extra steps.

Page: https://tryals.app/practice/computer-science-i/program-design-for-scientific-or-engineering-tasks/order-the-steps-of-diagnosing-and-repairing-a-failing-exact-equality
