C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Proposal to add the circle constant "tau" to the list of C++ mathematical constants

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Sat, 24 Aug 2024 13:34:22 -0400
On Sat, Aug 24, 2024 at 10:49 AM Thiago Macieira via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Saturday 24 August 2024 06:37:45 GMT-7 Phil Endecott via Std-Proposals
> wrote:
> > The paper does say, on page
> > 6, that "we don't
> > have to provide replacements for both M_PI and M_PI_2 and M_PI_4. The
> > user will be
> > able to divide the M_PI replacement by 2 and by 4 ...". But there isn't
> > any rationale
> > for why the provided constants should be limited only to those that the
> > user cannot
> > easily form from other constants. Does anyone recall the discussion?
>
> Divisions and multiplications by powers of 2 are exact in IEEE 754 binary
> floating points. There's no loss of precision in the process nor any
> precision
> lacking in the result.
>

Right. In base-10, there's at least a philosophical argument to be made
that "6.28..." might be slightly more precise than "2 * 3.14...".
But in base-2 floating-point, there's no precision difference at all: one
number simply has an exponent 1 greater than the other, and their mantissas
are bit-for-bit identical.
(Now, theoretically the C++ variable templates like `std::numbers::pi_v<T>`
might permit `T` to *be* a decimal type. But that can't be the main reason
to provide `tau`, because then we'd logically need to provide `M_PI / 3`,
`M_PI / 5`, etc., also, since they suffer the same tiny precision loss even
in base-2.)

That said, there is a (slight) ergonomic benefit to writing
    radius * std::numbers::tau
instead of
    radius * 2 * std::numbers::pi
and I do agree that *if* this number is provided for ergonomics, then it
should be named `std::numbers::tau`.

–Arthur

Received on 2024-08-24 17:34:35