python division: traditional division, true division, floor division

'''
 x/y traditional division & true division
 In version 2.6, the decimal will be omitted for integers, and the fractional part will be preserved for floating point numbers. In
 version 3.0, it will become true division regardless of any type
 . To implement true division, you need to import from __future__ import division
 x//y Floor division & truncation
division . Regardless of the type of the operation object, the fractional part of the result is always omitted, leaving the smallest divisible integer part. The result type depends on Type of operand
 '''
 print ( 10 / 4 )    #output 2.5
 print ( 10 // 4 )    #output 2
 print ( 10 / 4.0 )    #output 2.5
 print ( 10 // 4.0 )    #output 2.0
 import math
 print (math .floor(

2.5 ))    #output 2
 print (math.floor(- 2.5 ))    #output -3    
 print (math.trunc( 2.5 ))    #output 2 #Direct truncation without judging the sign
 print (math.trunc(- 2.5 ))    #output -2 #Direct truncation without judging the sign

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326001701&siteId=291194637