Python 3 integer division. How to make math operators consistant with C

前端 未结 5 1321
野趣味
野趣味 2020-12-06 09:54

I need to port quite a few formulas from C to Python and vice versa. What is the best way to make sure that nothing breaks in the process?

I hope my question doesn\'

5条回答
  •  Happy的楠姐
    2020-12-06 10:13

    In C:

    -11/2 = -5
    

    In Python:

    -11/2 = -5.5
    

    and also in python:

    -11//2 = -6
    

    To achieve C-like behaviour, write int(-11/2) in python, this will evaluate to -5.

提交回复
热议问题