我正在Unity 3D上编写C#脚本。我有两个Vector3
是相同的。当我这样做时:
Debug.Log(vect1);
Debug.Log(vect2);
我得到了相同的结果(500.0, 150.0, 0.0)
。问题在于,当我做vect1.Equals(vect2)
时,我得到了错误!怎么可能?
P.S。
我确信它们都是Vector3
,因为当我vect1.GetType()
和vect2.GetType()
时,我总是Vector3
。
答案 0 :(得分:4)
尽管是Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As LongPtr, _
ByVal hWnd2 As LongPtr, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String _
) As LongPtr
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As LongPtr, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any _
) As LongPtr
Private Declare PtrSafe Function SetTimer Lib "user32" _
(ByVal hwnd As LongPtr, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As LongPtr) As LongPtr
Private Declare PtrSafe Function KillTimer Lib "user32" _
(ByVal hwnd As LongPtr, _
ByVal nIDEvent As Long) As Long
,Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String _
) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any _
) As Long
Private Declare Function SetTimer Lib "user32" _
(ByVal hwnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" _
(ByVal hwnd As Long, _
ByVal nIDEvent As Long) As Long
通过身份比较实现struct
。换句话说,Vector3
如果它们是同一个实例,则只会等于Equals
。
但是,vect1
确实会实现vect2
来测试值的相等性,所以请改用它。
有关详细信息,请参阅https://msdn.microsoft.com/en-us/library/vstudio/ms128863%28v=vs.90%29.aspx。
答案 1 :(得分:1)
Vector3
overrides the ==
operator to"对于非常接近等于"的向量,返回true。由于您的浮点值可能存在一些难以察觉的差异,因此您可以尝试使用==
代替:
vect1 == vect2
答案 2 :(得分:1)
这就是为什么你可以看到会发生什么
Vector3 v1 = new Vector3(150.001f, 150.002f, 150.003f);
Vector3 v2 = new Vector3(150.002f, 150.003f, 150.004f);
Debug.Log(v1);
Debug.Log(v2);
Debug.Log(v1 == v2);
Debug.Log(v1.Equals(v2));
Debug.Log(Vector3.Distance(v1, v2) > 1e-3f);
这将打印出来
(150.0,150.0,150.0)
(150.0,150.0,150.0)
假
假
真
问题是你的足够接近的定义可能与团结的定义不同。您可以使用此功能检查
public static bool AlmostEqual(Vector3 v1, Vector3 v2, float tolerance)
{
return Mathf.Abs(Vector3.Distance(v1, v2)) <= tolerance;
}
答案 3 :(得分:-1)
大家好我告诉你我到目前为止是如何解决这个问题的。我已经成员进行成员比较。
public static <A, B, C> Stream<C> zip(
Stream<A> a,
Stream<B> b,
BiFunction<A, B, C> op) {
Iterator<A> i1 = a.iterator();
Iterator<B> i2 = b.iterator();
Iterable<C> i = () -> new Iterator<C>() {
@Override
public boolean hasNext() {
return i1.hasNext() && i2.hasNext();
}
@Override
public C next() {
return op.apply(i1.next(), i2.next());
}
};
// Not certain whether we can do this in parallel - probably not.
return StreamSupport.stream(i.spliterator(), false);
}
我知道它很冗长,但至少它有效,因为它是整数之间的简单比较......