Skip to contents

We implemented methods for several base generics for the angle() vectors.

Usage

# S3 method for class 'angle'
as.double(x, unit = angular_unit(x), ...)

# S3 method for class 'angle'
as.complex(x, modulus = 1, ...)

# S3 method for class 'angle'
format(x, unit = angular_unit(x), ..., use_unicode = is_utf8_output())

# S3 method for class 'angle'
print(x, unit = angular_unit(x), ..., use_unicode = is_utf8_output())

# S3 method for class 'angle'
abs(x)

Arguments

x

angle() vector

unit

A string of the desired angular unit. Supports the following strings (note we ignore any punctuation and space characters as well as any trailing s's e.g. "half turns" will be treated as equivalent to "halfturn"):

  • "deg" or "degree"

  • "half-revolution", "half-turn", or "pi-radian"

  • "gon", "grad", "grade", or "gradian"

  • "rad" or "radian"

  • "rev", "revolution", "tr", or "turn"

...

Passed to print.default()

modulus

Numeric vector representing the complex numbers' modulus

use_unicode

If TRUE use Unicode symbols as appropriate.

Value

Typical values as usually returned by these base generics.

Details

  • Mathematical Ops (in particular + and -) for two angle vectors will (if necessary) set the second vector's angular_unit() to match the first.

  • as.numeric() takes a unit argument which can be used to convert angles into other angular units e.g. angle(x, "degrees") |> as.numeric("radians") to cast a numeric vector x from degrees to radians.

  • abs() will calculate the angle modulo full turns.

  • Use is_congruent() to test if two angles are congruent instead of == or all.equal().

  • Not all implemented methods are documented here and since angle() is a numeric() class many other S3 generics besides the explicitly implemented ones should also work with it.

Examples

  # Two "congruent" angles
  a1 <- angle(180, "degrees")
  a2 <- angle(pi, "radians")

  print(a1)
#> <angle<degrees>[1]>
#> [1] 180°
  print(a1, unit = "radians")
#> <angle<degrees>[1]>
#> [1] 3.141593 rad
  print(a1, unit = "pi-radians")
#> <angle<degrees>[1]>
#> [1] 1π rad

  cos(a1)
#> [1] -1
  sin(a1)
#> [1] 0
  tan(a1)
#> [1] 0

  # mathematical operations will coerce second `angle()` object to
  # same `angular_unit()` as the first one
  a1 + a2
#> <angle<degrees>[1]>
#> [1] 360°
  a1 - a2
#> <angle<degrees>[1]>
#> [1] 0°

  as.numeric(a1)
#> [1] 180
  as.numeric(a1, "radians")
#> [1] 3.141593
  as.numeric(a1, "turns")
#> [1] 0.5

  # Use `is_congruent()` to check if two angles are "congruent"
  a1 == a2
#> [1] TRUE
  isTRUE(all.equal(a1, a2))
#> [1] FALSE
  is_congruent(a1, a2)
#> [1] TRUE
  is_congruent(a1, a2, mod_turns = FALSE)
#> [1] TRUE
  a3 <- angle(-180, "degrees") # Only congruent modulus full turns
  a1 == a3
#> [1] FALSE
  isTRUE(all.equal(a1, a2))
#> [1] FALSE
  is_congruent(a1, a3)
#> [1] TRUE
  is_congruent(a1, a3, mod_turns = FALSE)
#> [1] FALSE