{"record":{"id":"ab4aa563c44947fc","repo":"TheAlgorithms/JavaScript","slug":"cannot-normalize-vectors-of-length-0","errorCode":null,"errorMessage":"Cannot normalize vectors of length 0","messagePattern":"Cannot normalize vectors of length 0","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Data-Structures/Vectors/Vector2.js","lineNumber":55,"sourceCode":"\n  /**\n   * Vector length.\n   *\n   * @returns The length of the vector.\n   */\n  length() {\n    return Math.sqrt(this.x * this.x + this.y * this.y)\n  }\n\n  /**\n   * Normalization sets the vector to length 1 while maintaining its direction.\n   *\n   * @returns The normalized vector.\n   */\n  normalize() {\n    const length = this.length()\n    if (length === 0) {\n      throw new Error('Cannot normalize vectors of length 0')\n    }\n    return new Vector2(this.x / length, this.y / length)\n  }\n\n  /**\n   * Vector addition\n   *\n   * @param vector The vector to be added.\n   * @returns The sum-vector.\n   */\n  add(vector) {\n    const x = this.x + vector.x\n    const y = this.y + vector.y\n    return new Vector2(x, y)\n  }\n\n  /**\n   * Vector subtraction","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Data-Structures/Vectors/Vector2.js#L37-L73","documentation":"Thrown by Vector2.normalize() (plain Error) when the vector's length (magnitude sqrt(x*x + y*y)) is exactly 0, i.e. x === 0 && y === 0. Normalization divides by length, so a zero vector would divide by zero; the library refuses to produce a NaN/undefined direction.","triggerScenarios":"Calling normalize() on new Vector2(0, 0); on a vector obtained by subtracting two identical points; on a default/zero-initialized vector that was never assigned real components.","commonSituations":"Direction vectors computed from (b - a) where a === b; physics/animation where an object's velocity is zero at rest; sensors returning 0,0 during initialization.","solutions":["Check vector.length() === 0 (or x === 0 && y === 0) before normalize().","Return a default unit vector (e.g. (1,0)) instead of normalizing when the input is zero.","Validate that the source points are distinct before deriving a direction from their difference.","Add an epsilon check (length < 1e-12) if floating-point near-zero magnitudes are possible."],"exampleFix":"// before\nconst dir = v.normalize() // throws when v is (0,0)\n\n// after\nconst dir = v.length() === 0 ? new Vector2(1, 0) : v.normalize()","handlingStrategy":"validation","validationCode":"function safeNormalize(v, fallback = new Vector2(1, 0)) {\n  return v.length() === 0 ? fallback : v.normalize()\n}","typeGuard":"const isNonZeroVector = (v) => v.length() !== 0","tryCatchPattern":"try {\n  return v.normalize()\n} catch (e) {\n  if (e instanceof Error && /length 0/i.test(e.message)) return new Vector2(1, 0)\n  throw e\n}","preventionTips":["Check v.length() === 0 before normalize; return a default unit vector instead.","Validate source points are distinct before deriving a direction from their difference.","Use an epsilon (length < 1e-12) for floating-point near-zero magnitudes.","Initialize direction vectors to a real unit vector, never (0,0), until a real direction is known."],"tags":["math","vector","divide-by-zero","geometry"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}