Close

0 == NULL and atof() is a bother

A project log for Gcode Manipulator

Why G53 zero your CNC machine when you could just MOVE YOUR CODE to the origin!

AndyAndy 02/16/2016 at 02:550 Comments

Bugs in code tend to come together, even worse, they cascade, one leading into another. In this case, there were two problems. First of all,

if (0 == NULL) {
  /*This will run*/
}
This is awful. A value of zero is equivalent to NULL?! Whatever. Don't use NULL, ever. Especially if you're checking if a variable is empty.

The second problem is:

atof(""); /* return: 0.0 */
atof("Hello World"); /* return: 0.0 */
atof returns 0.0f if it fails to convert the given character string into a number. Who thought that was a good idea?!

Therefore, the code has a new hack: AFTER a line is parsed and "converted" to a number, a check is run to see if the variable that was used to set the coordinates is empty. If it is, then std::nan() is used to overwrite that section of the parsed line struct and later on, when it is important to know whether the variable was empty or not, std::isnan() is used to check. What a mess.
I updated the Github repository with the latest changes, all seems to be well now. I'm working on compiled binaries to put on this page, because that 1Gb of free space on Hackaday.io is tempting me!

Discussions