Floating point rounding

2020-04-10 Permalink

What’s the most common and incorrect way to round a floating point number? It’s this:

float x = (int)(x + 0.5);

The cast to int truncates the fractional part. For negative numbers this yields completely bogus results. For example, −7.1 is offset to −6.6, which is in turn truncated to −6.

Using floor

A simple fix for the above problem is to use the floor function instead, that would consistently round towards negative infinity:

float x = floorf(x + 0.5); // -7.1 is rounded to -7.0

Unfortunately this method is still wrong. In fact there are two classes of numbers where it fails:

FreeBSD cheatsheet

2019-09-29 Permalink

Generate a secure password:

$ head -c 30 /dev/random | uuencode -mr -

Recover partitions (including exfat):

$ testdisk /dev/disk-or-section

Create then extract an encrypted backup (split into chunks):

$ tar -cf - input/ | openssl aes-256-cbc -md sha256 -pbkdf2 | split -b 100M -a 4 -d - output/backup.tar.
$ cat output/backup.tar.* | openssl aes-256-cbc -md sha256 -pbkdf2 -d | tar -xfv -

Set time in current timezone:

# date 2315

To set ISO date format, go to /usr/share/locale/en_US.UTF-8/LC_TIME (or similar) and change %m/%d/%Y to %Y-%m-%d. It's not respected by software that relies on their own date formatting (offender: Qt).

After tweaking the MIME Applications Associations, update the cache through

$ update-desktop-database ~/.local/share/applications/


Display the post-install message:

$ pkg query %M firefox

List non-automatic packages:

$ pkg query -e '%a = 0' %o

True encapsulation in C++

2019-04-25 Permalink

Access specifiers are taught as the encapsulation mechanism in C++. However, I’ve mentioned before how poorly they perform their function.

For example:

class A {
	// ... public interface ...
	void f(C);
	B b;

In order for this code to compile, B must be previously defined and C must be previously declared. Both of these types, as well as the function f and the layout of the private members of A are an implementation detail. Yet they leak out to the users of A. This in turn has negative impact on compile times (of incremental builds in particular), makes headers harder to comprehend, and makes it harder to change A implementation without breaking the ABI.

In the following text I refer to this approach as the ‘traditional’ method.

Useless language features

2019-03-01 Permalink

It strikes me how language designers don’t get this simple truth:

If the feature usefulness does not outweigh the complexity it adds to the language, it shall be left out.

In particular, if the feature is of the ‘no-op’ kind—i.e. it can be removed while retaining an identically functioning program without significantly sacrificing code clarity, or there is another way to express the same idea within the language in perhaps even better way. Many such features are better to be turned into implementation specific annotations (like C++ attributes), compiler warnings, or left out completely.

Let’s take a look at how it applies to C++ .

Access specifiers

C++ has three access specifiers: public, private and protected. They are the glorified mechanism for encapsulation in C++. However, if you ever wrote C++ code before, you know how poor the encapsulation they provide: