0

For some reason my function "isint" will not work for some fractions. Here is the code:

isint<-function(x){if(x!=round(x)){return(0)}else{return(1)}}

isint(1.05/1.05) gives me 1 however isint(1040.55/1.05) gives me 0.

Why is this the case?

Eddy
  • 1

2 Answers2

1

Check this.

options(digits = 20)
1040.55/1.05
[1] 990.99999999999989

Since 991!=990.99999999999989 you get 0

Community
  • 1
  • 1
Erdem Akkas
  • 2,062
  • 10
  • 15
0

This is just because arithmetic base-2 is not exact base-10. A better test would be to use !all.equal(x,round(x)), which allows for small discrepancies. For example, compare the following...

> all.equal(1040.55/1.05,991)
[1] TRUE
> identical(1040.55/1.05,991)
[1] FALSE

So, try isint<-function(x){if(!all.equal(x,round(x))){return(0)}else{return(1)}}

Andrew Gustar
  • 17,295
  • 1
  • 22
  • 32