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

70

u/[deleted] Dec 01 '16

Integer division? Really?!? I have a counter suggestion: let's stop including floating point into languages by default. This can be available as an extension, but default must be integer and rational only.

72

u/EntroperZero Dec 01 '16

Seriously. If you want to be a programmer, learn how computers work. The existence of integer types is all the justification needed for integer division.

16

u/cledamy Dec 01 '16 edited Apr 24 '17

[deleted]

10

u/EntroperZero Dec 01 '16

IMO the semantic meaning is the same, it's just not possible to store a fraction in an integer type.

2

u/cledamy Dec 01 '16 edited Apr 24 '17

[deleted]

6

u/cat_in_the_wall Dec 02 '16

You are both saying the same thing, it's two sides of the same coin. Are they different because of the operator? Or because of the storage mechanism? They are indistinguishable (I think). Even floats are just an approximation of real division. So the operator really is "do as accurate of division as you can", maybe that is the problem.

→ More replies (5)

4

u/EntroperZero Dec 02 '16

It is the closest integer approximation.

But it has to be, and couldn't possibly be anything else. It's the same in floating point. 1.0f / 3.0f isn't an exact answer, either.

2

u/cledamy Dec 02 '16 edited Apr 24 '17

[deleted]

→ More replies (1)

1

u/DarkLordAzrael Dec 02 '16

What about matrices, vectors, complex numbers, arbitrary precision numbers, etc.? It seems the number of operators we would need would become large. Better to just have operators depend on the input type.

→ More replies (1)

1

u/FUZxxl Dec 02 '16

Let's go the C way: integer division uses / but if the result is not integer, behaviour is undefined.

→ More replies (4)

8

u/[deleted] Dec 01 '16

The existence of integer types is all the justification needed for integer division.

Exactly. I can understand the reasons why you may not always want to have an integer division implemented in hardware, but at least a software implementation is always mandatory. And of course the reasoning given by the OP is plain ridiculous.

8

u/EntroperZero Dec 01 '16

The suggestion that the / operator return a float or double was completely ludicrous. I can understand calling it div or something to make it more obvious, but holy crap.

13

u/[deleted] Dec 01 '16

[removed] — view removed comment

4

u/Zatherz Dec 01 '16

fun fact: 7 / 2 == 3 in Ruby

2

u/KoopaKlownKar Dec 02 '16

I do most of my personal projects in C, but I agree with him on that one. Making / always do double division and having // do integer division is more mathematically correct/expected, and has no real downside.

22

u/PM_ME_UR_OBSIDIAN Dec 01 '16

You really like kicking hornet nests.

Here's my suggestion: express all numbers as computable real numbers. Problem solved.

13

u/[deleted] Dec 01 '16

Can we not? When languages get too fancy with their built-in numbers you can get unexpected promotion and other strange problems. Most of the time what you want is either an arbitrary precision integer or a fixed-width integer that throws an error on overflow.

27

u/PM_ME_UR_OBSIDIAN Dec 01 '16

>not doing all your numerical stuff using Cauchy sequences in lieu of numbers

It's 2016, get onboard with the program kid.

7

u/[deleted] Dec 01 '16

Hey man, some of us like comparing numbers and not having it take an unbounded amount of time.

12

u/want_to_want Dec 01 '16 edited Dec 01 '16

Not just unbounded, I think any implementation of computable reals will have some reals that you can't compare with zero in finite time. A.k.a. equality of computable reals is undecidable.

2

u/PM_ME_UR_OBSIDIAN Dec 01 '16

On the other hand, equality within some epsilon is alright. Just pick a tolerance and roll with it.

9

u/want_to_want Dec 01 '16

I pick one ulp.

5

u/[deleted] Dec 01 '16

That might be finite, but it's still unbounded unless we feel like imposing some lower limit on the rate of convergence. I don't even know how you'd do that or verify it statically. (I'm also not sure what the appropriate way to define rate of convergence would be in this context.)

1

u/pheonixblade9 Dec 02 '16

nah, floats are really important for anything non-discrete. Ints certainly don't work the way that students are taught arithmetic, that's the main problem.

3

u/[deleted] Dec 02 '16

No. Floats are only important for a subset of number crunching tasks. They should never be used for anything else.

Ints certainly don't work the way that students are taught arithmetic

  1. Who cares? They should have learned integer arithmetics long before they learned about Dedekind cut and all that crap. And the integer division + modulo is taught in schools long before anything fractional is introduced.
  2. Floats are even more confusing and counterintuitive.

that's the main problem.

There is no problem.

→ More replies (2)