
给定数字的大小意味着该特定数字之间的差异 和零。它还可以表示一个数学对象相对于该数学对象中其他对象的大小 同种。我们将遵循这里的第一个定义,以及大小或绝对值 数字的表示为 |x|,其中 x 是实数。我们探索展示的方式 给定实数的绝对值或大小。
我们可以自己编写一个程序来找出给定实数的大小。这 下面解释了示例。
int value;
if (value < 0) {
value = (-1) * value;
}
#include <iostream>
using namespace std;
// finds the magnitude value of a given number
int solve(int value){
// if smaller than zero, then multiply by -1; otherwise return the number itself
if (value < 0) {
value = (-1) * value;
}
// return the magnitude value
return value;
}
int main(){
int n = -19;
//display the number and its magnitude
cout << "The number is: " << n << endl;
cout << "The magnitude value of the number is: " << solve(n) << endl;
return 0;
}
The number is: -19 The magnitude value of the number is: 19
abs() 函数返回给定数字的绝对值或大小值。它 支持所有数值类型,并且可在 C++ STL 中使用。
double value; double absValue = abs(value);
在名为 value 的变量中获取数字输入。 (名称可以是任何内容)
使用abs()函数转换为给定变量的绝对/幅度值。
显示/使用震级值。
#include <iostream>
using namespace std;
// finds the magnitude value of a given number
double solve(double value){
// return the magnitude value using the abs function
return abs(value);
}
int main(){
double n = -197.325;
//display the number and its magnitude
cout << "The number is: " << n << endl;
cout << "The magnitude value of the number is: " << solve(n) << endl;
return 0;
}
The number is: -197.325 The magnitude value of the number is: 197.325
各种数学运算都需要确定震级值。因为 其中,我们必须设计方法来找出震级值并学习 输出相同的各种内置函数。在给定的文章中,我们讨论了 这两种方法都适用于 C++ 编程语言。
以上就是获取给定数字的大小的C++程序的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号