Floating point operations and numerical data types

From the standpoint of mathematical operations, we usually think of numbers as just that. However, a computer takes a more broken down approach to numbers. Most of the time this doesn't matter, but when we have to deal with large datasets and are concerned about either memory or speed, it can make a big difference. R essentially has two numeric data types; integer and double precision (also called numeric). The integer data type handles exact values denoted as integers. As per the IEEE floating point standard, the double precision type handles values as rounded decimals. R has no single data type as is used in languages like C.

Here, we will create two vectors of integers, x and y, and divide one by the other. Mathematically (but not computationally), we get whole number results. Let's look at the following code:

> x <- as.integer(seq(1, 10, by = 1))
> y <- as.integer(seq(2, 20, by = 2))
> x
 [1]  1  2  3  4  5  6  7  8  9 10
> y
 [1]  2  4  6  8 10 12 14 16 18 20
> y/x
 [1] 2 2 2 2 2 2 2 2 2 2

The y/x vector has 10 members that look like integers (and all single digit integers). It should supposedly be the same size as x and y, but when we look at the size, it isn't. It's almost twice the size in memory. Let's have a look at the following code:

> object.size(x)
88 bytes
> object.size(y)
88 bytes
> object.size(y/x)
168 bytes

The reason is because the division operator has coerced the results of y/x to be a double precision data type, as we can see in the following code:

> typeof(x)
[1] "integer"
> typeof(y)
[1] "integer"
> typeof(y/x)
[1] "double"
>

Keeping values that are integers in integer form in memory can save a substantial amount of memory. As shown in the earlier code, the as.integer command is a generic command that can accomplish this.

Note

The command for creating a sequence, seq, by default returns the type integer. So the as.integer command was not strictly necessary in the previous code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.179.100