Skip to content

Commit 2345489

Browse files
committed
Merge branch 'master' into fix/issue-975-inits
2 parents 6019efe + 356fa04 commit 2345489

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: cmdstanr
22
Title: R Interface to 'CmdStan'
3-
Version: 0.8.1
3+
Version: 0.8.1.9000
44
Date: 2024-06-06
55
Authors@R:
66
c(person(given = "Jonah", family = "Gabry", role = "aut",

R/data.R

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,18 @@ process_data <- function(data, model_variables = NULL) {
186186
# generating a decimal point in write_stan_json
187187
if (data_variables[[var_name]]$type == "int"
188188
&& !is.integer(data[[var_name]])) {
189-
mode(data[[var_name]]) <- "integer"
189+
if (!isTRUE(all(is_wholenumber(data[[var_name]])))) {
190+
# Don't warn for NULL/NA, as different warnings are used for those
191+
if (!isTRUE(any(is.na(data[[var_name]])))) {
192+
warning("A non-integer value was supplied for '", var_name, "'!",
193+
" It will be truncated to an integer.", call. = FALSE)
194+
}
195+
mode(data[[var_name]]) <- "integer"
196+
} else {
197+
# Round before setting mode to integer to avoid floating point errors
198+
data[[var_name]] <- round(data[[var_name]])
199+
mode(data[[var_name]]) <- "integer"
200+
}
190201
}
191202
}
192203
}

R/utils.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ matching_variables <- function(variable_filters, variables) {
3636
)
3737
}
3838

39+
is_wholenumber <- function(x, tol = sqrt(.Machine$double.eps)) {
40+
abs(x - round(x)) < tol
41+
}
42+
3943
# checks for OS and hardware ----------------------------------------------
4044

4145
os_is_windows <- function() {

tests/testthat/test-data.R

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,50 @@ test_that("process_data() corrrectly casts integers and floating point numbers",
355355
fixed = TRUE
356356
)
357357
})
358+
359+
test_that("process_data warns on int coercion", {
360+
stan_file <- write_stan_file("
361+
data {
362+
int a;
363+
real b;
364+
}
365+
")
366+
mod <- cmdstan_model(stan_file, compile = FALSE)
367+
expect_warning(
368+
process_data(list(a = 1.1, b = 2.1), model_variables = mod$variables()),
369+
"A non-integer value was supplied for 'a'! It will be truncated to an integer."
370+
)
371+
372+
stan_file <- write_stan_file("
373+
data {
374+
array[3] int a;
375+
}
376+
")
377+
mod <- cmdstan_model(stan_file, compile = FALSE)
378+
expect_warning(
379+
process_data(list(a = c(1, 2.1, 3)), model_variables = mod$variables()),
380+
"A non-integer value was supplied for 'a'! It will be truncated to an integer."
381+
)
382+
383+
expect_no_warning(
384+
process_data(list(a = c(1, 2, 3)), model_variables = mod$variables())
385+
)
386+
})
387+
388+
test_that("Floating-point differences do not cause truncation towards 0", {
389+
stan_file <- write_stan_file("
390+
data {
391+
int a;
392+
real b;
393+
}
394+
")
395+
mod <- cmdstan_model(stan_file, compile = FALSE)
396+
a <- 10*(3-2.7)
397+
expect_false(is.integer(a))
398+
test_file <- process_data(list(a = a, b = 2.0), model_variables = mod$variables())
399+
expect_match(
400+
" \"a\": 3,",
401+
readLines(test_file)[2],
402+
fixed = TRUE
403+
)
404+
})

0 commit comments

Comments
 (0)