c语⾔整数和浮点数_C++处理整数和浮点数
c语⾔ 整数和浮点数
C ++中的所有数字 ( All About Numbers in C++ )
In C++ there are two types of numbers. and . There are also variants of these types that hold bigger numbers, or only numbers but they are still ints or floats.
在C ++中,有两种类型的数字。 和 。 这些类型也有⼀些变体,可以容纳更⼤的数字,或仅保留数字,但它们仍然是整数或浮点数。
An int is a whole number like 47 without a decimal point. You can't have 4.5 babies or loop 32.9 times. You can have $25.76 if you use a float. So when you create your program, you must decide which type to use.
⼀个整数是⼀个像47这样的整数,没有⼩数点。 您不能⽣育4.5个婴⼉,也不能循环32.9次。 如果使⽤浮点数,则可以有$ 25.76。 因此,在创建程序时,必须确定要使⽤的类型。
为什么不只使⽤浮标呢? ( Why not Just Use Floats? )
This is what some scripting languages do? Because it's inefficient, floats take up more memory and are generally slower than ints. Also, you cannot easily compare two floats to see if they are equal like you can with ints.
这是某些脚本语⾔做什么? 由于浮动效率低下,因此浮动占⽤更多内存,并且通常⽐整数慢。 ⽽且,您⽆法像使⽤int⼀样轻松地⽐较两个浮点数以查看它们是否相等。
To manipulate numbers you have to store them in memory. Because the value can be easily changed, it's called a variable.
要操纵数字,您必须将其存储在内存中。 由于该值可以轻松更改,因此称为变量。
Read more about variables in
在变量中阅读有关变量的更多信息
The that reads your program and converts it into machine code needs to know what type it is, i.e. whether it's an int or a float, so before your program uses a variable, you must it.
读取程序并将其转换为机器代码的需要知道它是什么类型,即它是int还是float,因此在程序使⽤变量之前,必须对其进⾏ 。
Here's an example.
这是⼀个例⼦。
int Counter =0;
float BasicSalary;
You'll notice that the Counter variable is set to 0. This is an optional initialization. It's a very good practice to initialize variables. If you don't initialize and then use them in code without having set an initial value, the variable will start with a random value that may 'break' your code. The value will be whatever was in memory when the program was loaded.
您会注意到Counter变量设置为0。这是可选的初始化。 初始化变量是⼀个很好的做法。 如果您不进⾏初始化,然后在未设置初始值的情况下在代码中使⽤它们,则变量将以可能会“破坏”代码的随机值开头。 该值将是加载程序时内存中的值。
有关Ints的更多信息 ( More about Ints )
What is the biggest number an int can store?. Well, it depends on the type of but it is generally accep
ted as 32 bits. Because it can hold almost as many negative values as positive, the range of values is +/- 2-32 to 232 or -2,147,483,648 to
+2,147,483,647.
⼀个int可以存储的最⼤数量是多少? 。 好吧,这取决于的类型,但通常被接受为32位。 因为它可以容纳⼏乎与正数⼀样多的负值,所以值的范围为+/- 2 -32⾄2 32或-2,147,483,648⾄+2,147,483,647。
This is for a signed int, but there is also an int that holds zero or positive. It has a range of 0 to 4,294,967,295. Just remember - unsigned ints don't need a sign (like + or -1) in front of them because they are always positive or 0.
这是针对有符号的int,但也有⼀个 int,它保持零或正数。 它的范围是0到4,294,967,295。 请记住 -⽆符号整数不需要在它们前⾯加⼀个符号(例如+或-1),因为它们始终为正或0。
短整数 ( Short Ints )
There is a shorter int type, coincidentally called short int which uses 16 bits (2 bytes). This holds numbers in the range -32768 to +32767. If you use a large umber of ints, you can possibly save memory by using short ints. It will not be any quicker, despite being half the size. 32 Bit CPUs fetch v
alues from memory in blocks of 4 bytes at a time. I.e. 32 bits (Hence the name- 32 Bit CPU!). So fetching 16 bits still requires a 32 bit fetch.
有⼀个较短的int类型,巧合地称为short int,它使⽤16位(2个字节)。 该数字范围为-32768⾄+32767。 如果您使⽤⼤量的整数,则可以使⽤简短的整数来节省内存。 尽管尺⼨只有⼀半,但速度不会更快。 32位CPU⼀次以4个字节的块的形式从内存中获取值。 即32位(因此命名为32位CPU!)。 因此,获取16位仍需要32位获取。
There is a longer 64 bit called long long in C. Some C++ compilers while not supporting that type directly use an alternate both Borland and Microsoft use _int64. This has a range of -9223372036854775807 to 9223372036854775807 (signed) and 0 to 18446744073709551615 (unsigned).
在C语⾔中,有⼀个较长的64位称为long long 。某些不⽀持该类型的C ++编译器直接使⽤替代名称-例如Borland和Microsoft都使⽤
_int64 。 其范围是-9223372036854775807⾄9223372036854775807(带符号)和0⾄18446744073709551615(⽆符号)。
As with ints there is an unsigned short int type that has a range of 0..65535.
与int⼀样,存在⽆符号short int类型,其范围为0..65535。
Note: Some computer languages refer to 16 bits as a Word.
注意 :某些计算机语⾔将16位称为Word。
精密算术 ( Precision Arithmetic )
双重⿇烦 ( Double Trouble )
There's no long float, but there is a type that is twice as big as float.
没有长浮点数,但是有⼀个精度类型,它是浮点数的两倍。
Float: Occupies 4 bytes. Range 17x10-38 to 1.7x1038
浮点数 :占⽤4个字节。 范围17x10 -38⾄1.7x10 38
Double: Occupies 8 bytes. Range 3.4x10-308 to 3.4308
双精度:占⽤8个字节。 范围3.4×10 -308到3.4 308
Unless you're doing scientific programming with very large or small numbers, you'll only use doubles for greater precision. Floats are good for 6 digits of accuracy but doubles offer 15.
除⾮您使⽤⾮常⼤或很⼩的数字进⾏科学编程,否则只会使⽤双精度来提⾼精度。 浮点数可提供6位数的精度,但双精度浮点数提供15。精确 ( Precision )
Consider the number 567.8976523. It is a valid float value. But if we print it out with this code below you can see lack of precision appearing. The number has 10 digits but is being stored in a float variable with only six digits of precision.
考虑数字567.8976523。 这是有效的浮点值。 但是,如果我们在下⾯的代码中将其打印出来,您会发现缺乏精度。 该数字有10位数字,但存储在float变量中,精度只有6位数字。
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
float value = 567.8976523;
cout.precision(8) ;
cout << value << endl;
return 0;
}
See for details on how cout works, and how to use precision. This example sets the output precision to 8 digits. Unfortunately floats can only hold 6 and some compilers will issue a warning about converting a double to a float. When run, this prints out 567.89764
有关cout的⼯作⽅式以及使⽤精度的详细信息,请参见 。 本⽰例将输出精度设置为8位数字。 不幸的是,浮点数最多只能容纳6个,并且某些编译器会发出有关将double转换为float的警告。 运⾏时,将打印出567.89764
If you change the precision to 15, it prints as 567.897644042969. Quite a difference! Now move the decimal point two to the left so the value is 5.678976523 and rerun the program. This time it outputs 5.67897653579712. This is more accurate but still different.
如果将精度更改为15,则打印为567.897644042969。 完全不同! 现在,将⼩数点后两位向左移动,以便该值为5.678976523,然后重新运⾏该程序。 这次输出5.67897653579712。 这更准确,但仍然不同。
If you change the type of value to double and the precision to 10 it will print the value exactly as defined. As a general rule, floats are handy for small, non integer numbers but with more than 6 digits, you have to use doubles.
如果将值的类型更改为double并将精度更改为10,则将完全按照定义打印该值。 通常,浮点数适⽤于较⼩的⾮整数,但位数超过6位,则必须使⽤双精度。
了解算术运算 ( Learn about Arithmetic Operations )
Writing computer software wouldn't be much use if you couldn't do addition, subtraction etc. Here's example 2.
如果您不能进⾏加法,减法等操作,那么编写计算机软件的⽤处就不会太⼤。这是⽰例2。
// ex2numbers.cpp
//
#include <iostream>
using namespace std;
int main()
{
int a=9;
int b= 12;
int total=a+b;
cout << "The total is " << total << endl;
return 0;
}
⽰例2的说明 ( Explanation of Example 2 )
Three int are declared. A and B are assigned values, then total is assigned the sum of A and B.
声明了三个int 。 为A和B分配值,然后为total分配A和B的总和。
在运⾏此⽰例之前 ( Before running this example )
Here's a little tip to save time when running Command Line applications.
这是在运⾏命令⾏应⽤程序时节省时间的⼀些技巧。
When you run this program from the Command Line, it should output "The number is 22".
当您从命令⾏运⾏该程序时,它应输出“数字为22” 。
其他算术运算 ( Other Arithmetic Operations )
As well as addition, you can do subtraction, multiplication and division. Just use + for addition, - for subtraction, * for multiplication and / for division.
除加法外,您还可以进⾏减法,乘法和除法。 只需将+⽤于加法,将-⽤于减法,将*⽤于乘法和/除法。
Try changing the above program- use subtraction or multiplication. You can also change ints to floats or .
尝试更改上述程序-使⽤减法或乘法。 您还可以将int更改为float或 。
With floats, you have no control over how many decimal points are displayed unless you set the precision as shown earlier.
使⽤浮点数时,除⾮您如前所述设置精度,否则您⽆法控制显⽰的⼩数点数。
使⽤cout指定输出格式 ( Specifying Output Formats with cout )
When you're outputting numbers, you need to think about these attributes of the numbers.
在输出数字时,需要考虑数字的这些属性。
Width- How much space is needed for the entire number
宽度-整数需要多少空间
Alignment - left or right- numbers tend to be right aligned
对齐-左或右数字趋于右对齐
Number of decimal places
⼩数位数
Sign or brackets for negative numbers.
负号的符号或⽅括号。
Thousands Separators. Big numbers look ugly without these.
千位分隔符。 没有这些,⼤数字看起来很难看。
Now width, alignment, number of decimal places and signs can be set by the cout object and iomanip include file functions.
现在,宽度,对齐⽅式,⼩数位数和符号可以由cout对象和iomanip包含⽂件函数设置。
Thousands separators are a little more complicated. They are set from the locale of a PC. A locale contains information relevant to your country- such as currency symbols and decimal point and thous
ands separators. In the UK and USA, the number 100.98 uses a decimal point . as the decimal point whereas in some European countries it is a comma so €5,70 means a price of 5 Euros and 70 cents.
千位分隔符要复杂⼀些。 它们是从PC的语⾔环境设置的。 语⾔环境包含与您的国家/地区有关的信息,例如货币符号和⼩数点以及千位分隔符。 在英国和美国,数字100.98使⽤⼩数点。 作为⼩数点,⽽在某些欧洲国家中,这是逗号,因此€5,70表⽰价格为5欧元和70美分。
int main()
{
double a=925678.8750;
cout.setf(ios_base::showpoint|ios_base::right) ;
cout.fill('=') ;
cout.width(20) ;
locale loc("") ;
cout.imbue( loc ) ;
cout.precision(12) ;
cout << "The value is " << a << endl;
//cout.unsetf(ios_base::showpoint) ;
浮点型变量floatcout << left << "The value is " << a << endl;
for (int i=5;i< 12;i++) {
cout.precision(i) ;
cout << setprecision(i)<< "A= " << a << endl;
}
const moneypunct <char, true> &mpunct = use_facet <moneypunct <char, true > >(loc) ;
cout << loc.name( )<< mpunct.thousands_sep( ) << endl;
return 0;
}
The output from this is
输出是
=======The value is 925,678.875000
The value is 925,678.875000
A= 9.2568e+005
A= 925,679.
A= 925,678.9
A= 925,678.88
A= 925,678.875
A= 925,678.8750
A= 925,678.87500
English_United Kingdom.1252,
关于语⾔环境和Moneypunct ( About Locale and Moneypunct )
The example used a locale from the PC in the line
该⽰例在该⾏中使⽤了来⾃PC的语⾔环境
locale loc("") ;
The line
线
const moneypunct <char, true> &mpunct = use_facet <moneypunct <char, true > >(loc) ;
creates an object mpunct which is a reference to a moneypunct template class. This has information
about the specified locale - in our case, the thousands_sep() method returns the character used for thousands separator.
创建⼀个对象mpunct ,该对象是对moneypunct模板类的引⽤。 它具有有关指定语⾔环境的信息-在我们的例⼦中, 数千个(sep)⽅法返回⽤于千位分隔符的字符。
Without the line
没有线

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。