0

I am running the code below. The function Ax returtns the value 1. But the ifelse statement does not recognize that Ax==1.

I've tried making the output of the function double-precision and all numeric values double. This hasn't worked.

I was hoping:
1) to get help troubleshooting this specific case
2) to get tips on how to avoid this problem in the future

Axfun<-function(beta,gamma,a,b,g,H0stud,Wh,Wi){ 
  ((b*beta + g*gamma)*(1 + 2*g*gamma)*(1 + gamma + b*((-beta)*(1 + gamma) + a*(-1 + beta)*(1 + g*gamma)))*H0stud*Wi)/
    ((-1 + b*beta)*(1 + gamma)*((-g)*gamma*(1 + 2*g*gamma)*Wi + b*beta*((-1 + g*gamma*(-2 + H0stud) + H0stud)*Wh - (1 + g*gamma)*H0stud*Wi)))
}

Ax<-Axfun(2^1,
          2^0,
          2^0,
          2^-3,
          2^-1,
          1,
          1,
          0.4)

ifelse(Ax>=1, 0, Ax)
user1375871
  • 1,199
  • 1
  • 12
  • 23
  • 3
    Check out `?all.equal`, e.g. `all.equal(Ax, 1)`, which allows differences within `tolerance`. – jbaums Jun 15 '15 at 01:32
  • 1
    And see also these SO posts: [1](http://stackoverflow.com/q/9508518/489704), [2](http://stackoverflow.com/q/2769510/489704), [3](http://stackoverflow.com/q/18794129/489704). – jbaums Jun 15 '15 at 01:38
  • Also Circle 1 of [The R Inferno](http://www.burns-stat.com/pages/Tutor/R_inferno.pdf) – Molx Jun 15 '15 at 01:52

1 Answers1

5

There is nothing wrong with the >=, your problem is that 1 is not really one. Try this

Ax >= 1
[1] FALSE
Ax == 1
[1] FALSE

and

format(Ax, digits = 20)
[1] "0.99999999999999977796"

Edit: A possible Solution

As solutions to your problems you can return the final result of your function to an object and then use it as the ceiling function.

If you have to work with such results maybe a read of the Circle 1 of the R Inferno is a good thing to do. You can find it here.

SabDeM
  • 7,050
  • 2
  • 25
  • 38