python语言print(3)出来为啥2.9999_Python:int(3.0)= 2

Observe the following python program

def goo(y,x):

y = float(y)

x = float(x)

yup = (y - x - 1) / x

yup = str(yup)

yup = yup.split(".")

decimal = "." + yup[1]

decimal = float(decimal)

output = decimal / (1/x)

print output

print int(output)

goo(9,5)

When the program executes "print output" the answer is 3.0

When the program executes "print int(output)" the answer is 2

"print int(output)" should execute to deliver 3, not 2

Why is this happening?

解决方案

The default for the print statement is to round the value to the nearest. int() truncates.

>>> print 2.99999999999999

3.0

>>> print int(2.99999999999999)

2

If you want to see a more exact representation, use repr.

>>> print repr(2.9999999999999)

2.9999999999999

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
class SpiralIterator: def init(self, source, x=810, y=500, length=None): self.source = source self.row = np.shape(self.source)[0] #第一个元素是行数 self.col = np.shape(self.source)[1] #第二个元素是列数 if length: self.length = min(length, np.size(self.source)) else: self.length = np.size(self.source) if x: self.x = x else: self.x = self.row // 2 if y: self.y = y else: self.y = self.col // 2 self.i = self.x self.j = self.y self.iteSize = 0 geo_transform = dsm_data.GetGeoTransform() self.x_origin = geo_transform[0] self.y_origin = geo_transform[3] self.pixel_width = geo_transform[1] self.pixel_height = geo_transform def hasNext(self): return self.iteSize < self.length # 不能取更多值了 def get(self): if self.hasNext(): # 还能再取一个值 # 先记录当前坐标的值 —— 准备返回 i = self.i j = self.j val = self.source[i][j] # 计算下一个值的坐标 relI = self.i - self.x # 相对坐标 relJ = self.j - self.y # 相对坐标 if relJ > 0 and abs(relI) < relJ: self.i -= 1 # 上 elif relI < 0 and relJ > relI: self.j -= 1 # 左 elif relJ < 0 and abs(relJ) > relI: self.i += 1 # 下 elif relI >= 0 and relI >= relJ: self.j += 1 # 右 #判断索引是否在矩阵内 x = self.x_origin + (j + 0.5) * self.pixel_width y = self.y_origin + (i + 0.5) * self.pixel_height col = int((x - dsm_data.GetGeoTransform()[0]) / dsm_data.GetGeoTransform()[1]) row = int((y - dsm_data.GetGeoTransform()[3]) / dsm_data.GetGeoTransform()[5]) z = val self.iteSize += 1 return x, y, z, row, col dsm_path = 'C:\sanwei\jianmo\Productions\Production_2\Production_2_DSM_part_2_2.tif' dsm_data = gdal.Open(dsm_path) dsm_array = dsm_data.ReadAsArray() spiral_iterator = SpiralIterator(dsm_array,x=810,y=500) while spiral_iterator.hasNext(): x, y, z, row, col = spiral_iterator.get() print(f'Value at ({x},{y}) ({row},{col}):{z}')这段代码怎么改可以当原始航片该位置像素值为 0 值,修改其像素值为 255,当 原始航片该位置像素值为 255 时,说明此点已被占用,则对地面点(X,Y,Z)标记此点位被遮蔽
05-24

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值