首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

求解两个单精度浮点数的乘积解决思路

2013-03-14 
求解两个单精度浮点数的乘积求解两个单精度浮点数的乘积float val2.22ffloat val2val*3.33333f//第一

求解两个单精度浮点数的乘积
求解两个单精度浮点数的乘积



float val=2.22f;

float val2=val*3.33333f;//第一种


float val3=2.22*3.33333;//第二种

哪一个可以保证精度不丢失?

第二:你们一般用什么法子求解两个浮点数的乘积
第一种?第二种?





[解决办法]
貌似第二种精度高一点。
第二种,乘法运算的是double相乘。

C89

3.2.1.5 Usual arithmetic conversions

Many binary operators that expect operands of arithmetic type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions: First, if either operand has type long double, the other operand is converted to long double. Otherwise, if either operand has type double, the other operand is converted to double. Otherwise, if either operand has type float, the other operand is converted to float. Otherwise, the integral promotions are performed on both operands. Then the following rules are applied: If either operand has type unsigned long int, the other operand is converted to unsigned long int. Otherwise, if one operand has type long int and the other has type unsigned int, if a long int can represent all values of an unsigned int, the operand of type unsigned int is converted to long int ; if a long int cannot represent all the values of an unsigned int, both operands are converted to unsigned long int. Otherwise, if either operand has type long int, the other operand is converted to long int. Otherwise, if either operand has type unsigned int, the other operand is converted to unsigned int. Otherwise, both operands have type int.

The values of operands and of the results of expressions may be represented in greater precision and range than that required by the type; the types are not changed thereby.

[解决办法]

引用:
其实我想知道的是 你为什么一定要保证精度

你是要用在军工上,用在战斗机上呢
还是要用在科研上,用来算天气预报呢

1、如果只是一般的软件开发,一般也就算到小数点后三到四位就差不多了吧
2、想精确,换单位而不是换数值,把你数值的单位换成更精确的单位,2.22m * 2.22m 换成 222cm * 222cm


你的意思是:

如果只要 在非特殊领域,的话,那么2种方式均可以了?




[解决办法]
显然大多数情况第二种精度高点啊。。。一个是先截取在乘,一个是先乘后截取,举个最极端的例子 
5.4 *5.4结果保留整数,如果你先截取在乘结果为25,先乘后截取是29,

热点排行