为什么(1/2)* x与0.5 * x不同?

时间:2021-11-01 06:03:01

This behaves as wanted:

这表现得很好:

double t = r[1][0] * .5;

But this doesn't:

但这不是:

double t = ((1/2)*r[1][0]);

r is a 2-D Vector.

r是2-D向量。

Just thought of a possibility. Is it because (1/2) is considered an int and (1/2) == 0?

想到了一种可能性。是因为(1/2)被认为是int而(1/2)== 0?

6 个解决方案

#1


58  

Is it because (1/2) is considered an int and (1/2) == 0?

是因为(1/2)被认为是int而(1/2)== 0?

Yes, both of those literals are of type int, therefore the result will be of type int, and that result is 0.

是的,这两个文字都是int类型,因此结果将是int类型,结果为0。

Instead, make one of those literals a float or double and you'll end up with the floating point result of 0.5, ie:

相反,使其中一个文字成为浮点数或双精度数,你最终会得到0.5的浮点结果,即:

double t = ((1.0/2)*r[1][0]);

double t =((1.0 / 2)* r [1] [0]);

Because 1.0 is of type double, the int 2 will be promoted to a double and the result will be a double.

因为1.0是double类型,所以int 2将被提升为double,结果将是double。

#2


14  

Write this instead:

写这个:

  double t = ((1/2.0)*r[1][0]);

1 / 2 is an integer division and the result is 0.

1/2是整数除法,结果为0。

1 / 2.0 is a floating point division (with double values after the usual arithmetic conversions) and its result is 0.5.

1 / 2.0是浮点除法(在通常的算术转换后具有双值),其结果为0.5。

#3


5  

Because 1/2 is int/int division. That means whatever is the result will have anything after the decimal point removed (truncated). So 1/2 = 0.5 = 0.

因为1/2是int / int分区。这意味着无论什么结果将删除小数点后的任何内容(截断)。所以1/2 = 0.5 = 0。

Normally I always write the first number in double : 1.0/2 …..

通常我总是把第一个数字写成双倍:1.0 / 2 ......

If you make the very first number a double then all remaining calculation is done in double only.

如果您将第一个数字设为double,则所有剩余计算仅以double进行。

#4


2  

double t = r[1][0] * .5;

is equivalent to:

相当于:

double t = ((1/2f)*r[1][0]);

and not:

double t = ((1/2)*r[1][0]);

Due to loss of decimal part when the temporary result of 1/2 is stored in an int variable.

当1/2的临时结果存储在int变量中时,由于小数部分的丢失。

As a guideline whenever there is a division and there is a possibility of the answer being real number, do not use int or make one of the operands float or double or use cast.

作为指导,只要存在除法并且答案可能是实数,不要使用int或使其中一个操作数浮点或双精度或使用强制转换。

#5


1  

You can write 1.0/2.0 instead. 1/2 displays this behaviour because both the denominator and numerator act are of an integer type and a variable of an integer type divided by another variable of an integer type is always truncated to an integer.

你可以写1.0 / 2.0。 1/2显示此行为,因为分母和分子动作都是整数类型,整数类型的变量除以整数类型的另一个变量总是被截断为整数。

#6


0  

I cannot merit or demerit the standard of the question but this seem very critical issue to me. We assume that compiler will do the laundry for us all the time , but that is not true some times.

我不能指出或不符合问题的标准,但这对我来说似乎是非常关键的问题。我们假设编译器会一直为我们洗衣服,但有时候这不是真的。

Is there any way to avoid this situation ?

有没有办法避免这种情况?

Possibly

OR

More importantly knowing the monster (C,C++) as most of the people point out above

更重要的是,了解大多数人指出的怪物(C,C ++)

I would like to know if there are other ways to trace these "truncation" issues at compile time

我想知道是否有其他方法可以在编译时跟踪这些“截断”问题

#1


58  

Is it because (1/2) is considered an int and (1/2) == 0?

是因为(1/2)被认为是int而(1/2)== 0?

Yes, both of those literals are of type int, therefore the result will be of type int, and that result is 0.

是的,这两个文字都是int类型,因此结果将是int类型,结果为0。

Instead, make one of those literals a float or double and you'll end up with the floating point result of 0.5, ie:

相反,使其中一个文字成为浮点数或双精度数,你最终会得到0.5的浮点结果,即:

double t = ((1.0/2)*r[1][0]);

double t =((1.0 / 2)* r [1] [0]);

Because 1.0 is of type double, the int 2 will be promoted to a double and the result will be a double.

因为1.0是double类型,所以int 2将被提升为double,结果将是double。

#2


14  

Write this instead:

写这个:

  double t = ((1/2.0)*r[1][0]);

1 / 2 is an integer division and the result is 0.

1/2是整数除法,结果为0。

1 / 2.0 is a floating point division (with double values after the usual arithmetic conversions) and its result is 0.5.

1 / 2.0是浮点除法(在通常的算术转换后具有双值),其结果为0.5。

#3


5  

Because 1/2 is int/int division. That means whatever is the result will have anything after the decimal point removed (truncated). So 1/2 = 0.5 = 0.

因为1/2是int / int分区。这意味着无论什么结果将删除小数点后的任何内容(截断)。所以1/2 = 0.5 = 0。

Normally I always write the first number in double : 1.0/2 …..

通常我总是把第一个数字写成双倍:1.0 / 2 ......

If you make the very first number a double then all remaining calculation is done in double only.

如果您将第一个数字设为double,则所有剩余计算仅以double进行。

#4


2  

double t = r[1][0] * .5;

is equivalent to:

相当于:

double t = ((1/2f)*r[1][0]);

and not:

double t = ((1/2)*r[1][0]);

Due to loss of decimal part when the temporary result of 1/2 is stored in an int variable.

当1/2的临时结果存储在int变量中时,由于小数部分的丢失。

As a guideline whenever there is a division and there is a possibility of the answer being real number, do not use int or make one of the operands float or double or use cast.

作为指导,只要存在除法并且答案可能是实数,不要使用int或使其中一个操作数浮点或双精度或使用强制转换。

#5


1  

You can write 1.0/2.0 instead. 1/2 displays this behaviour because both the denominator and numerator act are of an integer type and a variable of an integer type divided by another variable of an integer type is always truncated to an integer.

你可以写1.0 / 2.0。 1/2显示此行为,因为分母和分子动作都是整数类型,整数类型的变量除以整数类型的另一个变量总是被截断为整数。

#6


0  

I cannot merit or demerit the standard of the question but this seem very critical issue to me. We assume that compiler will do the laundry for us all the time , but that is not true some times.

我不能指出或不符合问题的标准,但这对我来说似乎是非常关键的问题。我们假设编译器会一直为我们洗衣服,但有时候这不是真的。

Is there any way to avoid this situation ?

有没有办法避免这种情况?

Possibly

OR

More importantly knowing the monster (C,C++) as most of the people point out above

更重要的是,了解大多数人指出的怪物(C,C ++)

I would like to know if there are other ways to trace these "truncation" issues at compile time

我想知道是否有其他方法可以在编译时跟踪这些“截断”问题