Vector: IsParallel now works.

There should be a devlog entry shortly describing this, but the
code for IsParallel wasn't working on arm64 (Linux or Darwin).
Floating point math is weird.
This commit is contained in:
Kyle Isom 2023-10-22 15:18:40 -07:00
parent 8868fe40a1
commit 7f0a814b3f
2 changed files with 22 additions and 7 deletions

View File

@ -7,6 +7,9 @@
<option name="FUNCTION_BRACE_PLACEMENT" value="2" />
<option name="FUNCTION_TOP_AFTER_RETURN_TYPE_WRAP" value="2" />
</Objective-C>
<clangFormatSettings>
<option name="ENABLED" value="true" />
</clangFormatSettings>
<files>
<extensions>
<pair source="cc" header="h" fileNamingConvention="PASCAL_CASE" />

View File

@ -193,8 +193,8 @@ public:
T
Angle(const Vector<T, N> &other) const
{
Vector<T, N> unitA = this->UnitVector();
Vector<T, N> unitB = other.UnitVector();
auto unitA = this->UnitVector();
auto unitB = other.UnitVector();
// Can't compute angles with a zero vector.
assert(!this->IsZero());
@ -214,12 +214,24 @@ public:
return true;
}
T angle = this->Angle(other);
if (scmp::WithinTolerance(angle, (T) 0.0, this->epsilon)) {
return true;
}
// If the two unit vectors are equal, the two vectors
// lie on the same path.
//
// Context: this used to use Vector::Angle to check for
// a zero angle between the two. However, the vagaries
// of floating point math meant that while this worked
// fine on Linux amd64 builds, it failed on Linux arm64
// and MacOS builds. Parallel float vectors would have
// an angle of ~0.0003 radians, while double vectors
// would have an angle of +NaN. I suspect this is due to
// tiny variations in floating point math, such that a dot
// product of unit vectors would be just a hair over 1,
// e.g. 1.000000001 - which would still fall outside the
// domain of acos.
auto unitA = this->UnitVector();
auto unitB = other.UnitVector();
return false;
return unitA == unitB;
}