R division result is not precise - r

> 52631579 - 52631559
[1] 20
> 20 / 52631559
[1] 3.800001e-07
> 52631579 / 52631559
[1] 1
> class(52631579 - 52631559)
[1] "numeric"
I would like the division to result in the precise float.

you can use sprintf function to format output
sprintf("%.50f", 52631579 / 52631559)
# [1] "1.00000038000014401795567664521513506770133972167969"

Related

R calculates wrong?

How can that be?
> mode(daten[1,16])
[1] "numeric"
> mode(weku)
[1] "numeric"
>
> weku
[1] 10.47855
> daten[1,16]
[1] 814995955
> daten[1,16]/weku
[1] 77777557
>
> 814995955/10.47855
[1] 77777551
>
I don't understand this. How can I get the correct calculation?
daten[1,16]/weku is correct.
R does not display all of the decimal values it stores internally. What is printed on the console is controlled by options("digits").
For example, compare print(pi), print(pi, digits=10), and print(pi, digits=22).

If minutes are doubles, why is the minimum of minutes 0?

I want to take the minimum of minute values.
> typeof(minutes(7))
[1] "double"
> min(c(minutes(7),minutes(8)))
[1] 0
> seconds(min(as.numeric(c(minutes(7),minutes(8)))))
[1] "420S"
Why is that?
After all it works with regular doubles:
> typeof(c(7.0,8.0))
[1] "double"
> min(c(7.0,8.0))
[1] 7

r min function does not return the correct number of decimals

In R I am using the min function on a vector of numeric values, like this vector:
v <- c(16.22900, 16.28857, 16.47363, 16.47412, 16.00000, 16.49463, 16.27246, 16.0366, 16.49609)
However when I apply the min function, I get this return value
min(v)
[1] 16
instead I would like this result:
[1] 16.00000
I checked the class of the vector but all seems ok
class(v)
[1] "numeric"
Where is the problem?
You are caught between internal representation of a value and how it is displayed.
R> v <- c(16.22900, 16.28857, 16.47363, 16.47412, 16.00000,
+ 16.49463, 16.27246, 16.0366, 16.49609)
R> min(v)
[1] 16
R> sprintf("%10.8f", min(v))
[1] "16.00000000"
R> identical(min(v), 16.0000000000000000000)
[1] TRUE
R>

Two (supposedly) identical date objects in R are not equal?

I have a simple question. I have two Date objects in R that are supposed to be identical (they have the same value and class), but R is saying they are not equal. I am running on linux though I get the same result on a windows machine. Why is this happening?
code:
start=as.Date("2014-12-31")
finish=as.Date("2014-11-28")
dates = seq(start,finish,length=6)
christmasEve = as.Date("2014-12-24")
print(dates[2])
print(christmasEve)
print(class(dates[2]))
print(class(christmasEve))
(christmasEve==dates[2])
output:
[1] "2014-12-24"
[1] "2014-12-24"
[1] "Date"
[1] "Date"
[1] FALSE
Any help would be greatly appreciated!
-Paul
The problem is that you are dividing a number of days that is not a multiple of six by six. Check out:
as.numeric(dates)
# [1] 16435.0 16428.4 16421.8 16415.2 16408.6 16402.0
start - finish
# Time difference of 33 days
Since you are creating the dates as a sequence the dates are not exact round numbers.
> as.numeric(dates)
[1] 16435.0 16428.4 16421.8 16415.2 16408.6 16402.0
> as.numeric(christmasEve)
[1] 16428
> as.character(christmasEve) == as.character(dates[2])
[1] TRUE
It is not possible to test your code as there is no sampleRate. I assumed that sampleRate is 6. You could compare your dates with the code below:
all(as.character(christmasEve) == as.character(dates[2]))
The whole things should work like that
> sampleRate <- 6
>
> start=as.Date("2014-12-31")
> finish=as.Date("2014-11-28")
> dates = seq(start,finish,length=sampleRate)
> christmasEve = as.Date("2014-12-24")
> print(dates[2])
[1] "2014-12-24"
> print(christmasEve)
[1] "2014-12-24"
> print(class(dates[2]))
[1] "Date"
> print(class(christmasEve))
[1] "Date"
> (christmasEve==dates[2])
[1] FALSE
>
> all(christmasEve == dates[2])
[1] FALSE
> all(as.character(christmasEve) == as.character(dates[2])
+ )
[1] TRUE

why as.numeric function in R doesn't work properly?

I have these two characters and the "as.numeric" function doesn't work same for them. Can anyone help me why this is happening?
options(digits=22)
a="27"
as.numeric(a)
[1] 27.00000000000000000000
a="193381411288395777"
as.numeric(a)
[1] 193381411288395776.0000
It can be seen that in the second case the last digit is not "7" and it is "6". Basically the "as.numeric" function decreases 1 unit from the number in the second case.
Any help is appreciated.
You need to learn about the limits of representation of exact numbers. R can tell you what it has:
R> .Machine
$double.eps
[1] 2.22045e-16
$double.neg.eps
[1] 1.11022e-16
$double.xmin
[1] 2.22507e-308
$double.xmax
[1] 1.79769e+308
$double.base
[1] 2
$double.digits
[1] 53
$double.rounding
[1] 5
$double.guard
[1] 0
$double.ulp.digits
[1] -52
$double.neg.ulp.digits
[1] -53
$double.exponent
[1] 11
$double.min.exp
[1] -1022
$double.max.exp
[1] 1024
$integer.max
[1] 2147483647
$sizeof.long
[1] 8
$sizeof.longlong
[1] 8
$sizeof.longdouble
[1] 16
$sizeof.pointer
[1] 8
R>
Use the int64 package:
library(int64)
> as.int64("193381411288395777")
[1] 193381411288395777

Resources