> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/custom/package/geometrykit/metricspace.md).

# MetricSpace

[swift](/ios/swift.md) ⟩ [type](/ios/swift/type.md) ⟩ [custom](/ios/custom.md) ⟩ [package](/ios/custom/package.md) ⟩ [GeometryKit](/ios/custom/package/geometrykit.md) ⟩ MetricSpace&#x20;

{% hint style="info" %}
Protocol Inheritance Hierarchy

* <mark style="color:purple;">MetricSpace</mark> -> [Vector](/ios/custom/package/geometrykit/vector.md) -> [Vector2D](/ios/custom/package/geometrykit/vector2d.md) -> [ComplexNumber](/ios/custom/package/geometrykit/complexnumber.md)
  {% endhint %}

{% tabs %}
{% tab title="💾 程式" %}

```swift
// 2024.12.11 - 15:00

// Protocol Inheritance Hierarchy
// • MetricSpace -> Vector -> Vector2D -> ComplextNumber

// 🅿️ MetricSpace (protocol)
public protocol MetricSpace {
    
    // ⭐️ 純量 (scalar)
    // • FloatingPoint：支援 +,-,*,/
    // • Comparable   ：可比較大小（如需排序或限制範圍時）
    
    associatedtype Scalar: FloatingPoint & Comparable
    
    // ⭐️ requirement
    func distance(to other: Self) -> Scalar
}

// MetricSpace: default behavior
extension MetricSpace {
    
    // ⭐️ p.isAlmostEqual(to: q)
    func isAlmostEqual(
        
        to other: Self,
        
        // ⭐ 容許誤差的比較（Tolerance-based Comparison）
        // default tolerance = ULP of 1.0 * 1000
        // .ulpOne: ULP (Unit of Last Place) of 1.0.....0 <-- this place
        // • Double.ulpOfOne = 2.220446049250313e-16
        // • Float.ulpOfOne = 1.1920929e-07
        // • 在 64 位元平台上(現今大部分的 Apple 裝置)，CGFloat 與 Double 有相同精度
        tolerance: Scalar = Scalar.ulpOfOne * 1000
        
    ) -> Bool {
        return distance(to: other) < tolerance
    }
    
}
```

{% endtab %}

{% tab title="👥 相關" %}

* [Vector](/ios/custom/package/geometrykit/vector.md)：conforms to <mark style="color:purple;">MetricSpace</mark>
  {% endtab %}

{% tab title="📗 參考" %}

* ChatGPT ⟩ [設計 MetricSpace 協定](https://chatgpt.com/share/675916e7-e2e4-800e-9dbb-b5ca5f1cca2e)&#x20;
  {% endtab %}
  {% endtabs %}
