#thread 'main' panicked at 'attempt to multiply with overflow'?

17 messages · Page 1 of 1 (latest)

unreal vapor
#

I'm getting the error "thread 'main' panicked at 'attempt to multiply with overflow', src\main.rs:18:5" and I am not quire sure where the error is or what is actually going wrong?

The code that seems to be the problem is:

pub fn xy_idx(x: i32, y: i32) -> usize {
    (y as usize * GRIDHEIGHT) + x as usize
}

[...]

fn update_pixel_position(mut pixels: Query<(&mut Position, &mut Transform)>, mut map: ResMut<Map>){
    for (position, mut transform) in &mut pixels {
        transform.translation.x = position.x;
        transform.translation.y = position.y;
        
        map.grid[xy_idx(position.x as i32, position.y as i32)] = 1;

    }
}

[...]

fn update_pixel_physics(mut pixels: Query<(&mut Physics, &mut Position,)>, map: Res<Map>){
    for (physics, mut position) in &mut pixels{
        if (physics.p_type == PhysicsType::Fall) | (physics.p_type == PhysicsType::Fill) { //Check Down and Down Diags for free space
            if map.grid[xy_idx(position.x as i32, (position.y - 1.0) as i32)] != 1{
                position.y -= 1.0;
            }
        }
    }
}

Calling xy_idx seems to be the problem from what I can gather.

spiral junco
#

You are given the exact location at which error occurred in your error message. Can you please specify which line is line 18, character 5?

unreal vapor
#

Clearly not woken up today since I didn't realise that :D

    (y as usize * GRIDHEIGHT) + x as usize
sand basin
#

your position.y is probably too big ( y * GRIDHEIGHT > 2_147_483_647)

#

can you tell us what are the values of position.y and the GRIDHEIGHT are?

unreal vapor
#

GRIDHEIGHT would be 720 and position.y should be 0.

sand basin
#

i don't think position.y is simply 0. how are you manipulating the positions?

#

are you assigning anything to position.y?

unreal vapor
#

Position is a component that is assigned to an entity, starting at (0,0). Right now update_pixel_physics is the only thing effecting position.y

sand basin
#

oh yeah i now understand.

// imagine this is your y position
let mut y = 0i32;

// now in the code, we subtract one
y -= 1;

// then we cast the value to usize
let y2 = y as usize;

// let's debug y2...
dbg!(y2);

// the output is...
   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 1.93s
     Running `target/debug/playground`
[src/main.rs:12] y2 = 18446744073709551615
unreal vapor
#

AH

sand basin
#

the problem is that casting from i32 to usize is actually surprising. you should explicit handle your negative values correctly

unreal vapor
#

usize isn't signed.

#

I was wondering right now if that was the issue

sand basin
#
pub fn xy_idx(x: i32, y: i32) -> usize {
    // The problematic part
    // |
    // vvvvvvv
    (y as usize * GRIDHEIGHT) + x as usize
    // casting i32 to usize leads to surprising results,
    // since negative values wraps around when
    // converting to unsigned integers 
}
unreal vapor
#

🤦‍♀️

#

I get it now no worries