Programming Languages


foxblock

Asleep
Joined
Jun 17, 2009
Messages
1,563
Location
Germany
Programming is fun, when the computer does what you actually told him to do, but when it just gives out SEGFAULTs or any press of a button results in a cryptic Windows error dialogue (you know this kind: "Error #1254 occurred. Continue?") it can be a real pain in the ass.
You then are about to throw your PC out of the window and when you finally, after 5 hours of rage, find the source of the problem (a missing semicolon) you just want to punch the creator of the language in the face.

So this thread is to post more oddities found in your daily programming or rant about anything you had to deal with.

I may start with this image I found on the net which I find it hilarious (some might already know it):
749cd15bf9d0254286148f468567b29e.jpg

Also a nice blog about some strange JS behaviour, makes me giggle every new day (also funny when you don't know JS or just have a general understanding of programming):
http://wtfjs.com/

Some examples:
Code:
[] == 0
  +[] === 0
  ++[] === 1  // sorta, though this is invalid js syntax, so... 
  [[]][0] === []
  ++[[]][0] === 1
  ++[[]][+[]] === 1  // yay! wtf!

Code:
"0" && {} // true
  0 && {} // false, ok...fair enough
  0 == "0" // true, wtf!

Oh yeah and the all-time classic:
How to shoot yourself in the foot in any programming language

have fun :D

foxblock out
 

Attachments

  • 749cd15bf9d0254286148f468567b29e.jpg
    749cd15bf9d0254286148f468567b29e.jpg
    191.8 KB · Views: 133
I REALLY hate PHP's "Put a dollar before every variable" bullshit. It ends up nailing me on a regular basis because I've never used it in any other language. I think Perl has it. So fuck Perl.
 
These are things that aren't oddities exactly; they make sense after you know how they work but produce nasty bugs the first time you accidentally stumble upon them. I'll add more stuff as soon as I can recall more.

Java:
Code:
float x = 1f / 0f - 1f / 0f;
if(x < 0f)
    System.out.println("less than zero");
else if(x > 0f)
    System.out.println("more than zero")
else if(x == 0f)
    System.out.println("zero");
else
    System.out. println("WTF??");
(prints "WTF??")

Java:
Code:
int x = 42;
x += 3.14159; //compiles + works
//x = x + 3.14159; //doesn't compile
C:
Code:
#include <stdio.h>

int main(int argc, char **argv) {
    int omglolzwtf = 0;
    if(omglolzwtf??!??!1)
        printf("Yeah.\n");
    return 0;
}
(prints: "Yeah.")
GCC requires the "-trigraph" option for this to work, because too many novice users accidentally stumble into this problem.
 
lulzfish said:
I REALLY hate PHP's "Put a dollar before every variable" bullshit. It ends up nailing me on a regular basis because I've never used it in any other language. I think Perl has it. So fuck Perl.
Hehe, PHP in general is some strange programming language and totally overrated. Stuff like Ruby is getting more popular now, but I can remember some time ago nearly anything was made with PHP and Flash.
PHP is nice and easy for some simple variable content, but when you start to use it for logins and databases things have to get fucked up by definition, still so many people tried it.

dflemstr said:
These are things that aren't oddities exactly; they make sense after you know how they work but produce nasty bugs the first time you accidentally stumble upon them. I'll add more stuff as soon as I can recall more.
The first one seems pretty obvious to me - I mean you can't expect infinity to behave like a normal number ;)
The latter two on the other hand...ugh

I also just encountered some strange behaviour in Delphi (speaking of which... I have got to rant about this language in general at some point).
Anyway, I still have to figure this out:
Code:
procedure SomeFunction;
  var Value : Real;
  begin
    Value := 0.2;
    if (Value <> 0.2) then
       ShowMessage('What the....?!'); // Shows "What the....?!"
  end;
Remember it's Delphi, so := is = and <> is != in nearly any other programming language.

foxblock out
 
Last edited by a moderator:
foxblock said:
I also just encountered some strange behaviour in Delphi (speaking of which... I have got to rant about this language in general at some point).
Anyway, I still have to figure this out:
Code:
procedure SomeFunction;
  var Value : Real;
  begin
    Value := 0.2;
    if (Value <> 0.2) then
       ShowMessage('What the....?!'); // Shows "What the....?!"
  end;
Remember it's Delphi, so := is = and <> is != in nearly any other programming language.

foxblock out

I learned about this in my CompSci class. Apparently, floating point numbers are stored in such a convoluted way, that when 0.2 is stored in a variable, it gets turned into 0.2000000000000002 or something.
 
Last edited by a moderator:
Aethix said:
I learned about this in my CompSci class. Apparently, floating point numbers are stored in such a convoluted way, that when 0.2 is stored in a variable, it gets turned into 0.2000000000000002 or something.
This is true. There is no way for most stock "number" types to represent multiples of 0.1 (besides multiples of 1.0), so they get approximated.

It's the same in fixed-point, unless you're using money-oriented fixed-point, which usually has an offset of 100, so 0.2 would be stored precisely as the integer "20".

So there's a saying, "don't use equals with floats, use greater than or .. something". I forgot the saying.
 
Last edited by a moderator:
lulzfish said:
Aethix said:
I learned about this in my CompSci class. Apparently, floating point numbers are stored in such a convoluted way, that when 0.2 is stored in a variable, it gets turned into 0.2000000000000002 or something.
This is true. There is no way for most stock "number" types to represent multiples of 0.1 (besides multiples of 1.0), so they get approximated.

It's the same in fixed-point, unless you're using money-oriented fixed-point, which usually has an offset of 100, so 0.2 would be stored precisely as the integer "20".

So there's a saying, "don't use equals with floats, use greater than or .. something". I forgot the saying.
Yes this approximation was taken advantage of by some cunning coders of yore, when they put rounding errors of various account into one they could access... :lol:
PS. Insomnia sucks.
 
Last edited by a moderator:
PokeParadox said:
Yes this approximation was taken advantage of by some cunning coders of yore, when they put rounding errors of various account into one they could access... :lol:
Actually I recall having read about this story, not that you mention it.

Well, at least I have learned my lesson, dammed floating points.
Thanks for the explanations.
 
Last edited by a moderator:
foxblock said:
PokeParadox said:
Yes this approximation was taken advantage of by some cunning coders of yore, when they put rounding errors of various account into one they could access... :lol:
Actually I recall having read about this story, not that you mention it.

Well, at least I have learned my lesson, dammed floating points.
Thanks for the explanations.
It's the premise (or at least major plot point) of about a dozen movies, including SuperMan 3 and Office Space.
 
Last edited by a moderator:
WizardStan said:
foxblock said:
PokeParadox said:
Yes this approximation was taken advantage of by some cunning coders of yore, when they put rounding errors of various account into one they could access... :lol:
Actually I recall having read about this story, not that you mention it.

Well, at least I have learned my lesson, dammed floating points.
Thanks for the explanations.
It's the premise (or at least major plot point) of about a dozen movies, including SuperMan 3 and Office Space.
And Hackers.
 
Last edited by a moderator:
foxblock said:
The first one seems pretty obvious to me - I mean you can't expect infinity to behave like a normal number ;)
Infinity? Nope. This is from a session in my Scala REPL/interpreter (also runs on the JVM like Java):
Code:
scala> val x = 1f / 0f
x: Float = Infinity

scala> if(x > 0f) println("Definitely greater...")
Definitely greater...

scala> println("The secret is: " + (x - x))
The secret is: NaN

scala>
As I said, easy to understand once you know why...
 
Last edited by a moderator:
dflemstr said:
foxblock said:
The first one seems pretty obvious to me - I mean you can't expect infinity to behave like a normal number ;)
Infinity? Nope. This is from a session in my Scala REPL/interpreter (also runs on the JVM like Java):
Code:
scala> val x = 1f / 0f
 x: Float = Infinity
 
 scala> if(x > 0f) println("Definitely greater...")
 Definitely greater...
 
 scala> println("The secret is: " + (x - x))
 The secret is: NaN
 
 scala>
As I said, easy to understand once you know why...
Well ok, but 1/0 is computed with Infinity...
I am not able to relate to all that "math" and probably every second mathematician wishes the creator of this an eternal damnation in hell ;)
 
Last edited by a moderator:
Back
Top