Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maptiles represent latitude of 0 as an inaccurate float #156

Open
davidjb opened this issue Dec 2, 2024 · 0 comments
Open

Maptiles represent latitude of 0 as an inaccurate float #156

davidjb opened this issue Dec 2, 2024 · 0 comments

Comments

@davidjb
Copy link

davidjb commented Dec 2, 2024

When using maptile to compute tile bounds, if you try and compute a latitude for a tile that involves the equator, you'll end up with an inaccurate float as a result - instead of 0, you get -0.00000000000000038584785111248965 aka -3.8584785111248965e-16. Calculations that depends on this value then become inaccurate (e.g. 0 > -0.00000000000000038584785111248965 gives true) as do equality comparisons and so on.

Calculations involving the longitude don't appear affected and represent 0 accurately, and the resulting floating point for 0 always appears consistently at least. See below:

package main

import (
	"fmt"

	"github.com/paulmach/orb"
	"github.com/paulmach/orb/maptile"
)

func main() {
	fmt.Println("Tile below equator")
	for i := 1; i < 6; i++ {
		tile := maptile.At(orb.Point{0, 0}, maptile.Zoom(i))
		fmt.Print("Tile ", tile, "\n")
		fmt.Print("\t", tile.Bound(), "\n")
	}

	fmt.Println("\nTile above equator")
	for i := 1; i < 6; i++ {
		tile := maptile.At(orb.Point{0, 0}, maptile.Zoom(i))
		northTile := maptile.New(tile.X, tile.Y-1, tile.Z)
		fmt.Print("Tile ", northTile, "\n")
		fmt.Print("\t", northTile.Bound(), "\n")
	}
}
Tile below equator
Tile {1 1 1}
        {[0 -85.05112877980659] [180 -3.8584785111248965e-16]}
Tile {2 2 2}
        {[0 -66.51326044311186] [90 -3.8584785111248965e-16]}
Tile {4 4 3}
        {[0 -40.97989806962013] [45 -3.8584785111248965e-16]}
Tile {8 8 4}
        {[0 -21.943045533438166] [22.5 -3.8584785111248965e-16]}
Tile {16 16 5}
        {[0 -11.178401873711772] [11.25 -3.8584785111248965e-16]}

Tile above equator
Tile {1 0 1}
        {[0 -3.8584785111248965e-16] [180 85.0511287798066]}
Tile {2 1 2}
        {[0 -3.8584785111248965e-16] [90 66.51326044311185]}
Tile {4 3 3}
        {[0 -3.8584785111248965e-16] [45 40.97989806962013]}
Tile {8 7 4}
        {[0 -3.8584785111248965e-16] [22.5 21.94304553343818]}
Tile {16 15 5}
        {[0 -3.8584785111248965e-16] [11.25 11.178401873711785]}

Tested with orb v0.11.1 and Go 1.23.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant