On Wed, May 6, 2020 at 10:26 PM Oscar Benjamin <oscar.j.benja...@gmail.com>
wrote:

> > That's the point though.  For *most* functions, the substitution
> principle is fine in Python.  A whole lot of the time, numeric functions
> can take either an int or a float that are equal to each other and produce
> results that are equal to each other.  Yes, I can write something that will
> sometimes overflow for floats but not ints.  Yes, I can write something
> where a rounding error will pop up differently between the types.  But
> generally, numeric functions are "mostly the same most of the time" with
> float vs. int arguments.
>
> The question is whether you (or Chris) care about calculating things
> accurately with floats or ints. If you do try to write careful code
> that calculates things for one or the other you'll realise that there
> is no way to duck-type anything nontrivial because the algorithms for
> exact vs inexact or bounded vs unbounded arithmetic are very different
> (e.g. sum vs fsum).


Sure. But a great many things I calculate are not particularly exact.  If I
want the mean of about a hundred numbers that are each somewhere in the
interval [1, 1e6], I'm probably not very interested in 1 ulp errors in
64-bit floating point.

And when I *do* care about being exact, I can either cast the arguments to
the appropriate type or raise an exception for the unexpected type.  If my
function deals with primes of thousands of digits, int is more
appropriate.  But maybe I want a Decimal of some specific precision.  Or a
Fraction. Or maybe I want to use gmpy as an external type for greater
precision.  If it's just `x = myfavoritetype(x)` as the first line of the
function, that's easy to do.


> I have fixed enough real examples of bugs relating to
> this to come to the conclusion that making non-interchangeable objects
> compare equal with == is an attractive nuisance.


Yeah, sometimes.  But not nearly as much of an attractive nuisance as using
`==` between to floating point numbers rather than math.isclose() or
numpy.isclose().  My students trip over ` (0.1+0.2)+0.3 == 0.1+(0.2+0.3)` a
lot more often than they trip over `1.0 == 1`.

-- 
The dead increasingly dominate and strangle both the living and the
not-yet born.  Vampiric capital and undead corporate persons abuse
the lives and control the thoughts of homo faber. Ideas, once born,
become abortifacients against new conceptions.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/RYFOIGPVJTURGWTEGHW3HWVQNOWOXK6N/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to