Monday, August 17, 2009

C++ Constant for PI

Where's the definition for pi? Doesn't C++ provide a constant? What about C?

The C++ standard doesn't provide a value for pi but it's simple to define yourself:

#include <iostream>
#include <cmath> // M_PI is not standard

using namespace std;

class MathConst {
public:
static const long double PI;
};

const long double MathConst::PI = acos((long double) -1);

int main() {
cout.precision(100);
cout << "PI ~ " << MathConst::PI << endl;
}
Output:

$ g++ MathConst.cpp -o pi
$ ./pi
PI ~ 3.14159265358979323851280895940618620443274267017841339111328125
That is accurate to 18 decimal places.

Here's a definition in C:

#include <math.h>
#include <stdio.h>

double pi() {
const double pi = acos((double) - 1);
return pi;
}

int main() {
printf("%.20f\n", pi());
return 0;
}

Wednesday, August 5, 2009

Postgresql: Indexes on Foreign Keys

This query identifies foreign keys that are potentially missing indexes (Postgresql does not create indexes on foreign keys automatically).
/*
  Look for foreign key constraints that are missing indexes on the
  referencing table.

  Orders results by the size of the referencing table, largest first,
  on the assumption that, all else being equal, they are the most likely
  to benefit from the addition of indexes.

  This is only meant as a starting point, and isn't perfect.
  It's possible, for example, that it will report a missing index
  when in fact one is available. e.g., it won't realize that an index on
  (f1, f2) could be used with a fk on (f1). However, it will recognize
  that an index on (f1, f2) can be used with a fk on (f2, f1).

  Usage: psql -q dbname -f pg-find-missing-fk-indexes.sql
*/

CREATE FUNCTION pg_temp.sortarray(int2[]) returns int2[] as '
  SELECT ARRAY(
      SELECT $1[i]
        FROM generate_series(array_lower($1, 1), array_upper($1, 1)) i
    ORDER BY 1
  )
' language sql;

  SELECT conrelid::regclass
         ,conname
         ,reltuples::bigint
    FROM pg_constraint
         JOIN pg_class ON (conrelid = pg_class.oid)
   WHERE contype = 'f'
         AND NOT EXISTS (
           SELECT 1
             FROM pg_index
            WHERE indrelid = conrelid
                  AND pg_temp.sortarray(conkey) = pg_temp.sortarray(indkey)
         )
ORDER BY reltuples DESC
;

Saturday, August 1, 2009

Linux: Download YouTube Videos

Install the youtube-dl script. With Ubuntu:

sudo apt-get install youtube-dl
Next, find the YouTube video you want to download and pass it to the script:

youtube-dl -b -t "http://www.youtube.com/watch?v=aEXFUbSbg1I"
Command-line options:

Usage: youtube-dl [options] video_url

Options:
-h, --help print this help text and exit
-v, --version print program version and exit
-u USERNAME, --username=USERNAME
account username
-p PASSWORD, --password=PASSWORD
account password
-o FILE, --output=FILE
output video file name
-q, --quiet activates quiet mode
-s, --simulate do not download video
-t, --title use title in file name
-l, --literal use literal title in file name
-n, --netrc use .netrc authentication data
-g, --get-url print final video URL only
-2, --title-too used with -g, print title too
-f FORMAT, --format=FORMAT
append &fmt=FORMAT to the URL
-b, --best-quality alias for -f 18
See also: youtube-dl Homepage