Paul D. Anderson wrote:
I was browsing the Python spec yesterday and came across this interesting and 
useful syntax:

"/" (one slash) means floating point division, e.g. 5/2 = 2.5 even though 5 and 
2 are integers

"//" (two slashes) means integer (floor) division, e.g. 5.0//2.0 = 2.0 even 
though 5.0 and 2.0 are floats.

I've always been a little troubled by the standard division operator being 
dependent on the types of the operands. (I understand the need for it, I just 
didn't like it much.) Now here is an elegant (IMHO) solution.

I like the idea of adding a "floating point division", but I think it should be a *new* feature while the current / and // are left as they are. How about "./"?

5   /  2   = 2         // Current behavior
5.0 /  2.0 = 2.5       // Current behavior
5   ./ 2   = 2.5       // New behavior
5.0 ./ 2.0 = 2.5       // It always floating-point divides

IOW, ./ is short for "cast operands to double and divide".

If ./ proves too hard to parse (I think it might require some lookahead), then /^ might work.

Reply via email to