taichi: If condition must be int32 error

To reproduce:

import taichi as ti
ti.init(default_ip=ti.i64)

@ti.kernel
def test():
    for i in range(100):
        if i % 2 == 0:
            print(i)

test()

The above code reports error

taichi.lang.exception.TaichiTypeError: 
`if` conditions must be of type int32; found i64. Consider using `if x != 0` instead of `if x` for float values.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (6 by maintainers)

Most upvoted comments

After I applied this modification,I can use four int64 to simulate float256 and draw this in 10^-55 precision 😃 Re:-0.6717536270481832370389902348813046305959745988723619134343491834092885898911496280 Im:0.4609205425545165168733304746281937721035176497122051662619296867035239619125442493 zoom:2.28E54 frame_00372 Thanks again for your efficient bug fix.

Maybe one or two days later, I will release my code.It is just an experimental program, but the efficiency is satisfactory to me.

I also tried this:

import taichi as ti
ti.init(default_ip=ti.i64)


@ti.kernel
def test():
    a = 20
    while ti.cast(a > 0, ti.i32):
        a -= 1


test()

still output: while conditions must be of type int32; found i64. Consider using while x != 0 instead of while x for float values.

and thank you for your quick reply.

import taichi as ti

ti.init(default_ip=ti.i64)


@ti.kernel
def test():
    a = 20
    while a > 0:
        a -= 1


test()

output:while conditions must be of type int32; found i64. Consider using while x != 0 instead of while x for float values.

it will still happened in while loop

Currently we use i32 to represent boolean and it is self-contained. The bug in this issue is due to wrong implementation, and can be easily fixed by #5487. i1 is only a nice-to-have for now, and we may plan it in some future version like v1.3 or so.