C++ Logo


Advanced search

Re: new approximately equal operators

From: Andrew Tomazos <andrewtomazos_at_[hidden]>
Date: Sun, 21 Jun 2020 18:09:58 +1000
If you're interested in working in this space, I'd start by publishing a
survey of some popular existing C++ libraries that provide floating-point
approximately equal functionality - and carefully analyzing and comparing
how they do it and how they are used. Next step would be to propose this
functionality into the standard library (std::approx_equal or similar),
probably initially to the Numerics subgroup (SG6). Once the standard
library has the functionality I can imagine a proposal to provide operators
for it - but the latter is unlikely to be accepted because new C++
operators have to meet an insanely high threshold of interest for some
reason. (Amusing to compare other languages like Scala or Perl 6 - where
arbitrary new operators can be user-defined like functions)

On Tue, Jun 9, 2020 at 5:55 AM Vishal Oza via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> I was wondering if there is any interest in adding approximately equal to
> operators into the language. The main purpose is to compare floating point
> calculations with rounding error, however they could be used for similarity
> of objects or fixed point rounded values as well.
> the operator I propose are: ~=, ~< , ~>, ~!
> these operators would translate at least to floating point as
> a ~= b ==> abs(a-b) < epsilon
> a ~< b ==> (a < b) || (abs(a-b) < epsilon)
> a ~> b ==> (a > b) || (abs(a-b) < epsilon)
> a ~! b ==> abs(a-b) >= epsilon
> There are two routes I can see with default implementation this language
> feature. either adding a default epsilon into the language that can be set
> as a global variable or adding some way of tracking errors. I would like
> more feedback on either way of default implementation.
> I would also like a way to specify the epsilon value or function for a
> single comparison. the best I can think of is
> a ~=b(c) ==> abs(a-b) < c
> a ~=b:c ==> abs(a-b) < c
> [c]a ~=b ==> abs(a-b) < c
> The precedence should be on the same level of all of the current
> comparison operators.
> This could be introduced in C++ 23 or later in there is any interate.
> Sincerely,
> Vishal Oza
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2020-06-21 03:13:21