It seems that you're using an outdated browser. Some things may not work as they should (or don't work at all).
We suggest you upgrade newer and better browser like: Chrome, Firefox, Internet Explorer or Opera

×
By the way, regarding gets(), apparently an old version of the finger daemon had code like this:

char buffer[512];
gets(buffer);

Can you see the problem here? (Hint: What happens if the user (or a program acting on behalf of the user) sends a string that doesn't fit in the buffer?)
LabView for anything more than controlling hardware. Nested If Then case structures.
Then of course there's code that's not bad as such, in fact it's so good it's almost magical, but quite unintuitive and extremely difficult to understand. Example: The fast inverse square root algorithm.

For those unfamiliar with it, calculating the inverse square root of a floating point number is an important part of lighting and reflection calculations used in 3D graphics. As such, it is quite important that this can be done very very quickly. The algorithm has been wrongly attributed to John Carmack, because is was used in Quake III Arena, but Carmack only used it, he didn't invent it.

For your enjoyment, here is the code of the function used in Quake III Arena:

float Q_rsqrt( float number )
{
long i;
float x2, y;
const float threehalfs = 1.5F;

x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed

return y;
}
Edit:
The GOG forum does not like whitespaces. I really wish we could use code tags here. For a properly formatted version of the code, see the Wikipedia page linked at the top of this post.
Post edited December 28, 2015 by Wishbone
Easy: The Code of Hammurabi.
The code an idiot would have on its luggage.

Also....
avatar
Wishbone: Then of course there's code that's not bad as such, in fact it's so good it's almost magical, but quite unintuitive and extremely difficult to understand. Example: The fast inverse square root algorithm.

For those unfamiliar with it, calculating the inverse square root of a floating point number is an important part of lighting and reflection calculations used in 3D graphics. As such, it is quite important that this can be done very very quickly. The algorithm has been wrongly attributed to John Carmack, because is was used in Quake III Arena, but Carmack only used it, he didn't invent it.

For your enjoyment, here is the code of the function used in Quake III Arena:

float Q_rsqrt( float number )
{
long i;
float x2, y;
const float threehalfs = 1.5F;

x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed

return y;
}
avatar
Wishbone: Edit:
The GOG forum does not like whitespaces. I really wish we could use code tags here. For a properly formatted version of the code, see the Wikipedia page linked at the top of this post.
It's interesting to note that the language Rust has an explicit way to do what he is doing. In Rust, the code would look something like this:

fn Q_rsqrt(number: f32) -> f32 {
let threehalves = 1.5; //const is the default in Rust

let x2 = number * 0.5f32;
let mut y = x; // Notice the "mut" keyword here
let mut i : i32 = unsafe { mem::transmute(y) }; // evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
y = unsafe { mem::transmute(i) };
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed

y
}

What do you think of how the code looks in Rust? (Note that mem::transmute doesn't convert the value; it effectively does nothing, just like the weird typecast of the C version, but no pointers are needed here.)

Edit: Made one correction, should be transmuting i, not y, for the second transmute.
Post edited December 28, 2015 by dtgreene
Well, once I fixed a problem in the code of a colleague.
He had two nested loops, and only two levels deep would he check to see if something needed to be done. If not, he was looping for no reason whatsoever, as the condition had nothing to do with the loops (he was checking the same thing over and over and over again).

It took me a while to realize that yes, this was as silly as it seemed.
Once I placed the loops inside the condition (i.e., only loop if necessary), the code finished 15 times faster, providing the same result.
I mentioned SaGa 1 before, but let me mention another ridiculous bug in the game.

I used a cheat to play as the final boss (though this bug doesn't require a cheat to trigger) and recruited 3 human females. I encountered a REDBULL and used REPENT (one of the final boss's attacks) to confuse it. The enemy then attacked, and while unable to hurt the final boss, the same attack did 20+ damage to the humans. Yes, one basic physical attack (probably HORN) hit the entire party at once. One more attack and I would have had 3 dead party members.

(Of note, getting an attack that causes confusion is possible without cheats with an Esper (Mutant) and the right RNG state, or with a Monster if you look up the table of what meat yields which monster.)

The worst part of this is that the WonderSwan Color version of this game *does not fix this bug*. Really? They really refused to fix a bug that makes several abilities worse than useless?

Playing as the final boss would, of course, make the game easy, as the only threat would be a certain enemy that uses SAW against you. (As I've mentioned before, there is a well-known bug that makes that attack work only when it isn't supposed to.)
Windows ME.
Most.broken.useless.OS.ever.invented.
The security on the system was so bad that programs that you did not accepted to be installed would be installed on the system and so many crashes.
You thought the Vista was bad? You haven't seen this,no ladies and gents. This was a class of it's own,it would BSoD on the start up screen!
Windows 3.1 is freaking Windows 7 or XP 3 compared to this OS.
avatar
dtgreene: It's interesting to note that the language Rust has an explicit way to do what he is doing. In Rust, the code would look something like this:

fn Q_rsqrt(number: f32) -> f32 {
let threehalves = 1.5; //const is the default in Rust

let x2 = number * 0.5f32;
let mut y = x; // Notice the "mut" keyword here
let mut i : i32 = unsafe { mem::transmute(y) }; // evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
y = unsafe { mem::transmute(y) };
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed

y
}

What do you think of how the code looks in Rust? (Note that mem::transmute doesn't convert the value; it effectively does nothing, just like the weird typecast of the C version, but no pointers are needed here.)
I think it looks exactly the same, only with a slightly different (and worse) syntax ;-)
avatar
dtgreene: It's interesting to note that the language Rust has an explicit way to do what he is doing. In Rust, the code would look something like this:

fn Q_rsqrt(number: f32) -> f32 {
let threehalves = 1.5; //const is the default in Rust

let x2 = number * 0.5f32;
let mut y = x; // Notice the "mut" keyword here
let mut i : i32 = unsafe { mem::transmute(y) }; // evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
y = unsafe { mem::transmute(y) };
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed

y
}

What do you think of how the code looks in Rust? (Note that mem::transmute doesn't convert the value; it effectively does nothing, just like the weird typecast of the C version, but no pointers are needed here.)
avatar
Wishbone: I think it looks exactly the same, only with a slightly different (and worse) syntax ;-)
What do you think of the use of mem::transmute in place of an ugly looking typecast?

(Of note, I didn't actually try compiling this code, so it could have a mistake. Actually, it does have a mstake in it: The second mem::transmute should be transmuting i actually.)
avatar
Wishbone: I think it looks exactly the same, only with a slightly different (and worse) syntax ;-)
avatar
dtgreene: What do you think of the use of mem::transmute in place of an ugly looking typecast?

(Of note, I didn't actually try compiling this code, so it could have a mistake. Actually, it does have a mstake in it: The second mem::transmute should be transmuting i actually.)
I think the mem::transmute is worse, because with the typecast you can see explicitly what is being done. The mem:transmute does the typecast implicitly, which makes it more difficult to understand.

And yes, I noticed you'd written y in place of i at the end, but I didn't think it was important to the discussion.
Well, most crap eventually lands here
avatar
Wishbone: Then of course there's code that's not bad as such, in fact it's so good it's almost magical, but quite unintuitive and extremely difficult to understand. Example: The fast inverse square root algorithm.

For those unfamiliar with it, calculating the inverse square root of a floating point number is an important part of lighting and reflection calculations used in 3D graphics. As such, it is quite important that this can be done very very quickly. The algorithm has been wrongly attributed to John Carmack, because is was used in Quake III Arena, but Carmack only used it, he didn't invent it.

For your enjoyment, here is the code of the function used in Quake III Arena:

float Q_rsqrt( float number )
{
long i;
float x2, y;
const float threehalfs = 1.5F;

x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed

return y;
}
avatar
Wishbone: Edit:
The GOG forum does not like whitespaces. I really wish we could use code tags here. For a properly formatted version of the code, see the Wikipedia page linked at the top of this post.
It occurred to me that the code, as written, isn't guaranteed to work in modern compilers. Thing is, under strict aliasing, the compiler is allowed to assume that pointers to different types can't point to the same data. Hence, in the first assignment to i, the compiler is allowed to say "this long pointer can't possibly point to the same memory location containing the float y", and therefore determine that the behavior is undefined. As a result, the compiler can legally optimize out the assignment to i. The assignment of y later on is similarly undefined. Therefore, the compiler can determine that the variable i is not used in the function and optimize it out. This results in the function giving less accurate results than it should have.

Fortunately, in gcc you can add -fno-strict-aliasing to the command line to disable this optimization.

It's worth noting that Linus Torvalds (the maintainer and original author of the Linux kernel, as well as the original author of git) thinks the strict aliasing rule is stupid (see
http://www.mail-archive.com/linux-btrfs@vger.kernel.org/msg01647.html), and as a result, the kernel is compiled with -fno-strict-aliasing. (Adding that switch would probably be a good idea if you are compiling Quake 3 Arena with a modern version of gcc.)

Edit: It's worth noting that this will only show up if optimizations are turned on (-O2). Disable optimization and suddenly the calculations are more accurate again. It sounds like this could be a *nasty* bug to try and fix, especially if you aren't aware of the strict aliasing rule.
Post edited December 29, 2015 by dtgreene
Someone tried to give me a code for Bad Rats once.