Occasionally you need to check whether a float is a valid number. There are times, such as when the number is read in to a function as an argument, when you can’t assume that the check has already been done. This check can be done using the isnan(number) function from math.h. For example: #include <math.h> [...]
Archive for the ‘Software Development’ Category
Checking if a float is not a number (nan) in C / C++
Posted: 10th March 2010 by Tim in C, C++Tags: C, divide by zero, double, float, fraction, math, nan, not a number, zero
Highlighting Doxygen tags in Vim
Posted: 30th December 2009 by Tim in Software Development, Ubuntu, VimTags: configuration, doxygen, syntax, syntax highlighting, vim
Doxygen highlighting is set up by default on most Vim installations, but for some reason it’s disabled. There are two options for enabling it. First, it can be enabled globally. This means adding the parameters to the global Vim configuration. Note that you may need to be root for this to work (ie: sudo echo [...]
Getting avr32-linux-gcc compiler on linux
Posted: 14th December 2009 by Tim in Bash, C, UbuntuTags: atmel, avr32, avr32-linux-gcc, buildroot, compile, gcc, platform
If you’re trying to compile C programs for Linux on an AVR32 architecture, you’re going to have to get the avr32-linux-gcc cross compiler. Note that you can’t use the avr32-gcc compiler, as this compiler makes programs which do not run on an operating system (ie: they talk to the system directly), which will not run [...]
PQexecParams example – PostgreSQL query execution with parameters
Posted: 19th November 2009 by Tim in C, PostgreSQLTags: C, coding, exec, parameter, PostgreSQL, programming, query
The PostgreSQL documentation states that PQexecParams can be called like so: PGresult *PQexecParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char * const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat); There are two parts of this call which can be tricky to novice C and/or PostgreSQL users and aren’t explained [...]
SSH Login without a password
Posted: 17th November 2009 by Tim in Bash, UbuntuTags: key, keygen, passphrase, password, public, remote, rsa, secure, ssh
If you access the same computer through SSH on a regular basis, or want to access a machine through SSH in a script, then you don’t want to have to worry about passwords. Luckily, there is a way to grant SSH access without a password while remaining secure. For a quick and easy fix, download [...]
Millisecond timer in C / C++
Posted: 18th October 2009 by Tim in C, C++Tags: C, code, coding, header, include, program, programming, time, timer
If you’re looking for a timer with fairly good accuracy in C or C++, you can use the functions in time.h and sys/time.h to build a millisecond timer. This is useful for things like evaluating the execution time of a program, roughly accurate to the nearest microsecond. double get_time_ms() { struct timeval t; gettimeofday(&t, NULL); [...]
Converting latitude and longitude into mercator (UTM) X and Y coordinates
Posted: 6th October 2009 by Tim in C, C++Tags: C, degrees, global, lat, latitude, lon, longitude, mercator, pi, position, utm
The following calculations have been sourced from the Navit Project source code, released under the GNU General Public License version 2. To convert latitude and longitude into UTM X and Y coordinates, we can simply perform two calculations (lat is latitude and lon is longitude): x = lon × 6371000.0 × pi รท 180 y [...]
List files which are not up to date in CVS
Posted: 5th October 2009 by Tim in Bash, CVS, UbuntuTags: Bash, cvs, grep, pipe, repository, script, shell, terminal
CVS is annoying in that if you want to find out which files have been modified or need updating, you can’t simply use the cvs status command as there’s too much information displayed. In order to make it useful, you really need to filter the output. Note: the following tutorial only works for linux computers [...]
Math theory tells us that doing logarithms to custom bases can be done in the following way: log[base n](x) = log(x) / log(n) we can do the same in C. For example, if we wanted to calculate log[base 2](x): #include <math.h> … double log_2 = log(x) / log(2.0); Be sure to compile with the -lm [...]
Nanosleep in C / C++
Posted: 29th September 2009 by Tim in C, C++Tags: C, call, coding, development, nanosleep, programming, sleep, software engineering, usleep
usleep is not a very accurate form of sleep in C / C++. From the man page: The sleep may be lengthened slightly by any system activity or by the time spent processing the call or by the granularity of system timers. nanosleep, on the other hand, is much more accurate. The following code will [...]
Removing directories in CVS
Posted: 21st September 2009 by Tim in Bash, CVSTags: Bash, cvs, delete, directories, files, folders, linux, remove, repository, script, unix, version control, versioning
There is no way to delete a folder in CVS like you can with files. The directories must be kept so that the versioning information relating to the files which used to be in the repository can still be used (ie: you can revert back to a revision when the files still existed). The only [...]
Connecting to MySQL in C
Posted: 20th August 2009 by Tim in C, MySQL, UbuntuTags: C, coding, compile, connectivity, database, development, gcc, MySQL, sockets, sql, Ubuntu
MySQL comes with a library to make talking to MySQL with C easyish. There are a few things you have to install first, though. I’m using Ubuntu 8.04 for this walkthrough, but things should be similar for other flavours of Linux. Before we start, we have to download the development files required: sudo apt-get install [...]
Adding subdomains in Apache
Posted: 20th August 2009 by Tim in Apache, UbuntuTags: Apache, asp, configuration, HTML, Javascript, PHP, restart, server, web
There are a number of ways to add subdomains in apache, but here is the quickest and most basic way to get it done. This example is for Ubuntu 8.04, but may be used on other flavours of linux if you find the correct paths to the files. Let’s say you have a blog on [...]
Baud rate and other serial comm settings in C
Posted: 4th August 2009 by Tim in CTags: baud, C, coding, comms, communication, networking, programming, rate, serial, settings
Communicating through a serial port in C is pretty simple once it’s set up; you just read and write to it as though it was a file. The setting up, however, can be a real pain. Here’s the short and simple way to get it done. Note that this code requires the following headers: #include [...]
One advantage of C++ over C is the ability to have default values for arguments in a function. This is a useful feature if your method takes arguments which are mostly set to one value, or if the default value is likely to change, and very useful for constructors. The default value is specified in [...]
Calling object functions in C++ with pthreads
Posted: 13th July 2009 by Tim in C++Tags: C, call, class, coding, function, programming, pthreads, Software Development, software engineering
For various reasons, you cannot create a pthread on an object’s function. There is, however, a few ways to get around that. One of the most flexible ways of doing this is to create a wrapper function to call an object’s method, taking a pointer to the object as an argument. For example, if you [...]
Adding images to OpenGL in C
Posted: 7th July 2009 by Tim in OpenGLTags: bmp, C, coding, fig, image, jpeg, jpg, load, OpenGL, raw, Software Development, software engineering, texture, tif
Adding an image to a 2D OpenGL screen may seem like a relatively simple task, but it’s not. OpenGL is a rendering language, and as such has no native support for loading JPEG, GIF, TIF, BMP or any other popular image type. If, like me, you’re looking for a quick and simple way to get [...]
vbo_copy_vertices: Assertion `0′ failed.
Posted: 7th July 2009 by Tim in OpenGLTags: coding, gl, glu, glut, OpenGL, Software Development, vbo
While writing an OpenGL program, I came across the following error: vbo/vbo_exec_draw.c:135: vbo_copy_vertices: Assertion `0′ failed. The problem occured while trying to plot ~1 million points in a graph. Reducing this number to 2000 points fixed the problem. So if you’re encountering this error, try simplifying your display.