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 , probability in ).
- 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.