Skip to content

INT::div_euclid and INT::rem_euclid return incorrect values for negative numerators #87970

@toothbrush7777777

Description

@toothbrush7777777

The results of SIGNED_INT::div_euclid and SIGNED_INT::rem_euclid are always incorrect when a % b != 0 && a.is_negative() (where a is the dividend and b is the divisor).

I tried something similar to this code:

fn main() {
    assert_eq!(div(-32768, -32767), (1, 1));
}
fn div(num: i16, div: i16) -> (i16, i16) {
    (num.div_euclid(div), num.rem_euclid(div))
}

I expected to see this happen:
The assert should not fire.

Instead, this happened:
The assert fired. The return value is (2, 32766) instead of (1, 1).

More general test case on playground

Meta

This bug exists in the latest stable, beta and nightly.
The bug appears to have been present since 1.38, when div_euclid and rem_euclid where introduced.
The profile (release or debug) makes no difference.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions