I have often wondered why C++ has evolved into the language of choice for most embedded developers. Do the developers feel more comfortable in expressing their ideas when they code in C++? Does the object oriented approach lead to a more structured code for a given problem, which makes it easier to read and understand? But, then, what price does this convenience come at? Is it possible to write programs in C++ that operate as close to the hardware as something written in C? Does the object-oriented features like dynamic resolution add overheads to the compiled code?
To answer these…
For all the years I have spent coding in C, I have avoided comparing floating-point numbers, because, unlike integer comparison, they are known to produce unpredictable results, owing to the method floating-point numbers are stored in memory. For example, if I run the code below, what do you think the output will be?
float n_0 = 1.0 / 2.0;
float n_1 = 1.0 / 200.0;
float n_2 = n_1 * 100.0;printf(n_0 == n_2 ? "You got lucky!" : "Not so accurate, are you?")
On almost every computer out there in the sun, you will end up “getting lucky”, and…