On Tue, Sep 12, 2017 at 09:23:04AM +0200, Victor Stinner wrote: > 2017-09-12 3:48 GMT+02:00 Steven D'Aprano <st...@pearwood.info>: > >> k = float("0x1.2492492492492p-3") # 1/7 > > > > Why wouldn't you just write 1/7? > > 1/7 is irrational, so it's not easy to get the "exact value" for a > 64-bit IEEE 754 double float.
1/7 is not irrational. It is the ratio of 1 over 7, by definition it is a rational number. Are you thinking of square root of 7? 1/7 gives the exact 64-bit IEEE 754 float closest to the true rational number 1/7. And with the keyhole optimizer in recent versions of Python, you don't even pay a runtime cost. py> (1/7).hex() '0x1.2492492492492p-3' I do like the idea of having float hex literals, and supporting them in float itself (although we do already have float.fromhex) but I must admit I'm struggling for a use-case. But perhaps "C allows it now, we should too" is a good enough reason. > I chose it because it's easy to write. Maybe math.pi is a better example :-) > > >>> math.pi.hex() > '0x1.921fb54442d18p+1' 3.141592653589793 is four fewer characters to type, just as accurate, and far more recognisable. -- Steve _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/