2.2 算术运算符和算术表达式(加、减、乘、除、求余、自增、自减,赋值运算,精度问题)

【重点】a += b执行速度更快

2*0.5,因为0.5的精度更高,所以运算结果的精度要和0.5一样。



位数太多采用高精度算法,位运算。

double d = a/b; //先算出a/b=3,再将3赋值给d,d=3.

d = (double)a/b; //先将a转化为double双精度型,然后再运算,运算结果依精度高的,因此(double)a/b=3.33333

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	int a = 10;
	int b = 3;
	double d = a/b;
	cout << d << endl;//3
	d = 5/2;//5/2=2
	cout << d << endl;//2
	d = 5/2.0;//5/2.0=2.5;
	cout << d << endl;//2.5
	d = (double)a/b;
	cout << d << endl;//3.33333
	return 0;
} 


#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
	int n1 , n2 = 5;
	n2 ++;//n2=6
	++ n2;//n2=7
	n1 = n2 ++;//n1=n2=7,n2自增1等于8
	cout << n1 << "," << n2 << endl;//7,8
	n1 = ++ n2;//n2自增1等于9,n1=n2=9;
	cout << n1 << "," << n2 << endl;//9,9
	return 0; 
}

猜你喜欢

转载自blog.csdn.net/yanyanwenmeng/article/details/80979518