[Development] Should QVariant be doing fuzzy comparisons on doubles?
André Pönitz
apoenitz at t-online.de
Fri Sep 23 20:17:47 CEST 2016
On Fri, Sep 23, 2016 at 09:56:30AM +0000, Edward Welbourne wrote:
> Indeed; having all values of some type equivalent is worse (for the "has
> my value changed" test you illustrate, at least) than having a value not
> equal to itself (which triggers a "changed" event for a non-change). In
> fact one can have one's mathematical purity by attacking the "for all x"
> part of the above instead of the "x == x" part. Although it's usual to
> talk about "a relation is reflexive on S if, for all s in S, it relates
> s to s"
Right. That's pretty much the definition the known universe uses:
https://en.wikipedia.org/wiki/Reflexive_relation
(and no, *I* didn't put it there)
> one can equally define reflexive by "whenever the relation
> relates any x to y, it also relates x to x and y to y".
> (This is the
> more natural formulation in a mathematics based on relations [*] rather
> than sets.)
>
> [*] http://www.chaos.org.uk/~eddy/maths/found/relate.xhtml#Types
>
> With that formulation (which doesn't tie the relation to a set that it's
> "on"), returning false for the types that don't support equality
> comparison works; we won't consider any value of such a type equal to
> *anything*, so our relation isn't required to relate them to themselves.
> We are then left with QVariant::operator== being a relation formally on
> only those QVariants that support comparison, not on all QVariants
> (although it tolerates being asked about the values it doesn't relate to
> anything; it just says no about them).
How does that help with QHash<QVariant, ...> which needs comparsion of the
keys?
> It's not ideal (and the justification is heterodox) but false is a valid
> answer for the incomparable values,
This is a nonsensical line of argument long as you try to use it to counter
"unexpected" results.
With your "uncomparable things compare false" approach QHash<>::contains()
will never return true for uncomparable QVariants, i.e. code like
void doSomethingOnlyOncePerValue(QVariant v)
{
static QHash<QVariant, int> seen;
if (seen.contains(v))
return;
seen.insert(v, 0);
doSomething(v);
}
will be quite surprising, too.
Andre'
More information about the Development
mailing list