All of this is fair enough. As I've learnt more about MATLAB I've found some of its dark corners — weird little inconsistencies in the language where the developers had to make a decision about how the language would behave, and in hindsight they jumped the wrong way. Every language has these, though some have more than most, and some languages will break compatibility to clear them up.
Today, however, I found something that went far beyond such minor complaints.
>> a = single(1);
>> b = double(2);
>> c = a*b;
>> class(c)
ans =
single
A single precision floating-point number, multiplied by a double precision floating-point number, returns a single. This means that MATLAB is silently destroying information.
To be fair, when I looked this up in the documentation, it was clearly documented. I just never thought to look, as I would never have expected behaviour like this in any language, and especially not from software which is intended for use in scientific computing.
Here's the worst of it, though:
>> a = double(3);
>> b = uint8(100);
>> c = a*b;
>> class(c)
ans =
uint8
>> c
c =
255
Not only is uint8 taken as the type of the result, but it silently truncates the real result (300) to the maximum value which will fit in the type, 255. No overflow error, not even a warning.
The danger of this is that from even one value, single precision (or worse) will silently propagate through the code. The only way you'd ever know is if this produces an very unexpected or impossible result. (In my case, I got negative variances using the computational form. This formula subtracts two very large numbers, so it's sensitive to precision).
Double precision is the default in many cases. For example, numeric literals in the code are interpreted as double. So there are cues giving the developer a false sense of security.
On discovering this, I had a daydream about exposing this to the world. In this fantasy a health warning would have to be added to the box: "This product will produce single precision results on single and double input, which is considered harmful in the state of Massachusetts". I then imagined that I was the tech lead for a software team, and after a tense conversation with the boss, had gathered all my developers to tell them that I could no longer trust the language we were using, and would have to start plans to migrate away*.
As it is, in the situation I'm in, I'll just have to swallow my pride and keep using it. I'll need to review all the code that I manage, and maybe rerun some of our calculations, some of which take several weeks. So, I'm sitting at home, ranting away, and drinking a double from my glorious motherland**. Cheers!
gpig rant disclaimer: For all that this is annoying, I have running water and no civil war, and so count myself lucky.
* I would actually, really have done this if I was in any position to do so. It's about the only time I've remotely been close to wishing for authority of any sort in my work life.
** Oban 14yo, in case you were wondering.
< MLP: Cyclist records drivers' bad behavior with helmet cam. | Fire, fire, fire! > |