Created
April 30, 2016 19:20
-
-
Save coleoguy/a50dba496fc55440cb4945fe8e25d703 to your computer and use it in GitHub Desktop.
stochastic rounding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
StochRound <- function(x){ | |
## extract the decimal portion | |
q <- abs(x - trunc(x)) | |
## draw a value 0 or 1 with probability | |
## based on how close we already are | |
adj <- sample(0:1, size = 1, prob = c(1 - q, q)) | |
## make it negative if x is | |
if(x < 0) adj <- adj * -1 | |
## return our new value | |
trunc(x) + adj | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment