Category Archives: Coding - Page 2

C++ Progress Counter

Much of the code I write these days is for numerical methods used in physics. What I often need is some output that tells me how long a program has come in for instance a Monte Carlo integration. I had to look a bit around on the net as a regular cout command doesn’t seem to work within a loop, and I want the progress counter to overwrite the same line as it progresses. Here is the solution:

j=0;
cout << "Progress: 0%";
for(i=0; i<iMax; i++) {
  //Algorithm here..

  if(i%(iMax/100)==0) {
    j++;
    cout << "\rProgress: " << j << "%";
    fflush(stdout);
  }
}
cout << endl;

Key elements here are the “\r” to return the cursor to the beginning of the line (for the overwrite) and “fflush(stdout)” to flush the buffer, something which isn’t done automatically if you do not have an endl.

How to stop the damn commentspam!

captchaI got fed up with the comment spam I get on my blog. I googled a bit and found a few solutions, but ended up with a plugin that adds CAPTCHA to the comment form.This little gem can be downloaded from here.

It is fun to observe the spammer-scripts bang their virtual heads against this simple little tool. I can observe all the spam attempts in my website log. :)

LaTeX Margins

Was writing a formula sheet for my quantum mechanics exam today, and needed to maximize usage of my available space on a piece of A4 paper. So I googled some solutions to get LaTeX to do just that. In addition to using the /tiny command to make small text of course.

Firstly I needed to minimize the margins, so I added this in the header:

\setlength\topmargin{-3cm}
\setlength\textheight{27cm}
\setlength\textwidth{19cm}
\setlength\oddsidemargin{-1.5cm}
\setlength\evensidemargin{-1.5cm}

Then I needed to split the page into 3 columns so I wouldn’t waste too much space at each lineshift:

\usepackage{multicol}

\begin{document}
\tiny
\begin{multicols}{3}
...text...
\end{multicols}

And last but not least, get rid of that annoying huge indent that the align environment has.

\documentclass[fleqn]{article}

\usepackage{amsmath}

\makeatletter
\setlength\@mathmargin{0.2cm}
\makeatother

Meh, WordPress will do…

After spending most the afternoon coding php scripts for a new website, I decided to just stick with WordPress afterall. There are many things I don’t like about it, but I managed to find plugins for the stuff I was missing. Not to mention the animated Tag Cloud I saw the other day. Looks fancy, just need more, uhm, tags… :)

The current theme is fine too, spent a lot of time yesterday cleaning it up. It had several bugs. I also modified a lot of the code. I may just replace the banner (have a few made) and change the colors too, at which point there won’t be much left of the original theme. I have kept the link to it on the footer tho, but removed the ads.

Tweaking is half the fun of a website!