Wikipedia:Reference desk/Archives/Computing/2007 November 1

From Wikipedia, the free encyclopedia

Computing desk
< October 31 << Oct | November | Dec >> November 2 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is an archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


Contents

[edit] November 1

[edit] Movie Freezes with Laptop Movement

I've got a pretty new, really nice Thinkpad T60p widescreen that doesn't seem to have any major problems (any more.) It's super fast. It doesn't freeze or anything. The only problem I've been noticing lately, and an annoying one, is that when I'm playing movies (in any player - VLC, WMP, Realplayer) if I agitate the computer more than a bit (pick it up, lift part of it up more than about 5 degrees off the ground), the video (and sound) will temporarily stop, and then restart a couple seconds later. This is annoying, because I like to watch movies in bed with my laptop on my chest, and if I shuffle around any, or reach to get something, or scoot back a bit, the videos stops for a bit and skips a couple seconds forward. What's the deal with this? I haven't been treating my computer particularly roughly. Could it be something I could correct, or should I have this checked by a professional, or do I have to send it back to Lenovo to get something physical changed? Thanks so much, 140.247.40.254 04:45, 1 November 2007 (UTC) Oh yeah. This is for both DVDs and downloaded videos. And one time I was showing a DVD (possibly a bit scratched) to a couple people, and it would routinely stop for 5 or so seconds every 1-2 minutes, even when it wasn't moved at all. Don't know if that's related. Any suggestions? Is this a videocard thing? Has something been banged up inside the computer? 140.247.40.254 04:47, 1 November 2007 (UTC)

Is it a DVD? If it's a DVD you're jostling the reader, which depends on some stability to work properly -- that's probably why it's freezing. If it's not a DVD, I have no clue. Gscshoyru 04:48, 1 November 2007 (UTC)

This may sound stupid but I will ask it anyways.

You mentioned the word "lately". Did you not have this problem under the *exact* circumstances before? Can you recreate the skipping situation at will? Do you have a video clip on your computer (preferably on your hard disk drive) that you can play without using your optical disk drive?

Thank you for answering. I hope we will come up with some idea soon. Regards, --Kushalt 05:42, 1 November 2007 (UTC)

Yeah. Right before I posted I tried viewing a couple videos on my hard drive (2 different ones) in several different players, and moving the computer around froze them consistently. I think this has been an issue for about as long as I have been watching movies on this (a couple months. I've had the computer since the beginning of this school year.) Also, I think it's tangentially important that music does NOT similarly freeze when I move the computer, although when the video freezes, its soundtrack does as well. I can't figure out what this could be. Which isn't to say that if I could I would have any idea what to do about it. 140.247.40.254 05:51, 1 November 2007 (UTC)

I've got an idea. Newer Thinkpads (especially the highend ones) sense movement and cease movement of the hard drive's platter and some other things to avoid any possible damages from falling, like off a desk. Perhaps its sensor is believing incorrectly you are dropping it. -Wooty [Woot?] [Spam! Spam! Wonderful spam!] 08:36, 1 November 2007 (UTC)

You've got a point, Wooty! Does anyone know if Lenovo has said anything about it yet? Or is it an isolated incicent? --Kushalt 12:55, 1 November 2007 (UTC) If this is not an isolated case, Lenovo is probably all ears about it because the motion sensor is one of the most touted features on a thinkPad. To the OP, please try notifying the manufacturer about your situation. Thanks, --Kushalt 13:46, 1 November 2007 (UTC)

This isn't a problem, this is a feature. It's not streaming off the DVD to the screen, it goes though a sizable buffer on the hard drive. The Active Protection System locks the read head when there's jostling to keep it from being damaged, so the hard drive is unavailable and requests for file I/O aren't filled until everything is unlocked a second or so later. I highly encourage you to leave this feature on, it'll save your hard drive years of life, but you can always go to Control Panel -> Active Protection System and turn down the sensitivity. You can use the preview pane there to see the physical orientation of your laptop and whether your current settings are causing the hard drive to lock in different positions. --ffroth 18:00, 1 November 2007 (UTC)

Thanks for all the diagnoses, but does anybody have any suggestions that wont take "years of life" off of my hard drive? Is the sensor over sensitive? Can I do anything about this? Should I ask some computer guy to help me do something about it? Or is the problem unfixable unless I take the aforementioned drastic actions? I don't know all that much about computers, so any really complex instructions might go over my head, but really anything's appreciated. Thanks so much for all your help. 140.247.41.134 02:41, 2 November 2007 (UTC)

Yeah I just said how to turn down the sensitivity in the above comment ^^^^^ --ffroth 05:46, 4 November 2007 (UTC)

[edit] C++

Are there any non dot net libraries for making simple get/post requests in C++. --Chris  G  09:17, 1 November 2007 (UTC)

[edit] Tutorials on making gnome-panel applets?

I want to make an applet to get my graphics card temperature and display it on my gnome-panel (and also as a little exercise). However I can't really find any tutorials on Google. Does anyone know any good tutorials for this, preferably for python? --antilivedT | C | G 10:04, 1 November 2007 (UTC)

  • On my system some simple examples live under /usr/share/doc/python-gnome2-desktop/examples/applet/ --Sean 10:56, 1 November 2007 (UTC)

[edit] Python newbie question

Also, how do you assign the stdout of a programme executed through os.system() into a variable? var = os.system() only assigns the exit state of the programme. --antilivedT | C | G 10:09, 1 November 2007 (UTC)

Try os.popen2() -- JSBillings 10:44, 1 November 2007 (UTC)
How do you use that? Documentations say it creates a file object but how do you read the stdout? This doesn't work (just using ls as an example)
>>> ls = os.popen2('ls')
>>> ls.child_stdout
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'child_stdout'
>>> ls.stdout
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'stdout'

I'm still quite new to python so maybe there's something very fundamental that I've done wrong? Thanks. --antilivedT | C | G 10:33, 2 November 2007 (UTC)

  • If you do this:
>>> import os
>>> os.popen2('ls')
    (<open file '<fdopen>', mode 'w' at 0xb7d09260>,
     <open file '<fdopen>', mode 'r' at 0xb7d09ec0>)
you can see that os.popen2() returns a tuple containing two file objects: the first one you can write to ('w'), and the second you can read from ('r'). We can keep references to those file objects like this:
>>> (to_child, from_child) = os.popen2('ls')
See the doco for file objects for how to access them. The easiest way is as an iterator:
>>> for line in from_child:
...     print line
--Sean 15:03, 2 November 2007 (UTC)
So there's nothing nice and simple like bash's
temp=$(nvclock -T | grep "GPU" | sed "s/[a-zA-Z :=>]//g")
and instead you have to go through the whole file object? This works and returns it as a float but do I have to somehow close the file object is well?
(to_child, from_child) = os.popen2('nvclock -T | grep "GPU" | sed "s/[a-zA-Z :=>]//g"')
temp = float(from_child.read())
--antilivedT | C | G 00:05, 3 November 2007 (UTC)
  • No, when the file object goes out of scope, it will be cleaned up automagically. --Sean 02:06, 3 November 2007 (UTC)
OK thanks. --antilivedT | C | G 02:11, 3 November 2007 (UTC)
  • FYI, here it is without grep or sed. I don't have nvclock, so I've faked it. --Sean 20:52, 3 November 2007 (UTC)
import os
import re
(to_child, from_child) = os.popen2('echo first line; echo GPU temp: 98.6; echo last line')
for line in from_child:
   match = re.match(r'GPU.*?(\d+\.\d+)', line)
   if match:
       temp = float(match.group(1))
       print "Temperature is %f" % temp
Is there any way to only select a specific line of the output so the whole grepping GPU step can be removed? The output of nvclock -T goes something like
robin@ubuntu:~$ nvclock -T
nVidia Geforce 6600/GT
=> GPU temperature: 67C
=> Board temperature: 47C
--antilivedT | C | G 23:40, 3 November 2007 (UTC)
  • The above does exactly that, since it needs "GPU" and then a float. I was trying to demonstrate that with the three-line echo output. The nvclock output is a little different to what I thought (no decimal). If the line is as you marked it, this will do for the regular expression:
r'GPU temperature:\s*(\d+)C'
If it might have a decimal, this will work:
r'GPU temperature:\s*(\d+(\.\d+)?)C'
--Sean 00:42, 4 November 2007 (UTC)

[edit] Variables in Ruby

Let's say I declare:

myVar = 3

Then, later, by accident I declare:

myVer = 6

In a familiar language like C, the compiler would basically say "myVer is undeclared!" and I would notice the error. But in Ruby, wouldn't it declare a new variable myVer and leave myVar equal to 3? This is not a criticism, I'm just curious as to whether this can be a serious problem or something that you just get used to and learn to check when a bug occurs.

Consequently, are there any free / open-source Ruby IDEs that provide lists of variables / methods within your code? —Preceding unsigned comment added by 91.84.143.82 (talk) 11:40, 1 November 2007 (UTC)

I don't know Ruby, but in Perl the equivalent problem is solved by enabling "strict mode", which forces you to declare your variables the first time you use them, by prefixing them with the keywords "my" (for local variables) or "our" (for global ones). The compiler will also (optionally) warn you if it sees two such declarations for the same variable within the same scope. Of course, you can still get similar problems in strict mode e.g. with hash keys — you set $someHash{"example"} and then try to access $someHash{"exmple"} or something like that — since the compiler won't check those, but it still reduces the risk significantly. —Ilmari Karonen (talk) 13:08, 1 November 2007 (UTC)
Alas, there is no ruby equivalent of use strict. From looking at some discussions on the topic, it appears to be a misunderstanding of the benefits of use strict, thinking that it's simply a matter of providing a guard against inadvertently creating global variables. Ruby provides some minimal protection in that a variable can't be used without being set, so you're protected against doing something like
myVar = 3
myOtherVar = myVer
But you're not protected against a scenario where the wrong variable is changed. I was a bit disappointed to discover this as I had hoped that Ruby might be a good successor to Perl, but this is the sort of thing which shows that would-be Perl successors miss out on the reasons behind some of the design decisions in Perl (PHP does Ruby one worse by not only not having use strict, but also not having arrays). Donald Hosek 16:43, 1 November 2007 (UTC)
PHP does too have arrays. They're even more flexible than Ruby's, so it does Ruby one better! --ffroth 20:33, 1 November 2007 (UTC)

[edit] Identified by Email

There are several parts to this question. I appreciate your patience in helping me understand all that follows:

1.If I send an email on Wikipedia using the email button on someone's user pages, how am I identified on that email? Does the person to whom I send the email get my user name and the return email address or just my user name and a "reply" function that does not reveal my email address?
2.In general terms, is there any simple way a person receiving an email with a clear return email address can trace that address back to a real person? (I am assuming here that the return email address is not the person's name already.)
3. Can ArbCom trace an email to a real person who is using his own computer to send an email to the commitee, absent anything like a court order or a threat or something requiring third-party intervention? Thanks for your help. Bielle 16:12, 1 November 2007 (UTC)
See Wikipedia:Emailing users. Your username and email are sent to the recipient. --— Gadget850 (Ed) talk - 18:21, 1 November 2007 (UTC)
That's the first question answered. Thank you, — Gadget850 (Ed) talk - . Bielle 18:56, 1 November 2007 (UTC)
If you want to remain anonymous, just send it from a throwaway account, or use "Email this user" from a sockpuppet you've only used at your library or something. --ffroth 20:32, 1 November 2007 (UTC)
Thanks, ffroth but I am not looking to remain anonymous. I just want to know what it is possible to do. Bielle 02:22, 2 November 2007 (UTC)
Generally speaking You can see the IP address in an email header. From this you can get the ISP. They obviously know the address and the name of the person who pays the bill, but they wouldn't tell the arbitration committee! Theresa Knott | The otter sank 02:43, 2 November 2007 (UTC)

[edit] Hello and welcome....

I think that this question is "out there" but here goes anyways:

Is there a program that will give me the notes from an MP3 file OR a MIDI file in simple-to-understand format? I'm not a computer illerate, I know my way around my hardrive but let's say for example:

I take, oh, Chopin's: Prelude Op. 28/15 in D flat Major better known as "Raindrop" and download it form this source-

http://www.chopinmusic.net/includes/media/midi/28-15-prelude.mid

now, using a MIDI format like WIDI or a similar program that can take MP3 or MIDI and play or download and etc... I could make it into a saved MIDI file with my control to write and what-not. Is there a program that would change the MIDI format into readable notes like "D#" or "A#" or well...you get it, on my computer? It works on my digital piano, but just for the sake of simplicity of keeping it in one area...?

Thanks for your time!

-ECH3LON 16:43, 1 November 2007 (UTC)

How about Finale Notepad. It's been a while since I used it, but it says it will import and export MIDI. By easy-to-understand format, I take it you mean actual musical notation. --Bennybp 16:52, 1 November 2007 (UTC)

BTW, it should just be one of those "click here to download" things, not the "sign up and recieve..." kinds of files, just plain OTC programs...like Wiki's link to download .ogg files. ECH3LON 16:58, 1 November 2007 (UTC)


You will never get a usable score out of an MP3 file. Especially not one with more than one instrument. Mid files are another issue though. It is often possible to get a usable score out of a .Mid file. I sometimes use Noteworthy Composer for this. It won't be perfect, and You may have to tweak the settings a little before you get a score that's as human-readable as you would like, but many MIDs can be converted into passably usable notation this way.
Of course, As for your specific example, The site you linked to also has sheet music. 69.95.50.15 17:04, 1 November 2007 (UTC)

[edit] help me find a venue to buy socket 478 motherboard please!?

Hi wiki's, my motherboard recently went dead and I would like to buy a new one of the same socket as opposed to upgrading the socket type which would require me to then also upgrade my cpu, ram, and video card. I have a socket 478 gigabyte 8IP900 motherbaord. I checked fry's and they don't carry them, I checked newegg and they only have 3 that are all horrible, and I searched my local craigslist to no avail....please help me find a good store/retailer/personal reference that can sell me a decent socket 478 motherboard. Thank you in advance. ````Brent —Preceding unsigned comment added by 64.238.49.65 (talk) 16:54, 1 November 2007 (UTC)

Try PriceWatch or PriceGrabber. --— Gadget850 (Ed) talk - 17:32, 1 November 2007 (UTC)
And be sure to check ResellerRatings to see if the store you're looking at is reputable or not. --Carnildo 22:23, 1 November 2007 (UTC)

[edit] Classic code & magic constants

Can anyone help me understand what's going on in these 2 pieces of classic code? I understand a lot of each one but I lack the fitting-together that I know would just make my jaw drop :) User:SteveBaker, I'm looking at you: you've probably had experience with at least the first one.

float InvSqrt (float x){
   float xhalf = 0.5f*x;
   int i = *(int*)&x;
   i = 0x5f3759df - (i>>1);
   x = *(float*)&i;
   x = x*(1.5f - xhalf*x*x);
   return x;
}

I realize there's a lot of magic going into that famous constant, but I'm more interested in figuring out how this works at all. Specifically, how does it preserve the decimals of the float through the int cast? I'm sure the question reflects some fundamental misundertanding but that's what I'm trying to root out.

The 2nd code is more obscure- it counts the total number of bits set in an integer v.

unsigned int v; // count bits set in this (32-bit value)
unsigned int c; // store the total here
static const int S[] = {1, 2, 4, 8, 16}; // Magic Binary Numbers
static const int B[5];
B[0] = 0x55555555; //01010101 01010101 01010101 01010101
B[1] = 0x33333333; //00110011 00110011 00110011 00110011
B[2] = 0x0F0F0F0F; //00001111 00001111 00001111 00001111
B[3] = 0x00FF00FF; //00000000 11111111 00000000 11111111
B[4] = 0x0000FFFF; //00000000 00000000 11111111 11111111

c = v - ((v >> 1) & B[0]);
c = ((c >> S[1]) & B[1]) + (c & B[1]);
c = ((c >> S[2]) + c) & B[2];
c = ((c >> S[3]) + c) & B[3];
c = ((c >> S[4]) + c) & B[4];

The pattern in B is obvious but I can't wrap my mind around their magic :( --ffroth 17:54, 1 November 2007 (UTC)

I also don't fully understand the magic behind that magic number, so I'll leave that explanation to someone else, but it's important to note that what you have in example one is not an int cast. It's a int* cast being performed on an float*. The data is not modified by the cast itself. The cast only exists so that the next line can perform integer operations on data in memory that was intended to be interpreted as a float. 69.95.50.15 18:06, 1 November 2007 (UTC)
Oh ok, so it just takes the first part of the float (the mantissa I assume) as an int and works its magic on that, then casts back to float* to pick up the exponent again? --ffroth 18:16, 1 November 2007 (UTC)
No, I don't think so. An Int and a Float should be the same size. Ultimately, both the float and the int are stored as (lets say) four bytes of undifferentiated binary data. The compiler keeps track of whether you're pretending that binary data is an int or pretending that data is a float. Casting the float* to an int* lets change what you're pretending, and treat the entire four bytes of the float as though it were the entire four bytes of an int. I hope I'm explaining this clearly. Ultimately, this function (in example #1) is a trick involving the way floats are being represented as binary. 69.95.50.15 18:42, 1 November 2007 (UTC)
Found via google : A discussion of this clever hack. 69.95.50.15 19:05, 1 November 2007 (UTC)
Well, the second one is an ugly-ized version of the one on my web site! My version is MUCH prettier and very easy to remember when you want to impress chicks at a party:
  n = (n & 0x55555555) + ((n & 0xaaaaaaaa) >> 1);
  n = (n & 0x33333333) + ((n & 0xcccccccc) >> 2);
  n = (n & 0x0f0f0f0f) + ((n & 0xf0f0f0f0) >> 4);
  n = (n & 0x00ff00ff) + ((n & 0xff00ff00) >> 8);
  n = (n & 0x0000ffff) + ((n & 0xffff0000) >> 16);
(Sadly, far fewer women than you'd hope are actually impressed by this...especially at parties!)
The way this works is that the first statement adds every odd numbered bit to it's neighbour (an even numbered bit) and stores each result into a little 2 bit number. It's doing all 16 one-bit additions in parallel! Each of those 16 two bit numbers are packed into the space where the odd numbered bit and it's neighbour used to be. The second statement adds every alternate 2 bit number to it's neighbouring 2 bit number and stores the result as a 4 bit number - and now there are 8 four bit numbers packed together...the next adds 4 bit numbers and packs them into 8 bits, then the next adds 8 bit numbers and stores them in 16 bits...and the last statement of all adds up the two 16 bit numbers to give you the answer.
This is unspeakably cool - but sadly, I can't claim to have invented it (although - if you happen to be a female at a party, I may accidentally make it SEEM as if I did). SteveBaker 19:01, 1 November 2007 (UTC)
Gah, stop taunting me! I don't get it at all. What's with the shifts? You're not adding every odd numbered bit to its neighbor, you're adding every odd numbered bit to its neighbor divided in half(and fourths, and sixteenths, 256ths, and 65536ths)! (which doesn't even make sense- why would you shift a 16 bit number 16 bits?) Also .. hold on let me think I think I'm starting to get it O_O --ffroth 19:30, 1 November 2007 (UTC)
We have an article on this: Hamming weight --Spoon! 19:48, 1 November 2007 (UTC)
Why does your code have n ANDed with separate numbers in each line? Not that I understand it at all yet but shouldn't it be more like:
  n = (n & 0x55555555) + (n & (0x55555555 << 1));
  n = (n & 0x33333333) + (n & (0x33333333 << 2));
  n = (n & 0x0f0f0f0f) + (n & (0x0f0f0f0f << 4));
  n = (n & 0x00ff00ff) + (n & (0x00ff00ff << 8));
  n = (n & 0x0000ffff) + (n & (0x0000ffff << 16));

GaHHH!!! --ffroth 20:01, 1 November 2007 (UTC)
NO!!!! The version above is *NOT* the same as the one I provided and this version doesn't work!!!! SteveBaker 21:01, 1 November 2007 (UTC)
Wait, with this method you always end up with the starting "v". So it's obvious you actually have to shift one of the addends but why it actually works is still a mystery --ffroth 20:10, 1 November 2007 (UTC)
Aha, turns out yours is even an ugly-ized version of one that actually makes sense. How about this?
  n = (n & 0x55555555) + ((n >> 1) & 0x55555555);
  n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
  n = (n & 0x0f0f0f0f) + ((n >> 4) & 0x0f0f0f0f);
  n = (n & 0x00ff00ff) + ((n >> 8) & 0x00ff00ff);
  n = (n & 0x0000ffff) + ((n >> 16) & 0x0000ffff);
Now it's starting to come together >_< --ffroth 20:16, 1 November 2007 (UTC)
Yeah - that works. I don't see how it's easier to understand - but whatever helps your brain! SteveBaker 21:01, 1 November 2007 (UTC)
Your skill in bit twiddling: +1. Your brain not being mash: -2. I finally get it, thanks. That last bit helped a lot, I would never have gotten it out of the original code, and probably not out of Steves --ffroth 20:29, 1 November 2007 (UTC)
Bit twiddling skills are important. The original version was ugly and inefficient. The five-line version is better and faster and much easier to grok.
So - what about this one?
  n = ((n >>  1) & 0x55555555) | ((n <<  1) & 0xaaaaaaaa) ;
  n = ((n >>  2) & 0x33333333) | ((n <<  2) & 0xcccccccc) ;
  n = ((n >>  4) & 0x0f0f0f0f) | ((n <<  4) & 0xf0f0f0f0) ;
  n = ((n >>  8) & 0x00ff00ff) | ((n <<  8) & 0xff00ff00) ;
  n = ((n >> 16) & 0x0000ffff) | ((n << 16) & 0xffff0000) ;
(It reverses the order of the bits in 'n' efficiently.) SteveBaker 21:01, 1 November 2007 (UTC)
In some programming languages (old-fashioned K&R C for example), you can swap the values of two variables in one statement like this:
 x ^= y ^= x ^= y ; 
('^' means 'XOR')...but it's not legal in more recent versions of C++ and ANSI C because there is now a ban on changing the value of a variable more than once in a single statement (bah!). I'm not sure about other languages.
SteveBaker 21:01, 1 November 2007 (UTC)
Yah, it's still possible with multiple statements
x^=y
y^=x
x^=y
We even have an article on it --ffroth 21:48, 1 November 2007 (UTC)
Oh - yes, sure - but it's not as much fun. In reality, both your way and mine will compile down to the same machine-code - and an old-fashioned "tmp=x;x=y;y=tmp;" is likely to be faster...but if you've ever had to program on a computer with only 32 bytes of RAM, you'll know where I'm coming from! SteveBaker 23:07, 1 November 2007 (UTC)
Isn't there some kind of added instruction set or something to IA32 that lets you actually "rename" the registers on a hardware level in supported processors so swapping values is just a matter of activating a different set of control lines? Any idea what I'm thinking of? --ffroth 04:49, 2 November 2007 (UTC)
On Pentium M and Core processors the FXCH instruction is implemented by register renaming and doesn't tie up any execution units. FXCH isn't a new instruction, though (it's been around since the 8087). -- BenRG 14:50, 2 November 2007 (UTC)
  • Nerds who have enjoyed this thread should waddle quickly to the library and check out the magnificent Hacker's Delight, by Henry Warren. -- Sean/TotoBaggins —Preceding unsigned comment added by 198.85.228.129 (talk) 21:22, 1 November 2007 (UTC)
Gulp. Heavy breathing. You realize I'm going to have to buy this don't you? It'll ruin all of my plans to buy that $200 pc. --ffroth 21:48, 1 November 2007 (UTC)
This made me lawl hard. Barnes and Noble has it categorized as "Home > Books > Terrorism". --ffroth 20:46, 2 November 2007 (UTC)
I never did get around to explaining the first one:
 float InvSqrt (float x){
    float xhalf = 0.5f*x;     // Line 1
    int i = *(int*)&x;        // Line 2
    i = 0x5f3759df - (i>>1);  // Line 3
    x = *(float*)&i;          // Line 4
    x = x*(1.5f - xhalf*x*x); // Line 5
    return x;
 }
The easiest way to understand this is to look at line 5 first. The traditional way to do a 1/sqrt(x) is to use the Newton–Raphson method - in which you take a guess at the answer - then correct that guess by repeatedly calculating error terms and retrying. Line 5 is ONE iteration of that method. So the 'x' that comes out of line 4 is a guess for 1/sqrt() and line 5 fixes it up a little bit. Line 1 is obvious - and it's really just there to keep the original X lying around ready for step 5. Lines 2 and 4 are there merely to allow line 3 to operate on the individual bits of the floating point representation of the original number. So what the all-important line 3 does is to generate a guess for 1/sqrt(x) that line 5 will merely correct somewhat. So what the heck does line 3 do?
The layout of a floating point number
The layout of a floating point number
Think of line 3 as working in logarithms to base 2. If you look at the diagram, you'll see that the integer version of a POSITIVE IEEE float (you can't take 1/sqrt of negative numbers - so it's always positive) has the exponent of the number (which in IEEE math is at the top end of the 32 bit word) as a simple integer with the mantissa stuck in the lower order bits. So very roughly - the integer image of a positive IEEE float 'x' is approximately log2(x). If that were true then (i>>1) simply halves the exponent and the minus sign in front of -(i>>1) is negating the result of that. Which is like taking the reciprocal of the square root of the original number x-1/2. The bigger mystery is this correction factor 0x5F3759F. The high order bits of that are there to correct for the fact that IEEE floating point doesn't literally store the exponent - it stores the exponent with some positive constant added to it to avoid the need to have a sign bit for the exponent - so the '5F' part is a correction for that. All of that messing around gets you a guess for 1/sqrt(x) that is within a factor of two of the exact answer - which after the correction factor applied by one step of Newton-Raphson gives you a reasonably good answer.
However, the lower order bits of this weird constant 0x5F3759F seem like they shouldn't matter much - after all, the shifting and negating of the mantissa as if it were a logarithm (when it's not) is all screwed up anyway...so why aren't the low order bits just all zeroes or something? Well, I believe (although I don't know for sure) that those low order bits were arrived at by trying every possible floating point number with this algorithm and seeing which value could be added to the result to minimise the worst case errors. I don't think it was ever calculated - I think someone wrote a program to figure out the best compromise over the entire number range. It significiantly improves the guess for some numbers - and (predictably) worsens it for others - but by writing a program to figure out the best compromise, you get better precision for the areas of the number range that would have had particularly bad answers - and worse precision for numbers that gave better results. In most applications, that's a good thing.
So, there you go. It's essentially a calculation on the log-to-base-2 of the number to guess at 1/sqrt(x) - then one iteration of a well known successive approximation algorithm. It's not rocket science - and it only LOOKS like black magic.
SteveBaker 23:52, 1 November 2007 (UTC)
So easy once someone just explains it! I can't wait for my copy of Hacker's Delight to arrive (yes I ordered it -_- no, not from amazon) --ffroth 04:43, 2 November 2007 (UTC)
Yeah - but beware. This 5 line chunk of code takes 5 long paragraphs of hard-to-read explanation to even give a rough idea of what it does - that's never a good sign. On a modern PC, InvSqrt is about 40% faster than 1.0f/sqrtf(x) - but you'd have to be pretty desperate to want to use it because it's inaccurate by around 0.2% in the worst parts of its range. There aren't many applications where you need to do 1/sqrt for any significant percentage of your total CPU time and a 40% saving of that small percentage - at a cost of 0.2% error rates just isn't worth it.
The thing to avoid at all costs is the temptation to use 1.0/InvSqrt(x) instead of sqrtf(x) - and on modern CPU's, the latter is typically faster as well as being more accurate. SteveBaker 18:09, 3 November 2007 (UTC)
It never even entered my mind to use 1/InvSqrt- but for applications where you need an InvSqrt like for vectors, you'd use it (and you're desperate for processing power because you're developing for 1990s hardware) --ffroth 05:42, 4 November 2007 (UTC)
But hm (if anyone's still reading this).. how about x*InvSqrt(x)? Still slower than sqrtf? Doubtlessly less accurate, but faster? --ffroth 23:23, 9 November 2007 (UTC)
Dweller's thread of the week. It's an 'out of the box' idea.
Dweller's thread of the week. It's an 'out of the box' idea.

Better late thanOK, better very late than never, but this thread (which is fairly impenetrable to me) is apparently very helpful, but it wins this last week's Ref Desk thread of the week award, mostly because of SteveBaker's comment "(Sadly, far fewer women than you'd hope are actually impressed by this...especially at parties!)". Anyway, well done, or should I say n = (n & 0x0f0f0f0f) + ((n >> 4) & 0x0f0f0f0f); --Dweller 14:18, 6 November 2007 (UTC)

Don't worry too much, Dweller. Even John Carmack didn't know what exactly he was doing:
 i  = * ( long * ) &y;  // evil floating point bit level hacking
 i  = 0x5f3759df - ( i >> 1 ); // what the fuck? 
-Wooty [Woot?] [Spam! Spam! Wonderful spam!] 10:46, 7 November 2007 (UTC)

[edit] NAT network bridging

I'm jealously eyeing this piece of crap to run as a server. I don't have an ethernet jack in my room (my laptop connects with wifi) so is there any daemon out there that can do this:

  1. I connect to the wireless network with my laptop.
  2. I connect my server box to my laptop with an ethernet crossover cable.
  3. The server box thinks it's connected to a router or something because it recieves network information and is able to connect. It's important that this is transparent to the linux box because I don't think I can expect any weird-setup support from gOS.
  4. My laptop translates (in the background) network requests from the ethernet jack to real network requests over wireless using NAT

So is there software out there that lets you do this? I've seen people pull off setups like connecting to the same network 4 times with different NICs for amazing bandwidth, so I figure there must be some kind of program that lets you mess with multiple network interfaces and forwarding --ffroth 18:36, 1 November 2007 (UTC)

And by NAT I guess I mean bridging, since I do want to be able to connect to the server from elsewhere within the (real) network --ffroth 18:43, 1 November 2007 (UTC)
If you're NATing, you're routing, not bridging. Software for this is common for linux I believe. There's probably something out there for windows too. Friday (talk) 18:46, 1 November 2007 (UTC)
Well yeah there's Internet Connection Sharing, but it's NAT-based and AFAIK there's no port forwarding option to get at the server from outside. I definitely want the server to use the laptop's network connection- not make a connection of its own, since the real network won't let me connect twice on the same user ID. That's NAT, right? So I need a port forwarding option, and something that can handle FTP's weirdness, neither of which ICS supports --ffroth 19:12, 1 November 2007 (UTC)
Oh wait, hold on ICS does support port forwarding. Verrry very promising, but I'd prefer to use something other than a "set up and forget" solution that actually affords a level of configurability --ffroth 19:19, 1 November 2007 (UTC)
Is there any NAT router software for windows? I see NAT32, but it costs money and doesn't work on vista anyway. --ffroth 21:24, 1 November 2007 (UTC)
Also, what category cable should I buy? Ethernet cables are dirt cheap from newegg, but the 7ft version only comes in cat6. It's backwards-compatible, right? Will my humble Intel Pro/1000 PL network card support cat-6? --ffroth 21:52, 1 November 2007 (UTC)
For much the same problem I use a ZyXEL G-470 ethernet-to-802.11 bridge. It bridges rather than NATing, and allows the ethernet devices behind it (a linux server, an antique media-extender, and an internet telephone box) to be on the network (and thus the internet) as if they were wired straight into the DSL box. It does WPA (which some similar products don't) and it's been absolutely as solid as a rock. -- Finlay McWalter | Talk 23:36, 1 November 2007 (UTC)
Why not buy a WiFi connection for your new server? It is pretty easy to get a WiFi expansion card (see here) or a WiFi USB adapter(see here) with prices starting from about $30 - just make sure you can get hold of the necessary Linux drivers for gOS (you might have to download these from the manufacturer's website). Astronaut 10:36, 2 November 2007 (UTC)
Because I can only connect one network interface to the school network at a time. Until you log in, every query to the network gateway is answered with a HTTP reply containing the HTML of the login page. After you use a browser to actually render that page and submit your login, the gateway functions normally. I don't think it'll let you log in 2 places at once. --ffroth 20:12, 2 November 2007 (UTC)
Yeah so login through your desktop, and use that as a NAT gateway for your laptop, that seems to make more sense if you want to use it as a server. YOu will have much more control with your linux box doing the routing. -- Diletante 21:02, 2 November 2007 (UTC)
Now that is a good idea. What would I use for this, iptables? --ffroth 00:01, 3 November 2007 (UTC)
Yeah. While you can compose the iptables rules yourself, to make things easy use a pre-made firewall script. Before I started using openwrt I used the projectfiles rc.firewall which you can find mirrored here, edit the rc.firewall script to suit your needs and make sure it runs at boot and you're good. I bet you can write a script in whatever language you are comfortable with to log in to the captive portal automatically too. -- Diletante 01:19, 3 November 2007 (UTC)
It only makes you log in once a week, so it's not enough of an issue to learn curl --ffroth 02:33, 3 November 2007 (UTC)

[edit] .NET question

As much as I hate it, my current job requires I work on .NET applications instead of Java Platform applications. Now, I have seen plenty of MSDN documentation of .NET technology, and they always give code samples both in VB.NET and C#. The difference between the code samples seems to be mostly syntactic, up to the point where one could write a parser to translate one language into another without even having access to the .NET library, or knowing how these languages are compiled. So what's the point in having two separate language implementations of the same library if they're this much alike? I understand the point of C#, but what I don't understand is why we need VB.NET if all it is is a more BASIC-looking way of writing C#. JIP | Talk 18:41, 1 November 2007 (UTC)

People who were already familiar with VB are seen as being less likely to be afraid of something called "VB.NET". Friday (talk) 18:43, 1 November 2007 (UTC)
Yeah, pretty much. A lot of (or most) people who use VB aren't 'programmers'. They use it to solve small problems for their business (or sometimes big problems, often with disasterous results, but that's another story...), but haven't really dabbled in programming beyond it. A lot of businesses use it like this. 130.179.28.118 20:26, 1 November 2007 (UTC)

VisualBasic.NET is seen by many as nothing more than a failed marketing ploy by Microsoft to fool people into thinking that they were not dropping all support for VisualBasic entirely, even though that's essentially what they did. (See e.g., [http://vb.mvps.org/vfred/Trust.asp VisualFred], Visual Fred, Visual Fred, Deprecation, Fear, uncertainty and doubt Dilbert). dr.ef.tymac 21:17, 3 November 2007 (UTC)

[edit] how can I...?

On a separate question: How (if i can) download videos from Youtube on to my computer (like a file)? I know that Google video has this quality but they don't have all of youtube's videos.

Any suggestions?

Thanks!

ECH3LON 19:59, 1 November 2007 (UTC)

If you're using Firefox, then there are a number of add-ons that do this, such as this one for example. Algebraist 20:24, 1 November 2007 (UTC)
Or unplug, which is made by some wikipedian guy here. It manages to get some videos that others don't, since it's clever about what it does, and actually tries to adapt when it hits a page it's not programmed to download from. 130.179.28.118 20:31, 1 November 2007 (UTC)
This is a great tool. The demo version works fine, and it saves it to MPEG format, not the .flv format that is a pain. --Omnipotence407 22:24, 1 November 2007 (UTC)
But that's another transcoding, losing even more quality off the hopelessly mangled youtube format --ffroth 20:18, 2 November 2007 (UTC)