Skip to content

Commit

Permalink
Add acidity example
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp authored and David Peter committed Jul 26, 2023
1 parent 9d17abb commit 3c31c3f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions book/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ generate_example musical_note_frequency "Musical note frequency" true
generate_example body_mass_index "Body mass index" true
generate_example pipe_flow_rate "Flow rate in a pipe" true
generate_example recipe "Recipe" true
generate_example acidity "Acidity" true
generate_example xkcd_2585 "XKCD 2585" false

generate_example numbat_syntax "Syntax overview" false
Expand Down
1 change: 1 addition & 0 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [Body mass index](./example-body_mass_index.md)
- [Flow rate in a pipe](./example-pipe_flow_rate.md)
- [Recipe](./example-recipe.md)
- [Acidity](./example-acidity.md)
- [XKCD 2585](./example-xkcd_2585.md)

# Numbat language reference
Expand Down
13 changes: 13 additions & 0 deletions book/src/example-acidity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- This file is autogenerated! Do not modify it -->

# Acidity

``` numbat
# Compute the pH (acidity) of a solution based
# on the activity of hydrogen ions
fn pH_acidity(activity_hplus: Concentration) -> Scalar =
- log10(activity_hplus / (mol / L))
print(pH_acidity(5e-6 mol / L))
```
6 changes: 3 additions & 3 deletions book/src/example-numbat_syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ let y3: Length / Time = 2 m/s # more complex type annotation
# 5. Function definitions
fn foo(x: Scalar) -> Scalar = 2 * x + 3 # A simple function
fn speed(s: Length, t: Time) -> Speed = s / t # Two parameters
fn my_sqrt<T>(x: T^2) -> T = x^(1/2) # A generic function
fn foo(z: Scalar) -> Scalar = 2 * z + 3 # A simple function
fn speed(len: Length, dur: Time) -> Speed = len / dur # Two parameters
fn my_sqrt<T>(q: T^2) -> T = q^(1/2) # A generic function
# 6. Dimension definitions
Expand Down
10 changes: 10 additions & 0 deletions examples/acidity.nbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Compute the pH (acidity) of a solution based
# on the activity of hydrogen ions
#
# https://en.wikipedia.org/wiki/PH

fn pH_acidity(activity_hplus: Concentration) -> Scalar =
- log10(activity_hplus / (mol / L))

print(pH_acidity(5e-6 mol / L))
assert_eq(pH_acidity(5e-6 mol / L), 5.3, 0.1)

0 comments on commit 3c31c3f

Please sign in to comment.