`_small_angle_of_2_vectors` loses accuracy for nearly parallel vectors (naive difference of products in Kahan's formula)
Mantenedores costumam responder em até 3 dias
Avaliação
- Dificuldade
- 2/5
- Tempo estimado
- 1-3 horas
- Facilidade para iniciantes
- 78/100
Direção de pesquisa
Leia uxarray/grid/utils.py para _small_angle_of_2_vectors e uxarray/utils/computing.py para o helper existente diff_of_products. Adicione testes para vetores quase paralelos, incluindo o caso reproduzível fornecido, e depois compare os resultados com ângulos precisos para as entradas float64. O trabalho estará concluído quando o erro para ângulos pequenos for reduzido a poucos ulps sem prejudicar os chamadores existentes.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
Version
v2026.09.1 (main at 88815a8c)
How did you install UXarray?
Source
What happened?
uxarray.grid.utils._small_angle_of_2_vectors loses accuracy for nearly parallel vectors. It implements Kahan's angle formula for vectors of arbitrary length,
angle = 2 * atan2(| v|u| - u|v| |, | v|u| + u|v| |)
but forms each component of v|u| - u|v| from two separately rounded products, i.e. a naive difference of two nearly equal products. The relative error grows as eps/angle: a mean of 2.5e-12 at 1e-5 rad, 2.4e-10 at 1e-7 rad, and 2.1e-8 at 1e-9 rad (200 random pairs of different lengths per angle, against the exact angle of the float64 inputs).
The function is used by grid/angles.py (face node angles), grid/point_in_face.py (winding number), and grid/coordinates.py (bounding circles). It currently has no tests. Found while reviewing #1806.
What did you expect to happen?
Kahan's formula should be accurate to a few ulps for small angles. Evaluating each component of v|u| - u|v| with the FMA-based diff_of_products (already in uxarray/utils/computing.py) brings the mean error down to below 1e-16 for angles down to 1e-7 rad.
Can you provide a MCVE to repoduce the bug?
import math
from fractions import Fraction
import numpy as np
from uxarray.grid.utils import _small_angle_of_2_vectors
# Two nearly parallel vectors in the xy-plane, with different lengths.
theta = 1e-9
u = np.array([3.7 * math.cos(0.4), 3.7 * math.sin(0.4), 0.0])
v = np.array([0.21 * math.cos(0.4 + theta), 0.21 * math.sin(0.4 + theta), 0.0])
# Exact angle between the float64 inputs: (u x v)_z and u . v in exact rationals.
ux, uy, vx, vy = (Fraction(float(c)) for c in (u[0], u[1], v[0], v[1]))
exact = math.atan(float((ux * vy - uy * vx) / (ux * vx + uy * vy)))
got = _small_angle_of_2_vectors(u, v)
print(f"exact = {exact:.17e}")
print(f"computed = {got:.17e}")
print(f"relative error = {abs(got - exact) / exact:.1e}")
Output on main:
exact = 1.00000003822873689e-09
computed = 1.00000004983169261e-09
relative error = 1.2e-08
- Linguagem predominante
- Python
- Estrelas
- 231
- Forks
- 58
- Merge médio
- 5d 7h
- PRs com merge (30d)
- 18
Preparar o ambiente
- Sem Dockerfile nem arquivo Docker Compose
- Tem um modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de UXARRAY/uxarray
-
ux.open_dataset currently supports structured dataTalvez já em andamento @Sevans711 assumiu há 3 dias. Abertabug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
UXARRAY/uxarray#1854 · 1 comentário · 1 responsável ·
Mantenedores costumam responder em até 3 dias
-
developer experience
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 90/100
Mantenedores costumam responder em até 3 dias
-
zonal_mean() and weighted_mean() should return the same type of objectTalvez livre de novo @Sevans711 assumiu há 41 dias e não há nenhum pull request aberto. Abertabug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
UXARRAY/uxarray#1733 · 1 responsável ·
Mantenedores costumam responder em até 3 dias
-
documentation
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 70/100
Mantenedores costumam responder em até 3 dias
-
Pre-commit bot update requires substantial code updatesTalvez já em andamento @cmdupuis3 assumiu há 3 dias. Abertadeveloper experience github_actions
UXARRAY/uxarray#1858 · 1 responsável ·
Mantenedores costumam responder em até 3 dias
Todas as issues de UXARRAY/uxarray
Issues semelhantes
-
good first issue test
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 85/100
ANCORATE/EncryptHome#8 ·
-
PluginProvider.storeInstance races and clobbers the dedup cache for concurrent loadsTalvez já em andamento @Wilson0511 assumiu hoje. Abertaneeds-triage
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 62/100
Mantenedores costumam responder em até 2 dias
-
Bug: failed Together chat calls do not record operation duration or error.typeTalvez já em andamento Um pull request vinculado a esta issue está aberto ou já foi mesclado. Aberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
Mantenedores costumam responder em até 1 dia
-
community documentation good first issue
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100