swede at earthling.net wrote:
> While T-division is the most common choice (used by C, C++, etc) F-division 
> is not uncommon. I've confirmed that Python and Ruby use F-division, and 
> Wikipedia claims that TCL and D do too. F-division behavior won't be 
> surprising or hard to explain to any users familiar with Python, which is 
> most developers today.

FWIW, I've personally been burned by this in python and I went around
asking people if they were aware of it and even very experienced
pythonists friends were not. Certainly a better job could be done
communicating about it than Python has done.   I encountered more
people who were surprised at what python was doing than people who
were unaware that integer divide was usually truncating (this may be a
product of sampling too many low level C programmers).

The C behavior makes /  truncating, which makes it consistent with casting.

There are certainly common cases where what you want is the quotient
and remainder. error_feet = error_in/12; error_in %=12;

> 3.  x / (2^n) can be converted to a bitwise right shift for any x
> 4.  x % (2^n) can be converted to a bitwise and for any x

When I want these cases I use the shifts and ands explicitly.  At
least in C this isn't out of line with writing code that reflects the
underlying behavior and performance. This helps make it clear that
changing the divisor has implications in whats actually being
performed. Perhaps I'm weird.

> Property 2 is useful for things like sample-rate conversion

If you're doing sample rate conversion in a way that this comes up—
you likely have bigger problems. :)

Not having the truncating case available would be bad... Graydon's
comments about both being available sounds good at least.

>  * Performance will stay the same for division by compile-time constants, 
> since these are transformed by the compiler into multiplies. (I actually 
> think performance will go up slightly in this case, but it's been a while 
> since I looked at the algorithm.)

For many constants its the same number of operations, for some the
flooring division saves an add and an and (or a shift and a subtract)
though I wouldn't actually expect this to make a measurable
performance difference anywhere.
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to