fcvt: fcvt

View source: R/num2str.R

fcvtR Documentation

fcvt

Description

Converts a number to a string containing either a floating point or a fractional number. Note that a repeating or recurring decimal (a number whose decimal representation becomes periodic) can be also expressed by rational number, e.g. \frac{1}{3}=0.333333333...=0.\overline{3}. It is the workhorse used in num2str.

Usage

fcvt(x, nsmall = 15, plus = FALSE, denom = -1)

Arguments

x

numeric: numbers to convert

nsmall

integer: number of significant digits for the mantissa/significand (default: 16)

plus

logical: should for positive numbers a plus sign used (default: FALSE)

denom

integer: denominator for fractional number

Details

  • If denom is negative then always decimal point numbers are used (default).

  • If denom is zero then a mix of decimal point and fractional numbers are used (whatever is shorter).

  • If denom is one then fractional numbers are used except for integers.

  • If denom is larger one then the denominator is set to denom if possible.

Value

character

Examples

x1 <- c(NA, NaN, -Inf, Inf, 0, pi*10^(-20:20))
fcvt(x1)
x2 <- c(-0.36, 3.6, -30.6, 0.36)
fcvt(x2)
x3 <- c((0:16)/8, 1/3)
fcvt(x3)           # as floating point number, equals denom=-1
fcvt(x3, denom=0)  # as floating point or fractional number
fcvt(x3, denom=1)  # as fractional number except for integers
fcvt(x3, denom=8)  # as fractional number with denominator denom if possible

sigbertklinke/exams2moodle documentation built on July 6, 2023, 3:26 p.m.