COHERENT manpages
This page displays the COHERENT manpage for sqrt() [Compute square root].
List of available manpages
Index
sqrt() -- Mathematics Function (libm) Compute square root #include <math.h> double sqrt(z) double z; sqrt() returns the square root of z. Example The following program prints all prime numbers between one and a positive integer that the user enters. It was written by Michael B. Young (myoung@mcs.csuhayward.edu). #include <math.h> #include <stdio.h> #include <stdlib.h> int main() { int i, userinput; /* get user input */ fprintf(stderr, "Enter an integer value greater than 2: "); scanf("%d", &userinput); if (userinput < 3) { fprintf(stderr, "Error: enter a positive integer > 2\n"); exit(EXIT_FAILURE); } /* test for all numbers between one and "userinput". */ /* for efficiency's sake, even numbers are not tested. */ /* two is the only even prime number */ printf("%d\n", 2); for (i = 3; i < userinput; i += 2) if (prime(i)) printf("%d\n", i); exit(EXIT_SUCCESS); } /* * function prime() - tests the passed integer testvalue for "prime-ness" * by testing whether each integer between 1 and the square root of * testvalue divides evenly into testvalue. Returns 1 if prime, 0 if not. */ int prime(testvalue) int testvalue; { int end, j, result; end = (int) sqrt ( (double) testvalue ); for (j = 2, result = 1; result == 1 && j <= end; j++) { if ((testvalue % j) == 0) result = 0; } return result; } See Also cos(), cosh(), libm, sin() ANSI Standard, §7.5.5.2 POSIX Standard, §8.1 Diagnostics When a domain error occurs (i.e., when z is negative), sqrt() sets errno to EDOM and returns zero.