How to check for a NAN (Not A Number) in C++ in embedded systems without exception handlers

Sometimes there are bugs or special corner conditions that makes a NAN (Not A Number) occur in code compiled from a C++ source and executed on an embedded systems without memory to run exception handlers.

Here is what @Jalf writes over at StackExchange:

According to the IEEE standard, NaN values have the odd property that comparisons involving them are always false. That is, for a float f, f != f will be true only if f is NaN.

Note that, as some comments below have pointed out, not all compilers respect this when optimizing code.

For any compiler which claims to use IEEE floating point, this trick should work.

If you inplement a function in a separate .cpp file , how do you access objects instantiated in main from functions implemented that file?

Sometimes there is a need to spread code into several .cpp files to avoid clutter (for example main.cpp, other1.cpp, other 2.cpp) . So if you implement a function in a separate .cpp file, how do you access objects instantiated in main?

Solution: use extern

other1.cpp:

extern ObjectType objectname

void doSomething (void) 
{
      objectname.method();
}

Of course this is very basic stuff, but many new programmers ask about this so I included a short post about this here.

How to develop and debug C++ code for Arduino in Visual Studio if you are tired of the Arduino IDE

The Arduino platform has gained a fantastic popularity over the past ten years. For small quick and dirty projects, the .ino files and the standard IDE is OK. However, for professional development projects and for developers that want control over the .hpp and .cpp files the standard Arduino IDE is somewhat regarded like a toy. Furthermore there is no proper debugger in the Arduino IDE  (whaaat?, you gotta be kidding?)

Well, this has changed as Visual Micro has developed a plugin to Microsoft Visual Studio. You can write code the normal way you do it with .cpp and .hpp files. You can also run the GDB debugger. The IDE has support for the regular .INO files and Arduino libraries. Here is how both .cpp and .ino files are handled: http://www.visualmicro.com/page/User-Guide.aspx?doc=INOs-and-CPPs.html

There is a free version of Visual Micro and it works against the free versions of Microsoft Visual Studio 2017 Community Edition. All you need is a Microsoft account to be able to download and install Visual Studio 2017 Community edition. Then in MSVC 2017, go to tools, Extensions and Updates and enter a search for Arduino in the search bar. It will will offer to automatically install the Arduino tools and the GDB debugger. Click install and you are on the right path.

Links:
http://www.visualmicro.com/
https://marketplace.visualstudio.com/items?itemName=VisualMicro.ArduinoIDEforVisualStudio

If you find this tip useful, please share on facebook and share the link. Also, feel free to report your experiences in the comment field.