HH (version 3.1-21)

formatHex: Format numbers in binary and hex and decimal format, and scan binary and hex.

Description

Format numbers in binary and hex and decimal format, and scan binary and hex. This is an early version of functions which will eventually be in Rmpfr.

Usage

formatHex(x, ...)

formatBin(x, scientific=TRUE, left.pad="_", right.pad=left.pad, ...)

formatDec(x, displaydigits=decdigits, digits, nsmall, ...)

scanBin(x, precBits=stop("Must specify precBits.", call.=FALSE), scientific=TRUE)

scanHex(x, precBits=NULL)

## not exported formatHexInternal(x, precBits=min(Rmpfr::getPrec(x)), ...)

Arguments

x
For the formatXXX functions, any numeric or mpfr object. For the scanXXX functions, the result of a previous formatXXX function.
precBits
Number of bits of precision. The default value is the number returned by Rmpfr::mpfr_default_prec() Double precision numbers have 53 bits. For more detail, see mpfr.

For the

scientific
Logical. When TRUE (the default) formatBin will display the binary representation in scientific notation (mpfr(3, 5) is displayed as +0b1.1000p+1). When FALSE, formatBin
...
Additional arguments. formatHexInternal, formatHex, formatBin: precBits is the only ... argument acted on. Other ... arguments are ignored.

formatDec: pr

left.pad, right.pad
Characters that will be used for left- and right-padding of the formatted string when shift=TRUE.
digits, nsmall
These arguments are intercepted by formatDec and ignored. formatDec generates its own values for these arguments from precBits and forwards those values to format.
displaydigits
The number of decimal digits displayed is the larger of this argument and the internally generated value that is a function of precBits.

Value

  • For the formatXXX functions, a character matrix containing the formatted represention of the argument. Arithmetic cannot be done on the result.

    For the scanXXX functions, a mpfr object containing the value of the argument.

Details

For the hexadecimal representation, the function sprintf is used directly. For the binary representation, the hexadecimal value is calculated and then edited by substitution of the binary representation of the hex characters coded in the HextoBin vector. For binary with shift=TRUE, the result of the shift=FALSE version is edited to align binary points. For the decimal representation, the hexadecimal value is calculated with the specified precision and then sent to the format function for scientific=FALSE or to the sprintf function for scientific=TRUE.

scanBin reads the output of formatBin. scanHex reads the output of formatHex.

References

R FAQ 7.31: Why doesn't R think these numbers are equal? system.file("../../doc/FAQ")

See Also

mpfr, sprintf

Examples

Run this code
FourBits <- Rmpfr::mpfr(matrix(0:31, 8, 4), precBits=4) ## 4 significant bits
dimnames(FourBits) <- list(0:7, c(0,8,16,24))
FourBits

formatHex(FourBits)
formatBin(FourBits)
formatBin(FourBits, shift=TRUE)
formatDec(FourBits)

FBB <- formatBin(FourBits)
scanBin(FBB, precBits=4)

FBH <- formatHex(FourBits)
scanHex(FBH)

TenFrac <- matrix((1:10)/10, dimnames=list(1:10, expression(1/x)))
TenFrac9 <- Rmpfr::mpfr(TenFrac, precBits=9) ## 9 significant bits
TenFrac9
formatHex(TenFrac9)
formatBin(TenFrac9)
formatBin(TenFrac9, shift=TRUE)
formatDec(TenFrac9)
formatDec(TenFrac9, precBits=10)

Run the code above in your browser using DataCamp Workspace