Search code examples
chapel

Bounds of 'low..high' must be integers of compatible types


I have just started to learn chapel and was trying a simple Sieve of Eratosthenes algorithm and was stuck at a point where I am not able to pass an array into a procedure.

Below is the implementation

config const n:int = 5;
var arrOfErtosthenes : [1..n] bool;

arrOfErtosthenes = true;
proc sieveOfEratothenes(array,n){
    for i in 2..sqrt(n){
        if(array[i] == true){
            for j in i*i..n{
                array[j] = false;
            }
        }
    }
}

sieveOfEratothenes(arrOfErtosthenes,n);
for i in arrOfErtosthenes.domain do{
    if(arrOfErtosthenes[i] == true) then write(i," , ");
}
writeln();

Before this I tried to get var n from user but in both the cases got the following error

$ chpl /e/Programming/Chapel/simpleExcercises.chpl
/e/Programming/Chapel/simpleExcercises.chpl:9: In function 'sieveOfEratothenes':
/e/Programming/Chapel/simpleExcercises.chpl:10: error: Bounds of 'low..high' must be integers of compatible types.
  /e/Programming/Chapel/simpleExcercises.chpl:19: called as sieveOfEratothenes(array: [domain(1,int(64),false)] bool, n: int(64))
note: generic instantiations are underlined in the above callstack

Solution

  • The source of error is in this line:

        for i in 2..sqrt(n) {
    

    The range 2..sqrt(n) consists of two different types, int and real. To address the issue, you can cast sqrt(n) to an int like so:

        for i in 2..(sqrt(n):int) {