|
|
Subscribe / Log in / New account

An introduction to the Julia language, part 1

An introduction to the Julia language, part 1

Posted Aug 30, 2018 9:53 UTC (Thu) by danielpf (guest, #4723)
In reply to: An introduction to the Julia language, part 1 by jem
Parent article: An introduction to the Julia language, part 1

>The problem is not the wasted memory word, or how much energy and time an increment costs. The problem is the explicit and error prone +1s and -1s in your code, when working with 1-based vectors and matrices on a problem that is better suited for 0-based indexing. There are a lot of problems like this; the Discrete Fourier Transform, for example.

This is a legend, because the +1 bug occurs in any language. For example in 0-based indexing you loop from 0 to N-1, so you must not forget the -1. I have programmed scientific applications in many languages overs decades and found that both kinds of indexing offer advantages and disadvantages in different contexts. In both indexing ways the +1 bug is lurking.


to post comments

An introduction to the Julia language, part 1

Posted Aug 31, 2018 6:19 UTC (Fri) by epa (subscriber, #39769) [Link] (1 responses)

Typically in languages with 0-based indexing the idiom will be to use a < comparison for the upper bound, as

for (int i = 0; i < N; i++)

So N-1 isn’t usually needed.

A neat helper, too, is Python’s half-closed range for list slices, a[n:m] giving m-n elements. This is more convenient than a .. inclusive range for many programming tasks.

An introduction to the Julia language, part 1

Posted Sep 6, 2018 6:43 UTC (Thu) by Wol (subscriber, #4433) [Link]

> Typically in languages with 0-based indexing the idiom will be to use a < comparison for the upper bound

Which is a trap for the unwary, or noob.

All languages have idioms. I once wrote a piece of code - in a class for C beginners - that effectively did

count = 0
for i = 1 to 13
count += (cardvalue == 3)
next

to find out how many threes were in the hand. I just could not get the lecturer to write the addition line correctly on the board until I told him to write it down, character by character, as I spelt it out. To me, that idiom is second nature, to him it was totally alien.

And yet, it SHOULD be completely natural to users of any language (like C) where the language defines the result "true" as being represented by the integer "1". (Note I said "result" not "value" ...)

and you'll find plenty of C programmers to whom the "<" idiom is alien, until they've been programming in C for quite a while and had it rammed in to them.

Cheers,
Wol


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds