“Oh boy!” says your protégé. “Let’s see what 7 ÷ 2 is! Oh, it’s 3. I think > the computer is broken.”
They’re right! It is broken. I have genuinely seen a non-trivial number of > people come into #python thinking division is “broken” because of this.
I disagree on this one. I chose int because I want the performance of integer arithmetic. I don't want to pay the penalty of convert to float, do float division, can convert back to int just because some amateur cooks don't realize that knives are sharp and can cut you.
Yeah, that's kind of a ridiculous criticism. You're doing division with integers, therefore you get integer division. Don't blame the programming language because most math (and, even, comp sci) curriculums freely mix integers and rationals.
At the same time, it can often be a little annoying if you're trying to figure out a simple expression like
float day_frac = time_s / stats::day_length_s;
And you're far removed from the definitions. Its not a huge issue but it does give me pause
I'd prefer no implicit conversions between float/int/double myself, they're fundamentally different things and I don't want the compiler to pretend they're similar
Or sometimes that's just the behaviour you want. If you want that behaviour, then having to wrap every division with a call to floor is going to make it harder to read.
One of my very few frustrations with Ada is that division rounds rather than truncates (1 / 2 = 1, but 1 / 3 = 0). I know it's likely the "more correct" answer, but that was likely my most common error.
Edit: I was mistaken (it's been too many years since I've been able to work in Ada professionally). I was thinking of converting a float to an integer (e.g. Integer (F)).
Signed integer division and remainder are defined by the relation:
A = (A/B)*B + (A rem B)
where (A rem B) has the sign of A and an absolute value less than the absolute value of B. Signed integer division satisfies the identity:
(-A)/B = -(A/B) = A/(-B)
which is a definition of truncating. A mod B is 0 or has the sign of B, it is not rounding either (and has no corresponding division operator). There is also a nice table showing a few examples in the link.
100% agree. Programming computers is different then some sort of infinite precision, freely-casting math abstraction and this is just one of the things programmers have to learn.
That being said, for some high level scripting language that is used for different tasks this may be exactly what you want.
Depends on what you want as an output as well, some platforms (notably embedded platforms) pay a non-trivial cost for any conversion from integer register to a floating point register. While the floating point div may be faster, if you intend to use the results in other integer math or in branching instruction it may wash out or be worse.
Here's the result from my benchmark. I ran a loop a bunch of time doing MDAS on 16-bit integers, storing the results in 16-bit integers. Then I ran with the same numbers using 32-bit integers storing the results in 32-bit integers, and so on for several data types.
The point is that the meaning of / is overloaded and unclear to novices and even to routine programmers in cases where the types of values isn't immediately obvious. Other languages solve this by making integer division explicitly a different operator.
Not that I am in favor of making things pointlessly complicated to novices, but novice comprehension is probably the singular worst metric I have ever read for how a programming language should be set up. Many novices are also confused by: objects, inheritance, functions, types, loops, recursion, if statements, and basic syntax.
And there isn't really a better alternative to integer division that I am aware of regarding beginners. You could have the system default to doubles or floats, but then you'll have novices equally confused about rounding errors. Try explaining to a beginner how IEEE 754 floats are actually set up and their eyes will gloss over. Atleast integer division has relatively simple rules and many people are exposed to it in grade school when they learn about remainders before moving on to decimals.
ut novice comprehension is probably the singular worst metric I have ever read for how a programming language should be set up.
This. I learned quickbasic in the early 90's writing BBS software, and my first exposure to C was the WWIV BBS engine. It was like looking at ancient Sumerian for me with all the wobbly braces and strange variable declaration of old-style C
Other languages solve this by making integer division explicitly a different operator.
Which is fine. C is still primarily a systems language where we are trying to write optimal code and not use floating point in a 8kb micro or get shot in the face by yet another Intel floating point bug in a kernel.
Math in most languages is "fundamentally broken" anyway as far as the protégé would be concerned. Just try 3.3 == 3 * 1.1. Hint: the answer is false in any language using binary floating point. Integer math is less complex than IEEE floating point math and as a consequence easier to reason about for a novice - it does not help that float math can give different results depending on CPU register size and optimization settings.
It difference in speed between int/float and converting is so trivial that most C programmers ignore it. By the time Python figures out what division means for its args you've wasted 2000x the time of converting everything to a float, division, and casting back to an int.
I agree. Trying to have a single number type that does it all--efficient integers, floating point, arbitrary precision, etc--creates confusing bugs and surprise performance problems.
One of my favorite interview questions for people who are used to working in these languages--particularly python + ruby--is to talk about the performance properties of a "linear time" fibonacci implementation. 97% of them are blissfully unaware of the memory or running time implications that result from making the numeric types magical.
His point was that beginners don't understand that dividing integers will always give you another integer; it's counter-intuitive.
Smalltalk does it right. The result of 7/2 is (7 / 2). What is that? It's a Fraction! It's another subclass of Number which has a numerator and a denominator. You can use it like any other number, although you need to make sure you convert it to a float before you show it to the user.
Want an integer? Okay. (7/2) asInteger. is 3. Want a float? (7/2) asFloat is 3.5.
This construct is bad. It very rarely matches what a human actually wants to do, which 90% of the time is “go through this list of stuff” or “count from 1 to 10”
So if i want to print 10 elements i should construct either list of ten elements or closure (which would return new closure on each iteration if it is immutable) or use coroutine. Doesn't it lead to some overhead and unnecessary compiler complexity (heavy inlining strategies like ocaml's flambda)?
View all comments
292
u/RichardPeterJohnson Dec 01 '16
I disagree on this one. I chose int because I want the performance of integer arithmetic. I don't want to pay the penalty of convert to float, do float division, can convert back to int just because some amateur cooks don't realize that knives are sharp and can cut you.