[Functions]D. Liang 5.19 Using the sqrt function.c

Description

Input two integers A and B (0<A,B<1000, A<B), then prints the square root of all integers between A and B (including A and B).

Example Input

0 5

Example Output

      Number  SquareRoot
           0      0.0000
           1      1.0000
           2      1.4142
           3      1.7321
           4      2.0000
           5      2.2361
					 

*The last line is a newline character.
You should set the width of print field of each column to 12, set the precision of floating-point numbers to 4 and fixed, justify the output to the right.

Pay attention to the output requirements.

//   Date:2020/4/3 
//   Author:xiezhg5 
#include <stdio.h>
#include <math.h>
int main(void)
{
	int a,b,i,j;
	scanf("%d %d",&a,&b);
	printf("      Number  SquareRoot\n");
	for(i=a;i<=b;i++)
		printf("%12d%12.4lf\n",i,sqrt(i));  //调用sqrt函数轻而易举解决 
	return 0;
}

sqrt, sqrtf, sqrtl

C Numerics Common mathematical functions
Defined in header <math.h>

在这里插入图片描述
1-3) Computes square root of arg.
4) Type-generic macro: If arg has type long double, sqrtl is called. Otherwise, if arg has integer type or the type double, sqrt is called. Otherwise, sqrtf is called. If arg is complex or imaginary, then the macro invokes the corresponding complex function (csqrtf, csqrt, csqrtl).

Parameters

arg - floating point value
Return value
If no errors occur, square root of arg ( a r g ) (\sqrt{arg}) is returned.
If a domain error occurs, an implementation-defined value is returned (NaN where supported).
If a range error occurs due to underflow, the correct result (after rounding) is returned.

Error handling

Errors are reported as specified in math_errhandling.
Domain error occurs if arg is less than zero.
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
If the argument is less than -0, FE_INVALID is raised and NaN is returned.
If the argument is +∞ or ±0, it is returned, unmodified.
If the argument is NaN, NaN is returned

Notes

sqrt is required by the IEEE standard to be exact. The only other operations required to be exact are the arithmetic operators and the function fma. After rounding to the return type (using default rounding mode), the result of sqrt is indistinguishable from the infinitely precise result. In other words, the error is less than 0.5 ulp. Other functions, including pow, are not so constrained.

Example

Run this code

#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <fenv.h>
 
#pragma STDC FENV_ACCESS ON
 
int main(void)
{
    // normal use
    printf("sqrt(100) = %f\n", sqrt(100));
    printf("sqrt(2) = %f\n", sqrt(2));
    printf("golden ratio = %f\n", (1+sqrt(5))/2);
    // special values
    printf("sqrt(-0) = %f\n", sqrt(-0.0));
    // error handling
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("sqrt(-1.0) = %f\n", sqrt(-1));
    if(errno == EDOM) perror("    errno == EDOM");
    if(fetestexcept(FE_INVALID)) puts("    FE_INVALID was raised");
}

Possible output:

sqrt(100) = 10.000000
sqrt(2) = 1.414214
golden ratio = 1.618034
sqrt(-0) = -0.000000
sqrt(-1.0) = -nan
    errno = EDOM: Numerical argument out of domain
    FE_INVALID was raised
发布了165 篇原创文章 · 获赞 124 · 访问量 5618

猜你喜欢

转载自blog.csdn.net/qq_45645641/article/details/105289606