I'm having a problem comparing floats, given the values below, I'm getting the wrong results when running this piece of code

let distAppart = 4.76837158203e-007
let factor = 0.01

if distAppart < factor and distAppart > -factor:
	print 'this should not print with the given values.... But it does anyway!!!!!!!!!!!'
else:
	print 'this is what should really be shown'

Think about it ...

distAppart < factor
distAppart > -factor

Both statements are true so it prints what it is supposed to.

Think about it ...

distAppart < factor
distAppart > -factor

Both statements are true so it prints what it is supposed to.

This was me being stupid and not actually looking at the exponent, I thought python was trying to pull the wool over my eyes and tell me that 4 > 0. Thanks for you help anyway. Yes I now see that both are true.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.