Browse Source

Add 'Printing, testing, debugging' chapter

David Peter 2 years ago
parent
commit
a95a16edb6
2 changed files with 44 additions and 0 deletions
  1. 1 0
      book/src/SUMMARY.md
  2. 43 0
      book/src/procedures.md

+ 1 - 0
book/src/SUMMARY.md

@@ -24,6 +24,7 @@
   - [Unit notation]()
   - [Operators and their precedence](./operators.md)
   - [Unit conversions](./unit-conversions.md)
+  - [Printing, testing, debugging](./procedures.md)
   - [Variable definitions]()
   - [Function definitions]()
 - [Advanced](./advanced.md)

+ 43 - 0
book/src/procedures.md

@@ -0,0 +1,43 @@
+# Printing, testing, debugging
+
+## Printing
+
+Numbat has a builtin `print` procedure that can be used to print the value of an expression:
+
+```nbt
+print(2 km/h)
+```
+
+## Testing
+
+The `assert_eq` procedure can be used to test for (approximate) equality of two quantities.
+This is often useful to make sure that (intermediate) results in longer calculations have
+a certain value, e.g. when restructuring the code. The general syntax is
+
+```nbt
+assert_eq(q1, q2)
+assert_eq(q1, q2, ε)
+```
+
+where the first version tests for exact equality while the second version tests for approximate
+equality \\( |q_1-q_2| < \epsilon \\) with a specified accuracy of \\( \epsilon \\). For example:
+
+```nbt
+assert_eq(2 + 3, 5)
+assert_eq(1 ft × 77 in², 4 gal)
+
+assert_eq(alpha, 1 / 137, 1e-4)
+assert_eq(3.3 ft, 1 m, 1 cm)
+``` 
+
+A runtime error is thrown if an assertion fails. Otherwise, nothing happens.
+
+## Debugging
+
+You can use the builtin `type` procedure to see the type (or physical dimension) of a quantity:
+
+```nbt
+>>> type(g0)
+
+  Length / Time²
+```