您好,登錄后才能下訂單哦!
在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。