Reputation: 371
In python using //
for division forces the result to be an integer. Is there an equivalent for multiplication?
For example, assume I have an integer W
which I scale by a float f
. It could be nice to have an operator such as .*
, so that:
int(W*f)==W.*f
Would be True.
Upvotes: 12
Views: 13221
Reputation: 4427
No, and there is unlikely to be one added, for two reasons.
When you take 12/5 you can reasonably want a integer, quotient, or real, all of which are well defined, take different values in python (without infinite floating precision) and behave differently.
When you multiply 12*5, you could also want those three, but the value will be identical.
For something like pi * 100000, you would need to know the type to end up with as well as the resolution technique for e.g. float to integer (floor, ceiling, round closest, round .5 up, round .5 down, bankers rounding). Without strong types this becomes a mess to hand down from above, and easier to delegate to the user and their own needs or preferences.
Upvotes: 5
Reputation: 8395
//
does not "force the result to be an integer", this may be coincidentally true, but describing the operator in this presumptuous way is (I believe) resulting in you thinking that there should be other analogous features, which there really aren't. //
is "floor division", which any type can overload to have any desired behaviour. There is no "floor multiplication" operator. If you want the result of multiplication to be forced to an integer, you've already shown a perfectly acceptable and straightforward way to do this:
int(W*f)
Upvotes: 6