[Solved] Square root function error

Today I write such a line of code:
vac = sqrt(sumV/numberOfSamples);

When compile I get error below:
az.cpp:119:36: error: ‘sqrt’ was not declared in this scope

I searched documentation and realized that square root is available with Spark core: http://docs.spark.io/firmware/#math-sqrt

My question is: what’s wrong here?

@tdphan do you have the library included?

#include "math.h"

As @semaja2 has pointed out, you are missing this #include.

The docs do state:

#MATH#

Note that in addition to functions outlined below all of the newlib math functions described at sourceware.org are also available for use by simply including the math.h header file thus:

  #include "math.h"

It works right after math.h included. Thank you for quick help.

1 Like

The docs are wrong. What is said is that in order to get functions not listed below you need to #include "math.h" - sqrt is listed below. I suppose I should report this at github

1 Like

Granted, the docs do word this a bit oddly (and this said from a guy speaking German ;-))

The way I understand this sentence you could read it two ways:

  1. as a statement of contrast (these few here and the others on the other side)
  2. as "The functions below are some of the newlib math functions, but you can use all of them once you include math.h"

I just reread the doco and I would agree this worded poorly, it should just simply state that the spark has the capabilities of the newlibmath library, and to use it you need to include the math.h

There is no compile error when I use time functions Time.zone(), Time.hour(), … without including any files. Same Math library no compile error if function abs() used. I get error only with function sqrt(). Solution is simple: include math.h. I think it would better if some one added such info in doc file.