I have come across a very strange issue in python. (Using python 2.4.x)
In windows:
>>> a = 2292.5
>>> print '%.0f' % a
2293
But in Solaris:
>>> a = 2292.5
>>> print '%.0f' % a
2292
But this is the same in both windows and solaris:
>>> a = 1.5
>>> print '%.0f' % a
2
Can someone explain this behavior? I’m guessing it’s platform dependent on the way that python was compiled?
The function ultimately in charge of performing that formatting is
PyOS_snprintf(see the sources). As you surmise, that’s unfortunately system-dependent, i.e., it relies on
vsprintf,vsnprintfor other similar functions that are ultimately supplied by the platform’s C runtime library (I don’t recall if the C standard says anything about the ‘%f’ formatting for floats that are “exactly midway” between two possible rounded values… but, whether the C standard is lax about this, or rather the C standard is strict but some C runtimes break it, ultimately is a pretty academic issue…).