Integer division. Specifically in to find interval for ODEs



Dea all,

a simple question regarding division of interger that I use via the function written by S. Weber (see: Forced ODEs, a start for a case study? ).

The warning message returned is:

Warning: integer division implicitly rounds to integer. Found int division: (left_ind + right_ind) / 2

Is there another/new way to do this now?
As written in the function: is there a controlled way without being yelled at with a warning?"

The code is:

int find_interval_elem(real x, vector sorted, int start_ind) {
  int res;
  int N;
  int max_iter;
  real left;
  real right;
  int left_ind;
  int right_ind;
  int iter;

  N = num_elements(sorted);

  if(N == 0) return(0);

  left_ind  = start_ind;
  right_ind = N;

  max_iter = 100 * N;
  left  = sorted[left_ind ] - x;
  right = sorted[right_ind] - x;

  if(0 <= left)  return(left_ind-1);
  if(0 == right) return(N-1);
  if(0 >  right) return(N);

  iter = 1;
  while((right_ind - left_ind) > 1  && iter != max_iter) {
    int mid_ind;
    real mid;
    // is there a controlled way without being yelled at with a warning?
    mid_ind = (left_ind + right_ind) / 2;
    mid = sorted[mid_ind] - x;
    if (mid == 0) return(mid_ind-1);
    if (left  * mid < 0) { right = mid; right_ind = mid_ind; }
    if (right * mid < 0) { left  = mid; left_ind  = mid_ind; }
    iter = iter + 1;
  if(iter == max_iter)
    print("Maximum number of iterations reached.");


I never found a way to silence away that warning. There are good reasons to have such a warning in Stan, but for this application its a bit annoying, I agree.


I would like to release an R package in a near future, so “warning messages” are not always welcome. I’ll see.
By the way, thank you for all your work with ODEs !


Agreed. We want to support a way of turning them off in the future, either on a case by case basis, or wholesale. It’s not there yet. We haven’t even been able to turn off all the Stan output.

Some of the compiler warning messages from C++ are much more challenging because they didn’t originate with us and fixing them is a mess process-wise.