{"record":{"id":"27f93064096149c7","repo":"TheAlgorithms/Go","slug":"mismatched-dimensions","errorCode":null,"errorMessage":"mismatched dimensions","messagePattern":"mismatched dimensions","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"math/geometry/distance.go","lineNumber":18,"sourceCode":"// distance.go\n// Find Euclidean distance between two points\n// time complexity: O(n) where n is the number of dimensions\n// space complexity: O(1)\n// author(s) [Chetan Patil](https://github.com/Chetan07j)\n\n// Package geometry contains geometric algorithms\npackage geometry\n\nimport (\n\t\"errors\"\n\t\"math\"\n)\n\n// EuclideanPoint defines a point with x and y coordinates.\ntype EuclideanPoint []float64\n\nvar ErrDimMismatch = errors.New(\"mismatched dimensions\")\n\n// EuclideanDistance returns the Euclidean distance between points in\n// any `n` dimensional Euclidean space.\nfunc EuclideanDistance(p1 EuclideanPoint, p2 EuclideanPoint) (float64, error) {\n\tn := len(p1)\n\n\tif len(p2) != n {\n\t\treturn -1, ErrDimMismatch\n\t}\n\n\tvar total float64 = 0\n\n\tfor i, x_i := range p1 {\n\t\t// using Abs since the value could be negative but we require the magnitude\n\t\tdiff := math.Abs(x_i - p2[i])\n\t\ttotal += diff * diff\n\t}\n","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/math/geometry/distance.go#L1-L36","documentation":"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.","triggerScenarios":"Thrown at math/geometry/distance.go:18 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","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"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}