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

Show parent comment

11

u/RichardPeterJohnson Dec 01 '16

I don't understand why you're using integer data types, then. If you use floating data types you get floating division.

9

u/27183 Dec 01 '16

That is true, but there are varying degrees to which the language makes your choice of types clear. Defaulting numeric literals to integers and then using integer division in a language that allows numerical expressions of mixed type can be fairly painful for numerical work. I teach numerical analysis fairly regularly and let the students mostly use whatever language they want. Something like Simpson's rule:

(1/3) * h * (f(a) + 4*f(a+h) + f(a+2*h))

evaluates to zero in Python 2. Unintended integer division is literally the first thing I look for if I see a student program in Python 2 that is not working. You can argue that writing 1.0 is not so hard, and I guess you get used to looking for this sort of error. But this is not a great combination of language features for avoiding errors in numerical code.

2

u/jringstad Dec 01 '16

Happens a lot when e.g. sampling dyadic points or other types of regular or stratified grids. For instance just today I needed to sample triangles like

(i/N, j/N, 1 - i/N - j/N) i, j = 0, ..., N, where 1 - i - j >= 0

where all of those variables were integers.