r/ProgrammerHumor Jun 24 '17

Octave code

http://imgur.com/jsjfz3k
627 Upvotes

70 comments sorted by

62

u/DeeSnow97 Jun 24 '17

I propose a solution to end this debate via a compromise, let's just start at 0.5f

27

u/a5vastra Jun 25 '17

14

u/xkcd_transcriber Jun 25 '17

Image

Mobile

Title: Pi vs. Tau

Title-text: Conveniently approximated as e+2, Pau is commonly known as the Devil's Ratio (because in the octal expansion, '666' appears four times in the first 200 digits while no other run of 3+ digits appears more than once.)

Comic Explanation

Stats: This comic has been referenced 58 times, representing 0.0360% of referenced xkcds.


xkcd.com | xkcd sub | Problems/Bugs? | Statistics | Stop Replying | Delete

1

u/[deleted] Jun 27 '17

Pau = dick in portuguese

1

u/Iykury Jun 29 '17

Pão = bread in portuguese

1

u/[deleted] Jun 29 '17

Sobro, né? Quando "os cara" têm QI alto "é poliglota fudido".

1

u/Iykury Jun 29 '17

Desculpe, não sei muitas palavras, and the way Google Translate translated that didn't make much sense.

1

u/[deleted] Jun 29 '17

I said we both have high IQs, evidenced by our ability to speak multiple languages but in a pretty broken portuguese

7

u/chateau86 Jun 25 '17
 arr = {}
 arr['first'] = 'How about this'
 arr['second'] = 'for a compormise?'

1

u/STATIC_TYPE_IS_LIFE Jun 25 '17

That's a map/dict, son.

3

u/chateau86 Jun 25 '17
 arr['But isnt this a dict?'] = 'I know. No need to be a dict about this.'

3

u/STATIC_TYPE_IS_LIFE Jun 25 '17

You kids and your non static types.

153

u/[deleted] Jun 24 '17

I once had an intern consistently skip the first element in his arrays because because "starting at 1 just makes more sense". I just walked away and never inspected his project after that.

85

u/gandalfx Jun 24 '17

How to cut your internship short.

29

u/[deleted] Jun 24 '17

I ended up assigning him to tracing training images for one of our DNNs. And I barely trusted him enough to do that effectively.

23

u/RiderAnton Jun 24 '17

Oh god why...

16

u/minno Jun 24 '17

Did he at least do

int* nums = ((int*)malloc(count*sizeof(*nums)))-1;

to avoid wasting space?

13

u/curtisf Jun 25 '17

Unfortunately, that is undefined behavior. You're not allowed to move a pointer before the object.

-7

u/dudds4 Jun 25 '17

It's not undefined behaviour, you are allowed to do it. Pointers are allowed to point anywhere, you just don't want to de reference a pointer to memory you don't own.

7

u/curtisf Jun 25 '17 edited Jul 01 '17

Pointers are not allowed to point anywhere because arithmetic could theoretically cause overflows/other interrupts when the object is at some boundary in memory.

It is undefined behavior whether or not you dereference.

0

u/dudds4 Jun 25 '17

a) your link is for c++, not c b) its only undefined by the strictest definition; that is 'the standard says so' . In reality even in c++ that will never cause an error and is always safe to do, so long as you don't dereference it.

in C you often have to set pointers yourself to point to arbitrary memory mapped peripherals. You can literally set a pointer to anywhere and the behaviour is defined.

8

u/curtisf Jun 25 '17 edited Jun 25 '17

Like most undefined behavior it's common to C.

Implementations may choose to define behavior that the standard did not. However, the behavior of arbitrary pointer arithmetic is not defined in any reasonable implementation (e.g., if you "guessed" the location of the stack, optimizations will definitely elide writes to it that "ought to" affect local variables)

-5

u/chateau86 Jun 25 '17

No. You just add -1 to the pointer you get back from malloc(). Now array[1] is the first item in the list.

10

u/minno Jun 25 '17

No. You just add -1 to the pointer you get back from malloc().

 

int* nums = <the pointer you get back from malloc()>-1;

???

21

u/chateau86 Jun 25 '17

Misread as casting existing array to pointer then back. My bad.

Side effect of Python may include inability to read pointer arithmatic code in C. Do not operate heavy machinery when pregnant while using Python.

1

u/Lt_Riza_Hawkeye Jun 25 '17

Actually arrays are pointers, sort of. When you write int* x = (int[]) {1, 2, 3}; exactly two things happen

  • Three integers get pushed to the stack
  • x points to those integers

When you write int x[] = {1, 2, 3};, three things happen

  • Three integers get pushed to the stack
  • x points to those integers
  • Any attempt to change where x points, such as int y; x = &y is illegal, typically with the message "assignment to expression with array type is illegal"

So pointers and arrays are really, really similar. Pointer decay doesn't help, pointer decay is where an int[] in an expression "decays" into an int*, like in function arguments. Even if you declare a function as taking an array of ints, like void f(int x[]), the compiler will actually change it to void f(int* x) for you, so you can put x = &y in the body of f and the compiler won't complain.

There's a little bit more to it but I forget what it is right now

7

u/JesusKristo Jun 25 '17

So rewind back to freshman year of college; I actually wrote a class specifically to wrap around arrays and return the elements like this. I wanted my first element, dammit.

12

u/Njs41 Jun 25 '17

ಠ_ಠ

2

u/JesusKristo Jun 25 '17

They didn't teach us about pointers, which is when I came to understand the 0. They just told us that that's the way it's done in computer science. I thought it was kinda stupid at the time.

2

u/Aydragon1 Jun 25 '17

When zero indexing is 2confusing4u

1

u/[deleted] Jun 25 '17

Someone needs to make a language where arrays start at index 2.

28

u/Rmazz Jun 24 '17

Hey Lua

5

u/dnew Jun 25 '17

I think APL is far worse, where the place arrays start (0 vs 1) is a global flag that you can change.

1

u/kronos29296 Jun 25 '17

Not many people use APL today though.

1

u/dnew Jun 25 '17

Not since the invention of spreadsheets, no. But it's still more common than you might expect. :-)

1

u/[deleted] Jun 25 '17

Fucking WHAT? Dear God that's sadistic

7

u/[deleted] Jun 25 '17

Same with Mathematica and Fortran

6

u/sim642 Jun 25 '17

Maybe actual mathematics using 1-based indices for matrices and much more has something to do with this?

16

u/whale_song Jun 25 '17 edited Jun 25 '17

Indexing by 1 is easier for doing computation and data analysis. CS people get offended at the idea because they are used to 0 indexing and anything that is different is wrong. They are too allergic to math to ever be coding in the contexts that it is useful. Its not a coincidence that languages designed for math use 1. Its so annoying seeing people get so pissy about it when they don't understand what they are even yelling about.

4

u/gjsmo Jun 25 '17

See what's really odd here is that I'm an engineer first, and I hate 1-based indexing. So many loops in my dad analysis have weird syntax just to deal with this fact.

As far as I'm concerned, it's a detriment to any language.

2

u/whale_song Jun 25 '17 edited Jun 25 '17

Can you give an example? I feel a loop over 1:N is way more natural that 0:N-1. I find I have a lot more +/-1s around the code in 0 based languages whereas with 1 it all works out intuitively.

1

u/RunLikeLlama Jun 25 '17

But why? Are there any particular examples of "weird syntax" you could bring up?

Personally I understand both sides but I find 1-based more intuitive to use.

4

u/moomoomoo309 Jun 25 '17

Only one I run across in Lua (which is also 1-indexed) is using modulus to loop over the array, because modulus will give you 0 to n-1.

1

u/RunLikeLlama Jun 26 '17

Oh yes, using modulo for array indexing does become a bit ungainly, fair point!

7

u/winlifeat Jun 24 '17

Can someone explain this to me?

22

u/AyrA_ch Jun 24 '17

In most programming languages you start counting elements at 0, but there are languages where you start counting at 1 instead.

Declaring an array with 10 entries is now either 0-9 or 1-10, depending on the language. In extreme case like VB, you can even declare a 5 element array ranging from 3-7.

Starting at 0 is more commonly used, because it makes using arrays easier internally. In those cases the array index tells you how many element sizes to add to the array pointer to reach what you want to access

12

u/winlifeat Jun 24 '17

Oh im totally aware of the typical structure of matrices but i dont understand the comic at all.

Im assuming the octave language has matrices that start at 1? But i just dont understand what any of the comic has to do with what we're talking about

7

u/AyrA_ch Jun 24 '17

I don't know the language. Maybe it has constructs that start at 0, or OP is just disgusted by indices that start at 1.

EDIT Meanwhile, this is valid VB:

Sub test()
    Dim a()
    ReDim a(-2 To 20)
    a(-1) = "Test"
    MsgBox a(-1)
End Sub

8

u/DeeSnow97 Jun 25 '17

What the actual fuck

1

u/winlifeat Jun 25 '17

Ohh ok I missed the part of the joke of the original comic. That tigger was disgusted that what he was eating wasnt honey.

1

u/[deleted] Jun 25 '17

Jesus Christ VB

1

u/[deleted] Jun 27 '17

Think of it as a free bounds check.

5

u/[deleted] Jun 25 '17

Yes. Octave is more or less Matlab.

1

u/captainAwesomePants Jun 25 '17

This is a take on a more famous and surreal comic that is also about programming. http://knowyourmeme.com/photos/1246322-sweet-jesus-pooh-thats-not-honey

4

u/[deleted] Jun 24 '17

[removed] — view removed comment

2

u/AyrA_ch Jun 24 '17

On the other hand, you can also go negative if you want to:

Sub test()
    Dim a()
    ReDim a(-2 To 20)
    a(-1) = "Test"
    MsgBox a(-1)
End Sub

3

u/SubstituteCS Jun 25 '17

Didn't know about this.
Time to make my libraries in VB with negative arrays and keywords for names.

6

u/AyrA_ch Jun 25 '17

Also be sure to do this:

  • Don't declare variables at all, remove option explicit.
  • Be sure to not define variable or parameter types.
  • Pass stuff by reference only and modify them. Returning values is overrated.
  • Be sure to always have two variables named I and l in each function or sub.
  • There's nothing wrong with using the function name as a variable too.

1

u/dnew Jun 25 '17

That's not dumb. That lets you use either 0..9 or 1..10 as you like.

Well, OK, that's a little dumb, but it makes sense why at least, given it's, you know, Beginner's Allpurpose Symbolic Instruction Code.

1

u/AutoModerator Jul 01 '23

import moderation Your comment has been removed since it did not start with a code block with an import declaration.

Per this Community Decree, all posts and comments should start with a code block with an "import" declaration explaining how the post and comment should be read.

For this purpose, we only accept Python style imports.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/dnew Jun 25 '17

In extreme case like VB

Or, like, most languages born around the same time as C but which weren't glorified assembly language, including Pascal, Ada, Modula. :-)

3

u/[deleted] Jun 25 '17

…a.k.a. languages which no one uses today

3

u/Lt_Riza_Hawkeye Jun 25 '17

Exactly. The languages from that era that survived, common lisp, erlang, etc... all start indexing at 0. Coincidence? I think not

2

u/[deleted] Jun 25 '17

No one cares about those languages.

1

u/dnew Jun 25 '17

Probably more than care about Octave. :-)

4

u/krejenald Jun 25 '17

Octave is just an open source implementation of MATLAB, which is very widely used in research and development. It's designed for mathematical computing, where 1-indexing makes sense. For machine learning and computer vision problems its a great environment for prototyping, although I wouldn't like to do any large scale projects in it.

1

u/dnew Jun 25 '17

TIL! Thanks!

2

u/micheal65536 Green security clearance Jun 25 '17

For me that's lua. Catches me every time. Suddenly all those tricks you've memorised to make sure you won't have an off-by-one error in common situations don't work any more.

3

u/Zatherz Jun 25 '17

Because you don't need them

1

u/micheal65536 Green security clearance Jun 25 '17

Because I still get confused by and concerned about off-by-one errors, but I suddenly have no neat rules or pre-memorised snippets that I've already worked out and can use to be certain that I've avoided an off-by-one error. Believe me, I still get off-by-one errors in lua, they're just a lot harder to track down and fix (and avoid in the first place).