HaloO,

Doug McNutt wrote:
Thinking about what I actually do. . .

A near equal test of a float ought to be a fractional error based on the current value of the float.

$x tested for between $a*(1.0 + $errorfraction) and $a*(1.0 - $errorfraction)

I strongly agree that checking relative errors in floating point
algorithms is ubiquitous and thus the creation of relative
ranges deserves a dedicated operator. So I wanted to make the
heretic proposal to use % for that. That is

    $x % $y;

to actually mean

    $x * (1 - $y/100) ..^ $x * (1 + $y/100);

I think that this reads good for numeric matches:

    if $x ~~ $y % 5 { say "within five percent" }

even though postfix % and infix ± read even better:

   if $x ~~ $y ± 5% { say "within five percent" }

and so I'm proposing to add these and 'within' as ASCII fallback
for ±.

The intended semantics could be achieved with postfix % returning
a Ratio type for which infix ± has an overloaded version that
creates a relative range as given above. Other operators that
don't have overloads for Ratio use it as a Num. This is much
easier than the closure generation with subsequent currying as
proposed by Larry elsewhere in this thread. This nicely allows

   $x += 5%;

to mean

   $x += $x * 0.05;

But defining the corresponding overloads of infix + is tricky
for infix:<+>:(Ratio,Num) because it could mean to increase the
Ratio and return a Ratio or ignore the Ratio type and return a
plain Num. I think the latter is saner.

BTW, in numerics one usually needs small values so we could have
a .ppm postfix operator that also returns a Ratio, albeit one
that is smaller by a factor of 10000. Alternatively a triple
application of % gives the same ratio. And of course we should
have ‰ (promille) and ‱ (permyriad) as well. And when we are at
it .ppb and .ppt are common as well. At least Wikipedia has them.
There one also finds that ratios are expressed with the SI prefixes
like nano without a unit or U for uno. But I guess at the latest
this all belongs into a Ratio (standard?) module.

Regards, TSa.
--

"The unavoidable price of reliability is simplicity" -- C.A.R. Hoare
"Simplicity does not precede complexity, but follows it." -- A.J. Perlis
1 + 2 + 3 + 4 + ... = -1/12  -- Srinivasa Ramanujan

Reply via email to