Skip to content

Maptiles represent latitude of 0 as an inaccurate float #156

@davidjb

Description

@davidjb

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions