在C++中,我们可以使用<cmath>
库来进行数学运算
#include <iostream>
#include <cmath>
int main() {
double number = 9.0;
double squareRoot = sqrt(number);
std::cout << "The square root of " << number << " is: " << squareRoot << std::endl;
return 0;
}
#include <iostream>
#include <cmath>
int main() {
double base = 2.0;
double exponent = 3.0;
double power = pow(base, exponent);
std::cout << "The power of " << base << " to the " << exponent << " is: " << power << std::endl;
return 0;
}
#include <iostream>
#include <cmath>
int main() {
double angleInRadians = M_PI / 4; // 45 degrees in radians
double sineValue = sin(angleInRadians);
double cosineValue = cos(angleInRadians);
double tangentValue = tan(angleInRadians);
std::cout << "The sine of " << angleInRadians << " radians is: " << sineValue << std::endl;
std::cout << "The cosine of " << angleInRadians << " radians is: " << cosineValue << std::endl;
std::cout << "The tangent of " << angleInRadians << " radians is: " << tangentValue << std::endl;
return 0;
}
#include <iostream>
#include <cmath>
int main() {
double number = 2.0;
double exponent = 3.0;
double result = pow(number, exponent);
std::cout << "The result of " << number << " raised to the power of " << exponent << " is: " << result << std::endl;
double logarithmBase = 2.0;
double logarithmValue = log(number) / log(logarithmBase);
std::cout << "The logarithm of " << number << " to the base of " << logarithmBase << " is: " << logarithmValue << std::endl;
return 0;
}
#include <iostream>
#include <cmath>
#include <limits>
int main() {
double number = 4.7;
int integerPart = static_cast<int>(number);
double fractionalPart = number - integerPart;
std::cout << "The integer part of " << number << " is: " << integerPart << std::endl;
std::cout << "The fractional part of " << number << " is: " << fractionalPart << std::endl;
int ceilingValue = ceil(number);
int floorValue = floor(number);
std::cout << "The ceiling value of " << number << " is: " << ceilingValue << std::endl;
std::cout << "The floor value of " << number << " is: " << floorValue << std::endl;
return 0;
}
这些测试示例展示了如何使用C++的<cmath>
库进行基本的数学运算。在实际应用中,你可能需要根据具体需求对这些函数进行调整和扩展。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。