
给定数字的大小意味着该特定数字之间的差异和零。它还可以表示一个数学对象相对于该数学对象中其他对象的大小同种。我们将遵循这里的第一个定义,以及大小或绝对值数字的表示为 |x|,其中 x 是实数。我们探索展示的方式给定实数的绝对值或大小。
朴素方法
我们可以自己编写一个程序来找出给定实数的大小。这下面解释了示例。
语法
int value;if (value < 0) { value = (-1) * value;}
算法
在数值变量(整数/双精度)中获取输入。如果数字 数字 := 数字 * (-1)否则,按原样返回号码。
示例
#include using namespace std;// finds the magnitude value of a given numberint 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: -19The magnitude value of the number is: 19
使用abs()函数
abs() 函数返回给定数字的绝对值或大小值。它支持所有数值类型,并且可在 C++ STL 中使用。
语法
double value;double absValue = abs(value);
算法
在名为 value 的变量中获取数字输入。 (名称可以是任何内容)
使用abs()函数转换为给定变量的绝对/幅度值。
显示/使用震级值。
示例
#include using namespace std;// finds the magnitude value of a given numberdouble 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.325The magnitude value of the number is: 197.325
结论
各种数学运算都需要确定震级值。因为其中,我们必须设计方法来找出震级值并学习输出相同的各种内置函数。在给定的文章中,我们讨论了这两种方法都适用于 C++ 编程语言。
以上就是获取给定数字的大小的C++程序的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444011.html
微信扫一扫
支付宝扫一扫