jeudi 22 octobre 2015

Approximate Equals in R

I have to perform an equation on variable i until it 'approximately equals' 100, but I can't seem to get it to work.

By approximately equals, I mean a tolerance of +/-2 on 100.

I have a while(TRUE) loop with a couple of if statements, but I can't seem to get the if's to work correctly.

Here's what I have at the moment:

PercentageIncrement <- function(RainPerc,Duration) {

 i = RainfallDepthChange(Duration)

 print(paste0("Output: ", i))

 while(TRUE) {

   i = i+(RainPerc*2)

   if (i > 98) { 

     if (i < 102) { 

       break()

     }

   }

   print(paste0("Output: ", i))

 } 

}

I've uploaded the whole function to give some context. At the moment, if I run this function with the following inputs:

PercentageIncrement(14.29,7) 

I get the following output:

[1] "Output: 14.2857142857143"
[1] "Output: 42.8657142857143"
[1] "Output: 71.4457142857143"

This is exactly correct, except for the fact that it is missing a fourth output. In this instance with these inputs, the output should be 100 exactly, but other inputs will vary between 98 and 102.

How might I go about fixing this?

Thanks

Aucun commentaire:

Enregistrer un commentaire