Here is a question for abnormal output of my taichi sentence.

Dear experts:
Recently I am designing a taichi function to calculate a variable named “step_l”, The code as followed part show.

@ti.func
def steplength(maxsigma, thisMaxStep, beam_pos, BeamDir):
    step_x = 0.0
    step_y = 0.0
    step2bound = 0.0
    cb = 0
    sl = 0.0
    xj = ti.ceil(beam_pos[1] / 0.1)
    yj = ti.ceil(beam_pos[2] / 0.1)
    step_rs = -ti.log(ti.random()) / maxsigma
    sl = min(step_rs, thisMaxStep)

    if BeamDir[1] < 1e-6 and -BeamDir[1] > -1e-6:
        step_x = 10000.0
    elif BeamDir[1] > 0:
        step_x = (xj * 0.1 - beam_pos[1]) / BeamDir[1]
    else:
        step_x = (ti.floor(beam_pos[1] / 0.1) * 0.1 - beam_pos[1]) / BeamDir[1]

    if BeamDir[2] < 1e-6 and -BeamDir[2] > -1e-6:
        step_y = 10000.0
    elif BeamDir[2] > 0:
        step_y = (yj * 0.1 - beam_pos[2]) / BeamDir[2]
    else:
        step_y = (ti.floor(beam_pos[2] / 0.1) * 0.1 - beam_pos[2]) / BeamDir[2]

    if step_x < step_y:
        step2bound = step_x
        cb = 2
    else:
        step2bound = (yj * 0.1 - beam_pos[2]) / BeamDir[2]
        cb = 3

    if sl < step2bound:
        cb = 0
    else:
        sl = step2bound
    return sl, cb
The sl(steplength variable will always be -0.0000001 after certain loops), So I decided to print its function information using appending code.
                print(maxsig,thisMaxStep,BeamDir,BeamPos)
                step_length, cb = steplength(maxsig, thisMaxStep, BeamPos, BeamDir)
                print(step_length)

The error part of print information is.
2.223392 0.200000 [-0.011177, -0.025937, 0.999601] [-0.039374, 25.117306, 13.100001]
0.038319
2.209526 0.200000 [-0.005739, -0.028285, 0.999583] [-0.039802, 25.116312, 13.138305]
0.061721
2.204434 0.200000 [-0.012702, -0.030314, 0.999460] [-0.040156, 25.114567, 13.200001]
-0.000001
2.204295 0.200000 [-0.012704, -0.030301, 0.999460] [-0.040156, 25.114567, 13.200001]
-0.000001
We can find that the sl number turns to be an abnormal value (-0.000001) what it should not be. Would u mind help me fix this error? Thanks a lot for your kind help.

Hi @photon , welcome to Taichi community.
Would you mind providing an executable repro? It is not easy to figure out the numerical error without repro code.