[Python] Mod operator in Free Pascal gives a different result than expected

Discussão em 'Python' iniciado por Stack, Janeiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    The mod operator in Free Pascal does not produce the results I would expect.

    This can be demonstrated by the program below whose output does not agree with the result of the same calculation in Python (or Google).

    program test(output);

    var
    a, b, c: longint;

    begin
    a := -1282397916;
    b := 2147483647;
    c := a mod b;
    writeln (c:16);
    end.


    -1282397916


    Compare this to the output of the Python script below which gives the result I expected.

    #!/usr/bin/python

    a = -1282397916
    b = 2147483647
    c = a % b
    print (c)


    865085731


    This is the same as the result as that obtained by pasting the following text into Google (and when using the mod operator in VAX‑Pascal).

    (-1282397916 % 2147483647)


    The question is: Why does Free Pascal behave differently? And how do I get the same result as that obtained when using the mod operator in Python?

    Continue reading...

Compartilhe esta Página