Computer Programming 简明教程
Computer Programming - Numbers
每种编程语言都支持操作不同类型的数字,例如简单的整数和小数。C、Java 和 Python 根据数字的性质将这些数字分类为几个类别。
Every programming language provides support for manipulating different types of numbers such as simple whole integers and floating point numbers. C, Java, and Python categorize these numbers in several categories based on their nature.
我们回到数据类型章节检查一下,我们在其中列出了与数字相关的核心数据类型:
Let’s go back and check the data types chapter, where we listed down the core data types related to numbers −
Type |
Keyword |
Value range which can be represented by this data type |
Number |
int |
-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
Small Number |
short |
-32,768 to 32,767 |
Long Number |
long |
-2,147,483,648 to 2,147,483,647 |
Decimal Number |
float |
1.2E-38 to 3.4E+38 till 6 decimal places |
这些数据类型被称为原始数据类型,你可以使用这些数据类型生成更多数据类型,即用户定义的数据类型。
These data types are called primitive data types and you can use these data types to build more data types, which are called user-defined data types.
我们已经看到在关于运算符的讨论期间,进行的各式各样的数学和逻辑操作。因此我们知道如何添加数字、减去数字、除以数字等等。
We have seen various mathematical and logical operations on numbers during a discussion on operators. So we know how to add numbers, subtract numbers, divide numbers, etc.
首先让我们看看如何打印 C 编程语言中可用的不同类型数字−
First let’s see how to print various types of numbers available in C programming language −
#include <stdio.h>
int main() {
short s;
int i;
long l;
float f;
double d;
s = 10;
i = 1000;
l = 1000000;
f = 230.47;
d = 30949.374;
printf( "s: %d\n", s);
printf( "i: %d\n", i);
printf( "l: %ld\n", l);
printf( "f: %.3f\n", f);
printf( "d: %.3f\n", d);
}
其余代码显而易见,但我们使用 %.3f 打印浮点数和双精度数,它表示要打印的小数点后面位数。当执行以上程序时,它产生以下结果−
Rest of the coding is very obvious, but we used %.3f to print float and double, which indicates the number of digits after the decimal to be printed. When the above program is executed, it produces the following result −
s: 10
i: 1000
l: 1000000
f: 230.470
d: 30949.374
Math Operations on Numbers
以下表格列出 C 编程语言中可用的各种有用的内置数学 functions ,可用于各种重要的数学计算。
The following table lists down various useful built-in mathematical functions available in C programming language which can be used for various important mathematical calculations.
例如,如果你想计算某数的平方根,比如 2304,那么你有一个可用于计算平方根的内置函数。
For example, if you want to calculate the square root of a number, for example, 2304, then you have a built-in function available to calculate the square root.
Sr.No. |
Function & Purpose |
1 |
double cos(double); This function takes an angle (as a double) and returns the cosine. |
2 |
double sin(double); This function takes an angle (as a double) and returns the sine. |
3 |
**double tan(double); This function takes an angle (as a double) and returns the tangent. |
4 |
double log(double); This function takes a number and returns the natural log of that number. |
5 |
double pow(double, double); The first is a number you wish to raise and the second is the power you wish to raise it to. |
6 |
double hypot(double, double); If you pass this function the length of two sides of a right triangle, it will return the length of the hypotenuse. |
7 |
double sqrt(double); You pass this function a number and it returns its square root. |
8 |
int abs(int); This function returns the absolute value of an integer that is passed to it. |
9 |
double fabs(double); This function returns the absolute value of any decimal number passed to it. |
10 |
double floor(double); Finds the integer which is less than or equal to the argument passed to it. |
以下是一个简单的例子,演示了一些数学运算。要利用这些函数,你需要在你的程序中包含 math 头文件 <math.h> ,和你包含 stdio.h 的方式相同。
Following is a simple example to show a few mathematical operations. To utilize these functions, you need to include the math header file <math.h> in your program in the same way you included stdio.h −
#include <stdio.h>
#include <math.h>
int main() {
short s;
int i;
long l;
float f;
double d;
printf( "sin(s): %f\n", sin(10));
printf( "abs(i): %f\n", abs(1000));
printf( "floor(f): %f\n", floor(230.47));
printf( "sqrt(l): %f\n", sqrt(1000000));
printf( "pow(d, 2): %f\n", pow(2.374, 2));
}
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
sin(s): -0.544021
abs(i): -0.544021
floor(f): 230.000000
sqrt(l): 1000.000000
pow(d, 2): 5.635876
除了上述用法外,你还将在循环计数、标志表示、C 编程中的真或假值中使用数字。
Besides the above usage, you will use numbers in loop counting, flag representation, true or false values in C programming.
Numbers in Java
以下是用 Java 编写的等效程序。Java 提供了 C 编程中几乎所有可用的数字数据类型。
Following is the equivalent program written in Java. Java provides almost all the numeric data types available in C programming.
你可以尝试执行以下程序,查看输出,它与上述 C 示例生成的输出相同。
You can try to execute the following program to see the output, which is identical to the result generated by the above C example.
public class DemoJava {
public static void main(String []args) {
short s;
int i;
long l;
float f;
double d;
s = 10;
i = 1000;
l = 1000000L;
f = 230.47f;
d = 30949.374;
System.out.format( "s: %d\n", s);
System.out.format( "i: %d\n", i);
System.out.format( "l: %d\n", l);
System.out.format( "f: %f\n", f);
System.out.format( "d: %f\n", d);
}
}
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
s: 10
i: 1000
l: 1000000
f: 230.470001
d: 30949.374000
Java 还为数学计算提供了一整套内置函数,你可以像在 C 编程中一样使用它们。
Java also provides a full range of built-in functions for mathematical calculation and you can use them in the same way as you did in C programming.
Numbers in Python
Python 与 C 和 Java 有点不同。它将数字分为 int 、 long 、 float 和 complex 。以下是 Python 中数字的一些示例−
Python is a little different from C and Java; it categorizes numbers in int, long, float and complex. Here are some examples of numbers in Python −
int |
long |
float |
complex |
10 |
51924361L |
0.0 |
3.14j |
100 |
-0x19323L |
15.20 |
45.j |
-786 |
0122L |
-21.9 |
9.322e-36j |
080 |
0xDEFABCECBDAECBFBAEl |
32.3+e18 |
.876j |
-0490 |
535633629843L |
-90. |
-.6545+0J |
-0x260 |
-052318172735L |
-32.54e100 |
3e+26J |
0x69 |
-4721885298529L |
70.2-E12 |
4.53e-7j |
以下是使用 Python 编写的等效程序−
Following is the equivalent program written in Python −
s = 10
i = 1000
l = 1000000
f = 230.47
d = 30949.374
print "s: ", s
print "i: ", i
print "l: ", l
print "f: ", f
print "d: ", d
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
s: 10
i: 1000
l: 1000000
f: 230.47
d: 30949.374
Python 还提供了一整套用于数学计算的内置函数,你可以像在 C 编程中一样使用它们。
Python also provides a full range of built-in functions for mathematical calculations and you can use them in the same way you have used them in C programming.