Skip to content

Commit

Permalink
Fix and extend paper_size example
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp authored and David Peter committed Jul 1, 2024
1 parent fcae1d2 commit 7c398ca
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
31 changes: 19 additions & 12 deletions book/src/example-paper_size.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,30 @@ struct PaperSize {
height: Length,
}
let size_A0 = PaperSize { width: 841 mm, height: 1189 mm }
fn paper_size_A(n: Scalar) -> PaperSize =
PaperSize {
width: floor(size_A0.width / 2^(n / 2)),
height: floor(size_A0.height / 2^(n / 2)),
}
if n == 0
then
PaperSize {
width: 841 mm,
height: 1189 mm
}
else
PaperSize {
width: floor(paper_size_A(n - 1).height / 2),
height: paper_size_A(n - 1).width,
}
assert_eq(paper_size_A(4).width, 210 mm)
assert_eq(paper_size_A(4).height, 297 mm)
fn paper_area(size: PaperSize) -> Area =
size.width * size.height
assert_eq(paper_size_A(3).width, 297 mm)
assert_eq(paper_size_A(3).height, 420 mm)
assert_eq(paper_size_A(4).width, 210 mm)
assert_eq(paper_size_A(4).height, 297 mm)
fn size_as_string(size: PaperSize) = "{size.width:>4} × {size.height:>5} {paper_area(size) -> cm²:>6.1f}"
fn row(n) = "A{n:<3} {size_as_string(paper_size_A(n))}"
assert_eq(paper_size_A(5).width, 148 mm)
assert_eq(paper_size_A(5).height, 210 mm)
print("Name Width Height Area ")
print("---- ------- -------- ----------")
print(join(map(row, range(0, 10)), "\n"))
```
31 changes: 19 additions & 12 deletions examples/paper_size.nbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,29 @@ struct PaperSize {
height: Length,
}

let size_A0 = PaperSize { width: 841 mm, height: 1189 mm }

fn paper_size_A(n: Scalar) -> PaperSize =
PaperSize {
width: floor(size_A0.width / 2^(n / 2)),
height: floor(size_A0.height / 2^(n / 2)),
}
if n == 0
then
PaperSize {
width: 841 mm,
height: 1189 mm
}
else
PaperSize {
width: floor(paper_size_A(n - 1).height / 2),
height: paper_size_A(n - 1).width,
}

assert_eq(paper_size_A(4).width, 210 mm)
assert_eq(paper_size_A(4).height, 297 mm)

fn paper_area(size: PaperSize) -> Area =
size.width * size.height

assert_eq(paper_size_A(3).width, 297 mm)
assert_eq(paper_size_A(3).height, 420 mm)

assert_eq(paper_size_A(4).width, 210 mm)
assert_eq(paper_size_A(4).height, 297 mm)
fn size_as_string(size: PaperSize) = "{size.width:>4} × {size.height:>5} {paper_area(size) -> cm²:>6.1f}"
fn row(n) = "A{n:<3} {size_as_string(paper_size_A(n))}"

assert_eq(paper_size_A(5).width, 148 mm)
assert_eq(paper_size_A(5).height, 210 mm)
print("Name Width Height Area ")
print("---- ------- -------- ----------")
print(join(map(row, range(0, 10)), "\n"))

0 comments on commit 7c398ca

Please sign in to comment.