A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
sqrt computes the square root of a double. sqrtf handles float. sqrtl handles long double. All three are in math.h. Passing a negative argument returns NaN and sets errno to EDOM. The functions use the hardware floating-point unit's square root instruction, which is fast and accurate to the full precision of the type. For integer square roots, cast to double, compute, and cast back, being aware of rounding.
#include <stdio.h>#include <math.h>#include <errno.h>int main(void) {printf("sqrt(144.0): %.4f\n", sqrt(144.0));printf("sqrt(2.0): %.10f\n", sqrt(2.0));printf("sqrtf(9.0f): %.4f\n", sqrtf(9.0f));printf("sqrtl(2.0L): %.10Lf\n", sqrtl(2.0L));errno = 0;double result = sqrt(-4.0);printf("sqrt(-4.0): %.4f (errno=%d)\n", result, errno);printf("isnan: %d\n", isnan(result));return 0;}
errno 33 is EDOM (domain error). The result is NaN. The isnan check confirms it. In production code, validate inputs before calling sqrt, or check isnan after. Do not let NaN propagate silently through a calculation pipeline. It contaminates every subsequent operation and produces garbage output far from the original error.
sqrt is a hardware-accelerated operation. It is fast and precise. The only thing to watch is negative inputs. Validate or check for NaN. Next: floating-point classification functions, which let you inspect whether a value is normal, subnormal, infinite, or NaN.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.