Detecting inadvertent use of integer division
bearophile
bearophileHUGS at lycos.com
Mon Dec 14 12:45:15 PST 2009
Don:
>You've used Python extensively, I haven't.<
The situation with Python isn't the same of D because Python has dynamic typing, so your operations must always know what they return (and their return type can change when necessary).
>in what contexts is / ambiguous or difficult to read?<
A silly example, maybe useless for you:
auto x = 5 / foo();
>BTW, does Python allow integer division of floating point numbers?<
This is how Python2.x used to work:
>>> 5 / 2
2
>>> -5 / 2
-3
>>> 5.0 / 2
2.5
>>> 5 / 2.0
2.5
>>> -5.0 / 2
-2.5
This is how Python3.x now works:
>>> from __future__ import division
>>> 5 / 2
2.5
>>> -5 / 2
-2.5
>>> 5.0 / 2
2.5
>>> 5 / 2.0
2.5
>>> -5.0 / 2
-2.5
>>> 5 // 2
2
>>> -5 // 2
-3
>>> 5.0 // 2
2.0
>>> 5 // 2.0
2.0
>>> -5.0 // 2
-3.0
I am not saying that Python behaviour is the best possible :-)
I think I'd like // to always return a int.
In Pascal:
int / int => float
float / int => float
int / float => float
float / float => float
int div int => int
float div int => idontremember
int div float => idontremember
Bye,
bearophile
More information about the Digitalmars-d
mailing list