The R Project SVN R

Rev

Rev 86436 | 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
68948 ripley 2
% Part of the R package, https://www.R-project.org
67314 maechler 3
% Copyright 1995-2015 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
66444 hornik 20
  \code{\link{typeof}(.) "double"}).  In text input (e.g., in
59482 ripley 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
 
90049 hornik 29
  Hexadecimal constants start with \samp{0x} or \samp{0X} followed by
30
  a non-empty sequence from \samp{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
 
86436 ripley 37
  Decimal constants consist of a non-empty 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
 
80492 kalibera 50
  A numeric constant immediately followed by \code{L} is regarded as an
40535 maechler 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
85550 hornik 68
  full \abbr{IEC} 60559 arithmetic (the most likely area of difference being
54103 ripley 69
  the handling of numbers less than \code{\link{.Machine}$double.xmin}).
67314 maechler 70
  The same code is used by \code{\link{scan}}.
71
}
72
\seealso{
34712 ripley 73
  \code{\link{Syntax}}.
67314 maechler 74
  For complex numbers, see \code{\link{complex}}.
34712 ripley 75
  \code{\link{Quotes}} for the parsing of character constants,
67314 maechler 76
  \code{\link{Reserved}} for the \dQuote{reserved words} in \R{}.
34712 ripley 77
}
40535 maechler 78
\examples{
67314 maechler 79
## You can create numbers using fixed or scientific formatting.
40535 maechler 80
2.1
67314 maechler 81
2.1e10
82
-2.1E-10
83
 
84
## The resulting objects have class numeric and type double.
85
class(2.1)
86
typeof(2.1)
87
 
88
## This holds even if what you typed looked like an integer.
89
class(2)
40535 maechler 90
typeof(2)
67314 maechler 91
 
92
## If you actually wanted integers, use an "L" suffix.
93
class(2L)
94
typeof(2L)
95
 
96
## These are equal but not identical
97
2 == 2L
98
identical(2, 2L)
99
 
100
## You can write numbers between 0 and 1 without a leading "0"
101
## (but typically this makes code harder to read)
102
.1234
103
 
40535 maechler 104
sqrt(1i) # remember elementary math?
41508 ripley 105
utils::str(0xA0)
40535 maechler 106
identical(1L, as.integer(1))
107
 
108
## You can combine the "0x" prefix with the "L" suffix :
51414 ripley 109
identical(0xFL, as.integer(15))
40535 maechler 110
}
34712 ripley 111
\keyword{documentation}