
给定数字的大小意味着该特定数字之间的差异 和零。它还可以表示一个数学对象相对于该数学对象中其他对象的大小 同种。我们将遵循这里的第一个定义,以及大小或绝对值 数字的表示为 |x|,其中 x 是实数。我们探索展示的方式 给定实数的绝对值或大小。
朴素方法
我们可以自己编写一个程序来找出给定实数的大小。这 下面解释了示例。
语法
int value;
if (value < 0) {
value = (-1) * value;
}
算法
- 在数值变量(整数/双精度)中获取输入。
- 如果数字
- 数字 := 数字 * (-1)
- 否则,
- 按原样返回号码。
示例
#includeusing 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()函数
abs() 函数返回给定数字的绝对值或大小值。它 支持所有数值类型,并且可在 C++ STL 中使用。
语法
double value; double absValue = abs(value);
算法
在名为 value 的变量中获取数字输入。 (名称可以是任何内容)
使用abs()函数转换为给定变量的绝对/幅度值。
WeWedding婚纱影楼小程序下载婚纱影楼小程序提供了一个连接用户与影楼的平台,相当于影楼在微信的官网。它能帮助影楼展示拍摄实力,记录访客数据,宣传优惠活动。使用频率高,方便传播,是影楼在微信端宣传营销的得力助手。功能特点:样片页是影楼展示优秀摄影样片提供给用户欣赏并且吸引客户的。套系页是影楼根据市场需求推出的不同套餐,用户可以按照自己的喜好预定套系。个人中心可以查看用户预约的拍摄计划,也可以获取到影楼的联系方式。
显示/使用震级值。
示例
#includeusing 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++ 编程语言。










