Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between double- precision data type and numeric data type

Tags:

r

precision

What is the significant difference between double-precision data type and numeric data type in R programming?

like image 781
Devyani Balyan Avatar asked May 09 '18 13:05

Devyani Balyan


People also ask

What is the difference between double and numeric?

double is the name of the type. numeric is the name of the mode and also of the implicit class. As an S4 formal class, use "numeric" .

Is DOUBLE PRECISION a numeric data type?

The data types real and double precision are inexact, variable-precision numeric types.

What is the difference between numeric and decimal data type?

There is a small difference between NUMERIC(p,s) and DECIMAL(p,s) SQL numeric data type. NUMERIC determines the exact precision and scale. DECIMAL specifies only the exact scale; the precision is equal or greater than what is specified by the coder.

What is a numeric data type?

Numeric data types are in two categories: exact and approximate. • Exact types include integer and decimal data types. • Approximate types include floating point data types.


2 Answers

From stat.ethz.ch:

It is a historical anomaly that R has two names for its floating-point vectors, double and numeric (and formerly had real). double is the name of the type. numeric is the name of the mode and also of the implicit class. As an S4 formal class, use "numeric". The potential confusion is that R has used mode "numeric" to mean ‘double or integer’

We can think of doubles as belonging to numeric. To see this:

 > is.double(1)
 [1] TRUE
 > is.numeric(1)
 [1] TRUE

R normally stores numbers as doubles. Using "numeric()" is the same as "double()." You can also store a number as a single or an integer. Both will be numeric. You may choose to force a number be stored as an integer for performance reasons, but unless you are building a package the trade offs might not be worth your time.

I suggest reading Gillespie's Overview for more info on type and performance.

like image 55
Peter_Evan Avatar answered Sep 30 '22 14:09

Peter_Evan


@tk3: numeric is NOT always identical to double (and real).

> x <- 10
> storage.mode(x)
[1] "double"
> is.numeric(x)
[1] TRUE
> x <- as.integer(x)
> storage.mode(x)
[1] "integer"
> is.numeric(x)
[1] TRUE
like image 39
user449361 Avatar answered Sep 30 '22 15:09

user449361