-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
0 converted to small double/float when used in a power function #775
Conversation
Hi @sstein93 You are right. This is about avoiding You can turn this off by |
I have thought about refactoring this code for a while. In theory, different models should be generated based on if you want this protection or not (since it adds a bit of overhead). Then the powers and the like could probably be only fixed if there is an issue (though that would take a bit more overhead...) |
Thank you @mattfidler! |
@mattfidler I just tried the example above and adding Do you know if this is due to a specific version of an R or C dependency? Thanks,
Here is my sessionInfo in case that is helpful:
|
This is likely based on the version of |
Hi @sstein93, The pull request this is being changed into probably behaves a bit library(rxode2)
#> rxode2 2.1.3.9000 using 8 threads (see ?getRxThreads)
#> no cache: create with `rxCreateCache()`
test_rxode_0 <- rxode2({
fn(0) <- 0
is_0_0 <- 0
check_0_exp_1.2 <- 0^1.2
check_near0_exp_1.2 <- (1.490116e-08)^1.2
check_0_exp_0.67 <- 0^0.67
check_near0_exp_0.67 <- (1.490116e-08)^0.67
check_0_exp_1 <- 0^1
check_near0_exp_near0 <- (1.490116e-08)^(1.490116e-08)
check_near0_exp_0 <- (1.490116e-08)^0
check_0_exp_0 <- 0^0
check_0_exp_near0 <- 0^(1.490116e-08)
check_0_div_0 <- 0/0
check_0_plus_0 <- 0 + 0
d/dt(fn) <- 1
})
test_events <- et(seq(0,10, by = 1))
test_sim <- rxSolve(test_rxode_0, events = test_events, returnType="data.frame",
safeZero=TRUE, safePow=TRUE, safeLog=TRUE, useStdPow=FALSE)
print(test_sim)
#> time check_0_exp_1.2 check_near0_exp_1.2 check_0_exp_0.67
#> 1 0 0 4.053817e-10 0
#> 2 1 0 4.053817e-10 0
#> 3 2 0 4.053817e-10 0
#> 4 3 0 4.053817e-10 0
#> 5 4 0 4.053817e-10 0
#> 6 5 0 4.053817e-10 0
#> 7 6 0 4.053817e-10 0
#> 8 7 0 4.053817e-10 0
#> 9 8 0 4.053817e-10 0
#> 10 9 0 4.053817e-10 0
#> 11 10 0 4.053817e-10 0
#> check_near0_exp_0.67 check_0_exp_1 check_near0_exp_near0 check_near0_exp_0
#> 1 5.702397e-06 0 0.9999997 1
#> 2 5.702397e-06 0 0.9999997 1
#> 3 5.702397e-06 0 0.9999997 1
#> 4 5.702397e-06 0 0.9999997 1
#> 5 5.702397e-06 0 0.9999997 1
#> 6 5.702397e-06 0 0.9999997 1
#> 7 5.702397e-06 0 0.9999997 1
#> 8 5.702397e-06 0 0.9999997 1
#> 9 5.702397e-06 0 0.9999997 1
#> 10 5.702397e-06 0 0.9999997 1
#> 11 5.702397e-06 0 0.9999997 1
#> check_0_exp_0 check_0_exp_near0 check_0_div_0 check_0_plus_0 fn
#> 1 1 0 0 0 0
#> 2 1 0 0 0 1
#> 3 1 0 0 0 2
#> 4 1 0 0 0 3
#> 5 1 0 0 0 4
#> 6 1 0 0 0 5
#> 7 1 0 0 0 6
#> 8 1 0 0 0 7
#> 9 1 0 0 0 8
#> 10 1 0 0 0 9
#> 11 1 0 0 0 10
test_sim <- rxSolve(test_rxode_0, events = test_events, returnType="data.frame",
safeZero=FALSE, safePow=FALSE, safeLog=FALSE, useStdPow=TRUE)
# This tests some of the differences between the solves:
m <- rxode2({
nan0 = 0/0
nanLog = log(-1)
nanPow = 0^-1
rPow = 0^-1.5
})
et <- et(1)
# No safe zero in place, use C's pow() function
rxSolve(m, et, safeZero=FALSE, safePow=FALSE, safeLog=FALSE, useStdPow = TRUE, returnType="data.frame")
#> time nan0 nanLog nanPow rPow
#> 1 1 NaN NaN Inf Inf
# No safe zero in place, use R's pow() function
rxSolve(m, et, safeZero=FALSE, safePow=FALSE, safeLog=FALSE, useStdPow = FALSE, returnType="data.frame")
#> time nan0 nanLog nanPow rPow
#> 1 1 NaN NaN Inf Inf
# Safe zero for everything
rxSolve(m, et, safeZero=TRUE, safePow=TRUE, safeLog=TRUE, useStdPow = FALSE, returnType="data.frame")
#> time nan0 nanLog nanPow rPow
#> 1 1 0 -36.04365 4.5036e+15 3.022315e+23 Created on 2024-08-28 with reprex v2.1.1 The rules are updated to:
If this successfully passes the checks, I will merge into the main I also plan to release at the end of September 2024 due to a CRAN |
Thanks for reporting @sstein93 |
Thanks @mattfidler !! |
No problem. |
Hello,
I found that 0 is converted to a small double (1.490116e-08) when used in the base of a power function inside an rxode2 model. This has caused large effects in my model. Particularly, when the amount of drug is 0 in the central compartment, I still see the "effect" compartment change, even though the derivative should be exactly 0 when the central compartment concentration is exactly 0. However, when I run the model with no drug added (i.e. central compartment is always exactly 0) the drug effect compartment changes 10-15% over the simulated 30 hours.
Here is a reproducible example where I've set some variables inside the rxode2 model definition. All of the variables that are 0^x should be 0 in the model results, however you can see in the output below that they are not.
Thanks!
Shayna