mirror of
https://github.com/felt/tippecanoe.git
synced 2026-10-02 16:35:40 +02:00
Tippecanoe formatted every double it wrote through milo::dtoa_milo, a vendored Grisu2. Grisu2 is fast, but it guarantees neither the shortest digit string nor the correctly rounded one: it only guarantees that what it prints parses back to the value it came from. In practice it prints a digit more than necessary about 0.16% of the time, and picks a neighbor of the correctly rounded digits about 32% of the time. This ports Russ Cox's fpfmt (https://github.com/rsc/fpfmt) to C++ in fpfmt/ and formats through it instead. fpfmt is both shortest and correctly rounded, and it is faster: full std::string formatting Grisu2 fpfmt speedup random bit patterns 156.62 ns 66.83 ns 2.34x geo coordinates 124.07 ns 58.62 ns 2.12x short decimals 69.37 ns 49.16 ns 1.41x small integers 44.18 ns 38.06 ns 1.16x digit generation only Grisu2 fpfmt speedup random bit patterns 90.07 ns 20.81 ns 4.33x geo coordinates 80.64 ns 20.18 ns 4.00x short decimals 55.61 ns 21.90 ns 2.54x small integers 40.23 ns 22.50 ns 1.79x (Intel Xeon @ 2.80GHz, g++ 13.3 -O3. `make fpfmt-bench` reproduces this, and `./fpfmt-bench -check` reruns the correctness sweep, which is why milo/dtoa_milo.h is kept even though nothing links it any more.) The port is deliberately literal, so it can be diffed against fpfmt.go. Its Short() agrees bit for bit with the Go original's on 445,640 values covering powers of ten, small integers and reciprocals, subnormals, and random bit patterns. Over 38.5 million values, fpfmt::dtoa always round trips, is never longer than Grisu2's output, and is shorter 61,329 times. Output is otherwise formatted exactly as before, including the choice between plain and exponential notation, so 26 expected test outputs change: some numbers lose digits (-26.170044999999999 becomes -26.170045), and some have a corrected final digit (9.823748927348929e+55 becomes 9.823748927348928e+55). Every changed token was checked to parse back to the identical double; none of the values themselves moved. milo/milo.h, whose only job was to declare the C shim jsonpull calls, is replaced by fpfmt/fpfmt.h, and the shim is renamed dtoa_shortest. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014wJRAuhMninQE4wK2TUfuZ
39 lines
1.4 KiB
C++
39 lines
1.4 KiB
C++
// C++ port of Russ Cox's fpfmt shortest-float formatting algorithm,
|
|
// from https://github.com/rsc/fpfmt (fpfmt.go).
|
|
//
|
|
// The original Go code is Copyright 2025 The Go Authors and is covered by
|
|
// the BSD-style license in fpfmt/LICENSE.txt.
|
|
//
|
|
// fpfmt::dtoa() is a drop-in replacement for milo::dtoa_milo(): it produces
|
|
// the same output format (JSON-ish shortest round-trippable decimal, with
|
|
// "nan", "inf", "-inf" for the non-finite cases), but always chooses the
|
|
// genuinely shortest digit string, where Grisu2 sometimes emits one digit
|
|
// more than necessary.
|
|
|
|
#pragma once
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string>
|
|
|
|
namespace fpfmt {
|
|
|
|
// shortest computes the shortest decimal d * 10**p that round-trips back to f.
|
|
// The caller must have already excluded 0, NaN, and ±Inf. The sign of f is
|
|
// ignored; the magnitude is what is formatted.
|
|
void shortest(double f, uint64_t *d, int *p);
|
|
|
|
// digits returns the number of decimal digits in d (d must be nonzero).
|
|
int digits(uint64_t d);
|
|
|
|
// format writes the milo-compatible rendering of (negative ? -1 : 1) * d * 10**p
|
|
// into buf, which must have room for at least 32 bytes, and returns the number
|
|
// of bytes written. nd must be digits(d).
|
|
int format(char *buf, uint64_t d, int p, int nd, bool negative);
|
|
|
|
// dtoa formats value the way milo::dtoa_milo() did, but using the shortest
|
|
// possible digit string.
|
|
std::string dtoa(double value);
|
|
|
|
} // namespace fpfmt
|