JuliaArrays / JuliaArrays/StaticArrays.jl
Accuracy loss in `normalize`/`cross`
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 844
- Forks
- 159
- Avg merge
- 3d 21h
- Merged PRs (30d)
- 3
Description
Hi!
I've noticed a small loss in accuracy when computing mat * vec:
Without StaticArrays:
julia> lookMat = Float32[ -0.707107 0.707107 0.0 -0.0
-0.485071 -0.485071 0.727607 0.485071
0.514496 0.514496 0.685994 -3.42997
0.0 0.0 0.0 1.0]
4×4 Matrix{Float32}:
-0.707107 0.707107 0.0 -0.0
-0.485071 -0.485071 0.727607 0.485071
0.514496 0.514496 0.685994 -3.42997
0.0 0.0 0.0 1.0
julia> lookMat * Float32[0.5, 0.5, 0, 1]
4-element Vector{Float32}:
0.0
0.0
-2.9154742
1.0
With StaticArrays loaded (no matter whether it's Float32 or Float64, the accuracy loss is the same - note the nonzero y component):
julia> Array(lookMat)
4×4 Matrix{Float64}:
-0.707107 0.707107 0.0 -0.0
-0.485071 -0.485071 0.727607 0.485071
0.514496 0.514496 0.685994 -3.42997
0.0 0.0 0.0 1.0
julia> Array(lookMat) * [0.5, 0.5, 0, 1]
4-element Vector{Float64}:
0.0
2.9802322387695312e-8
-2.915476143360138
1.0
The matrix is generated by this function:
function lookAt(eye, target, up)
# up is local up of the camera
zaxis = -normalize(target - eye)
xaxis = normalize(cross(up, zaxis))
yaxis = cross(zaxis, xaxis)
res = @SMatrix Float32[
xaxis[1] xaxis[2] xaxis[3] -dot(eye, xaxis);
yaxis[1] yaxis[2] yaxis[3] -dot(eye, yaxis);
zaxis[1] zaxis[2] zaxis[3] -dot(eye, zaxis);
0 0 0 1
]
return res
end
What this does, in essence, is orient a coordinate system with origin at (2,2,2) to point at target in the -z direction. So lookMat * target should be exactly a vector in -z direction.
Accuracy loss of the StaticArray version aside, is it expected that this also happens for the regular Array multiplication?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the reported lookAt function and its normalize, cross, and matrix-vector multiplication calls. Reproduce the Julia examples with and without StaticArrays, then compare the Float32 and Float64 results. Done means determining whether the nonzero y component and precision difference are expected, and documenting or correcting the behavior as appropriate.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- performance
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100