fmt: Auxiliary Formatting Functions

Description Usage Arguments Details Examples

Description

Auxiliary functions for displaying numeric elements in exercises.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
fmt(x, digits = 2L, zeros = digits < 4L, ...)

round2(x, digits = 0) 

char_with_braces(x)

num_to_tol(x, reltol = 0.0002, min = 0.01, digits = 2)

## S3 method for class 'matrix'
toLatex(object, skip = FALSE, fix = getOption("olat_fix"),
  escape = TRUE, ...)

Arguments

x

numeric vector.

digits

integer. Digits that should be used for rounding.

zeros

logical. Should trailing zeros be added?

reltol

numeric. Relative tolerance (relative to correct solution x).

min

numeric. Minimum absolute tolerance.

object

matrix.

skip

logical. Should an additional skip be added between rows?

fix

logical. Should an additional empty column be added between all columns? This is a workaround for OLAT that collapses spaces between columns in MathML.

escape

logical. Should LaTeX commands be escaped (as appropriate for Sweave) or not (as appropriate for knit)?

...

passed to format for fmt, currently not used in the toLatex method.

Details

Various functions that help displaying numerical results in exercises:

The function fmt rounds and adds trailing zeros (by default if digits is lower than 4).

The function round2 does what is known in German as kaufmaennisches Runden (rounding away from zero for trailing 5s).

The function char_with_braces adds parentheses for negative elements (in order to facilitate their display in equations).

The function num_to_tol (or equivalently num2tol) computes the absolute tolerance based on a numeric solution x and a relative tolerance reltol.

The toLatex method sets up a matrix array with parentheses.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
## emulate how students round
## (rather than using the round-to-even strategy R employs)
round2(c(0.005, 0.015), digits = 2)
round(c(0.005, 0.015), digits = 2)

## this is also employed internally in the fmt() formatting function
fmt(c(0.005, 0.015))

## the main purpose of fmt() is that some numeric result can be displayed
## both at high accuracy and then at the rounding that students should do
## (e.g., with 2 or 3 digits)
sol <- runif(1)
fmt(sol, 6)
fmt(sol, 2)

## but fmt() also assures showing a very high numer of significant digits
## (up to 12)
sol <- 123456 + sol
sol
fmt(sol, 6)
fmt(sol, 2)

## and fmt() also takes care of adding trailing zeros (if digits < 4)
fmt(1)
fmt(1, digits = 3)
fmt(1, digits = 6)

## char_with_braces() is for adding parentheses, e.g., before constructing a sum
paste(char_with_braces(-2:2), collapse = " + ")

## for including a matrix in a LaTeX formula
x <- matrix(1:4, ncol = 2)
toLatex(x)
toLatex(x, skip = TRUE)

## compute absolute tolerances:
## minimum is 0.01
num_to_tol(1)
## but can be larger for larger solutions
num_to_tol(100)

flaviobarros/exams documentation built on May 28, 2019, 8:39 p.m.