The R Project SVN R

Rev

Rev 59482 | Rev 67314 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/base/man/NumericConstants.Rd
2
% Part of the R package, http://www.R-project.org
59482 ripley 3
% Copyright 1995-2012 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
34712 ripley 6
\name{NumericConstants}
56186 murdoch 7
\alias{NumericConstants}
50476 murdoch 8
\alias{1L}
9
\alias{0x1}
10
\alias{1i}
34712 ripley 11
\title{Numeric Constants}
12
\description{
13
  How \R parses numeric constants.
14
}
15
\details{
44834 ripley 16
  \R parses numeric constants in its input in a very similar way to C99
34712 ripley 17
  floating-point constants.
18
 
40535 maechler 19
  \code{\link{Inf}} and \code{\link{NaN}} are numeric constants (with
59482 ripley 20
  \code{\link{typeof}(.) "double"}).  In text input (e.g. in
21
  \code{\link{scan}} and \code{\link{as.double}}), these are recognized
22
  ignoring case as is \code{infinity} as an alternative to \code{Inf}.
44834 ripley 23
  \code{\link{NA_real_}} and \code{\link{NA_integer_}} are constants of
24
  types \code{"double"} and \code{"integer"} representing missing
44840 ripley 25
  values.  All other numeric constants start with a digit or period and
26
  are either a decimal or hexadecimal constant optionally followed by
27
  \code{L}.
34712 ripley 28
 
29
  Hexadecimal constants start with \code{0x} or \code{0X} followed by
46521 ripley 30
  a nonempty sequence from \code{0-9 a-f A-F .} which is interpreted as a
44834 ripley 31
  hexadecimal number, optionally followed by a binary exponent.  A binary
54103 ripley 32
  exponent consists of a \code{P} or \code{p} followed by an optional
44834 ripley 33
  plus or minus sign followed by a non-empty sequence of (decimal)
34
  digits, and indicates multiplication by a power of two.  Thus
35
  \code{0x123p456} is \eqn{291 \times 2^{456}}{291 * 2^456}.
34712 ripley 36
 
44828 ripley 37
  Decimal constants consist of a nonempty sequence of digits possibly
34712 ripley 38
  containing a period (the decimal point), optionally followed by a
39
  decimal exponent.  A decimal exponent consists of an \code{E} or
40
  \code{e} followed by an optional plus or minus sign followed by a
41
  non-empty sequence of digits, and indicates multiplication by a power
42
  of ten.
43
 
44840 ripley 44
  Values which are too large or too small to be representable will
45
  overflow to \code{Inf} or underflow to \code{0.0}.
46
 
34712 ripley 47
  A numeric constant immediately followed by \code{i} is regarded as an
48
  imaginary \link{complex} number.
49
 
40535 maechler 50
  An numeric constant immediately followed by \code{L} is regarded as an
51
  \code{\link{integer}} number when possible (and with a warning if it
52
  contains a \code{"."}).
53
 
34712 ripley 54
  Only the ASCII digits 0--9 are recognized as digits, even in languages
44834 ripley 55
  which have other representations of digits.  The \sQuote{decimal
56
    separator} is always a period and never a comma.
34712 ripley 57
 
44834 ripley 58
  Note that a leading plus or minus is not regarded by the parser as
59
  part of a numeric constant but as a unary operator applied to the constant.
34712 ripley 60
}
51414 ripley 61
\note{
62
  When a string is parsed to input a numeric constant, the number may or
63
  may not be representable exactly in the C double type used.  If not
64
  one of the nearest representable numbers will be returned.
61433 ripley 65
 
54103 ripley 66
  \R's own C code is used to convert constants to binary numbers, so the
67
  effect can be expected to be the same on all platforms implementing
68
  full IEC 600559 arithmetic (the most likely area of difference being
69
  the handling of numbers less than \code{\link{.Machine}$double.xmin}).
70
  The same code is used by \code{\link{scan}}.  } \seealso{
34712 ripley 71
  \code{\link{Syntax}}.
40535 maechler 72
 
34712 ripley 73
  \code{\link{Quotes}} for the parsing of character constants,
74
}
40535 maechler 75
\examples{
76
2.1
77
typeof(2)
78
sqrt(1i) # remember elementary math?
41508 ripley 79
utils::str(0xA0)
40535 maechler 80
identical(1L, as.integer(1))
81
 
82
## You can combine the "0x" prefix with the "L" suffix :
51414 ripley 83
identical(0xFL, as.integer(15))
40535 maechler 84
}
34712 ripley 85
\keyword{documentation}