python 中的奇怪浮点数比较
Strange floating point comparison in python
我对 python 浮动比较逻辑感到疯狂,你们能告诉我我应该怎么做才能强制 python 正确地进行比较吗:
(Pdb) type( nonOverlaps[-1].end )
<type 'float'>
(Pdb) type(interv.start)
<type 'float'>
(Pdb) p nonOverlaps[-1].end
381690.887195
(Pdb) p interv.start
381682.616861
#So, nonOverlaps[-1].end is LARGER than interv.start
(Pdb) p nonOverlaps[-1].end <= interv.end
True
(Pdb) p nonOverlaps[-1].end < interv.end
True
#OMG, this comparison return value should be False
您的问题是代码中的拼写错误。当您应该与 interv.start.
进行比较时,您正在将 nonOverlaps[-1].end 与 interv.end 进行比较
我对 python 浮动比较逻辑感到疯狂,你们能告诉我我应该怎么做才能强制 python 正确地进行比较吗:
(Pdb) type( nonOverlaps[-1].end )
<type 'float'>
(Pdb) type(interv.start)
<type 'float'>
(Pdb) p nonOverlaps[-1].end
381690.887195
(Pdb) p interv.start
381682.616861
#So, nonOverlaps[-1].end is LARGER than interv.start
(Pdb) p nonOverlaps[-1].end <= interv.end
True
(Pdb) p nonOverlaps[-1].end < interv.end
True
#OMG, this comparison return value should be False
您的问题是代码中的拼写错误。当您应该与 interv.start.
进行比较时,您正在将 nonOverlaps[-1].end 与 interv.end 进行比较