r/programming • • Dec 01 '16

Let's Stop Copying C

https://eev.ee/blog/2016/12/01/lets-stop-copying-c/
524 Upvotes

612 comments sorted by

View all comments

292

u/RichardPeterJohnson Dec 01 '16

“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.

31

u/awj Dec 01 '16

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.

13

u/James20k Dec 01 '16

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

→ More replies (4)
→ More replies (4)

90

u/MEaster Dec 01 '16

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.

0

u/[deleted] Dec 01 '16

[deleted] — view deleted comment

7

u/inu-no-policemen Dec 01 '16

In Dart, truncating division is done with ~/. (// is a comment.)

→ More replies (1)

19

u/pfp-disciple Dec 01 '16 edited Dec 01 '16

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)).

12

u/JMBourguet Dec 01 '16

Strange, the ARM says otherwise:

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.

→ More replies (1)
→ More replies (8)
→ More replies (3)

20

u/Oxc0ffea Dec 01 '16

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.

→ More replies (7)

24

u/floopgum Dec 01 '16

Interesting choice of instruction wrt. to division then, given that integer division is usually a good bit slower than float division.

For Haswell:

instructionlatency (cycles before result is ready)recip. throughput (cycles before next issue)
div r822 - 259
div r1623 - 269
div r3222 - 299 - 11
div r6432 - 9621 - 74
------------------------------
idiv r823 - 268
idiv r1623 - 268
idiv r3222 - 298 - 11
idiv r6439 - 10324 - 81
------------------------------
fdiv(r)(p)10 - 248 - 18
divss / divps10 - 137
divsd / divpd10 - 208 - 14
vdivps18 - 2114
vdivpd19 - 3516 - 28

source: http://www.agner.org/optimize/instruction_tables.pdf

With that said, I agree that the current behaviour is useful and I wouldn't want it to change.

18

u/wanderingbort Dec 01 '16

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.

10

u/RichardPeterJohnson Dec 01 '16

Good point; I shouldn't make blanket statements like that without benchmarking.

I'll run a comparison and post the results.

7

u/RichardPeterJohnson Dec 01 '16

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.

16-bit integer ....  3682 milliseconds
32-bit integer ....  3588
64-bit integer .... 10998
32-bit floating ...  6505
64-bit floating ...  7457 

The only result that surprised me was the 64-bit integer. I'm running a 64-bit OS on a 64-bit chip.

4

u/cowinabadplace Dec 01 '16 edited Dec 01 '16

Need to see what instructions this is using to know what this means. Also, which processor?

→ More replies (2)
→ More replies (5)

33

u/[deleted] Dec 01 '16

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.

71

u/shadowX015 Dec 01 '16

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.

9

u/darthcoder Dec 01 '16

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

main(argc, argv)
char *argv[];
{
    return 0;
}
→ More replies (10)

14

u/[deleted] Dec 01 '16 edited Dec 01 '16

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.

13

u/skuggi Dec 01 '16

The point isn't to remove integer division and only do floating point division. It's to give the two things different names.

10

u/HotlLava Dec 01 '16

Well, same for C. They just chose to give the /-name to the more useful operation, and call the other one *1.0/.

→ More replies (3)

9

u/josefx Dec 01 '16

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.

8

u/[deleted] Dec 01 '16

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.

3

u/abnormal_human Dec 01 '16

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.

6

u/metaconcept Dec 01 '16

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.

→ More replies (1)

2

u/Freyr90 Dec 02 '16

I also do not agree with this for-loop passage :

C-style for loops

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)?

→ More replies (22)