第一章 编程初步
1.3.2 数学函数
课程里的公式
将公式全部写出查看结果
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << "a. " << sin( 3.1415926 / 6 ) << endl;
cout << "b. " << asin( 1.0 / 2.0 ) << endl;
cout << "b. " << asin( 1 / 2 ) << endl;
cout << "b. " << asin( 1.0 / 2 ) << endl;
cout << "b. " << asin( 1 / 2.0 ) << endl;
cout << "c. " << cos( -3.1415926 ) << endl;
cout << "d. " << acos( 1.0 ) << endl;
cout << "e. " << tan( -3.1415926 * 4 / 3 ) << endl;
cout << "f. " << atan( 1.0 ) << endl;
cout << "g. " << log( 10.0 ) << endl;
cout << "h. " << log10( 2.71828 ) << endl;
cout << "****" << endl;
cout << "i. " << abs( -1 ) << endl;
cout << "i. " << abs( -1.2 ) << endl;
cout << "j. " << fabs( -2.5 ) << endl;
cout << "k. " << sqrt( 10 * 10 - 4 * 2 * 3 ) << endl;
cout << "l. " << floor((sqrt(5.0) + 1.0) /2.0) << endl;
cout << "m. " << exp( 2.8 ) << endl;
cout << "n. " << (exp(2.0) - exp(-2.0)) / 2.0 << endl;
cout << "o. " << pow( 5.4, 3.7 ) << endl;
cout << "p. " << pow( 15.635, 1.0/3.0 ) << endl;
return 0;
}
结果显示
结果分析
1.第二个公式示例 b. 中,计算式用整数和小数明显影响到计算结果,因为 1/2 会被自动计算为整数等于0,所以最终结果为0;
2. fabs 和 abs 的区别,fabs 为计算浮点数的绝对值,abs 为计算整数的绝对值;
3. 但是实测 i. abs 同样支持小数,网上查询与头文件和C版本有关,参考课程中老师给出的网站 C++ reference
语法自测-试题和答案
cout必须小写