TheAlgorithms/Go · error
mismatched dimensions
Error message
mismatched dimensions
What it means
Raised by EuclideanDistance when the two EuclideanPoint operands have different numbers of coordinates (len(p2) != len(p1)); distance in n-dimensional space is only defined between points of equal dimension.
Source
Thrown at math/geometry/distance.go:18
// distance.go
// Find Euclidean distance between two points
// time complexity: O(n) where n is the number of dimensions
// space complexity: O(1)
// author(s) [Chetan Patil](https://github.com/Chetan07j)
// Package geometry contains geometric algorithms
package geometry
import (
"errors"
"math"
)
// EuclideanPoint defines a point with x and y coordinates.
type EuclideanPoint []float64
var ErrDimMismatch = errors.New("mismatched dimensions")
// EuclideanDistance returns the Euclidean distance between points in
// any `n` dimensional Euclidean space.
func EuclideanDistance(p1 EuclideanPoint, p2 EuclideanPoint) (float64, error) {
n := len(p1)
if len(p2) != n {
return -1, ErrDimMismatch
}
var total float64 = 0
for i, x_i := range p1 {
// using Abs since the value could be negative but we require the magnitude
diff := math.Abs(x_i - p2[i])
total += diff * diff
}
View on GitHub (pinned to 5ba447ec5f)
Solutions
- Pad the shorter point with zeros to match dimensions before comparing
- Reject mismatched points upstream with a clear message about expected dimensionality
- Check len(p1)==len(p2) before calling
Defensive patterns
Strategy: type-guard
When it happens
Trigger: Thrown at math/geometry/distance.go:18 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02).
Data as JSON: /api/errors/27f93064096149c7.
Report an issue: GitHub.