Re: %% with zero denominator
On 2017-12-11 5:21 PM, Sean McAfee wrote: Well, /any/ function or operator that returns a boolean /could/ return a Failure instead of (or in addition to) False to provide additional information to those who want it, but if the condition is not really a Failure, wouldn't that be misleading? Like this highly contrived example: For my part, in the design of my Muldis D programming language (whose design is influenced a lot by Perl 6), I use the data type name "Excuse" as my analogous core concept to Failure or the Exception of some languages. The idea is we have some context where a normal value doesn't exist, so the Excuse value gives the excuse for not having one. The singleton Excuse value named No_Reason is the canonical analogy to a generic unqualified null/nil/undef singleton typical in other languages. The Excuse value named Div_By_Zero is what would result from a number of mathematical operators, and there are others for common situations, and users can define more. The idea here is that an Excuse can either be a stored value such as how null is used in a SQL database, or it can be a result from an operation that doesn't give a normal result. An Excuse does compare equal to itself. -- Darren Duncan
Re: %% with zero denominator
On Mon, Dec 11, 2017 at 3:01 PM,wrote: > > On 2017-12-11 12:22 PM, Sean McAfee wrote: > >> Well, not really. I don't think x %% 0 should return a Failure at all. >> > > Is there a particular problem the current implementation fails to solve? > In boolean > context `x %% 0` *is* equivalent to False. The Failure carries additional > information > to those who want it. You can discard that information with `so x %% 0` if > you aren't > already evaluating in Bool context. Seems returning a Failure is the best > of both worlds. > > Well, *any* function or operator that returns a boolean *could* return a Failure instead of (or in addition to) False to provide additional information to those who want it, but if the condition is not really a Failure, wouldn't that be misleading? Like this highly contrived example: sub is-nonzero($x) { $x.Num / $x.Num } I feel that %% returning a Failure when given a zero second argument is similarly misleading/unhelpful. At least, if the intention of "a %% b" is simply to return whether a is divisible by b. If it's explicitly intended to mean just "a % b == 0" and inherit all of the failure modes of the % operator, then fine, I suppose. It seems more clunky to me, though. 1 / 0 is an expression which can evaluate to no sensible value > > It can produce a sensible value. In floating point context (that is in Num > view), that's a perfectly cromulent positive infinity, again per IEEE 754 > standard: > > m: say (1 / 0).Num > rakudo-moar fed56be25: OUTPUT: «Inf» > > To be perfectly cromulent I'd expect to be able to multiply that Inf by 0 and get my 1 back.
Re: %% with zero denominator
On 2017-12-11 12:22 PM, Sean McAfee wrote: Well, not really. I don't think x %% 0 should return a Failure at all. Is there a particular problem the current implementation fails to solve? In boolean context `x %% 0` *is* equivalent to False. The Failure carries additional information to those who want it. You can discard that information with `so x %% 0` if you aren't already evaluating in Bool context. Seems returning a Failure is the best of both worlds. We try to follow IEEE 754-2008 standard whenever we can and I have some memory of us verifying last year stuff like modulo was reasonably conforming. 1 / 0 is an expression which can evaluate to no sensible value It can produce a sensible value. In floating point context (that is in Num view), that's a perfectly cromulent positive infinity, again per IEEE 754 standard: m: say (1 / 0).Num rakudo-moar fed56be25: OUTPUT: «Inf» Cheers, ZZ
Re: %% with zero denominator
not what you think: module operator in % in perl6 is defined as $b - $a * floor($b / $a) On Mon, Dec 11, 2017 at 10:37 PM, Sean McAfeewrote: > On Mon, Dec 11, 2017 at 12:56 PM, Darren Duncan > wrote: > >> On 2017-12-11 12:22 PM, Sean McAfee wrote: >> >>> Well, not really. I don't think x %% 0 should return a Failure at all. >>> >>> 1 / 0 is an expression which can evaluate to no sensible value, so it >>> makes >>> sense to fail there. By the question "Is one divisible by zero?" has >>> the simple >>> answer "No." >>> >> >> I strongly disagree with you. >> >> First of all, the reason there is no sensible value is that the answer is >> BOTH "yes" and "no" at the same time, so you can't choose one. Zero DOES >> divide evenly into anything, and typically does so an infinite number of >> times. > > > I strongly disagree that the answer to "Is 1 divisible by 0?" can be "yes" > in any meaningful, commonly-understood sense. There is no number which one > can multiply by zero and get one, even an "infinite number," whatever that > means. The inputs to and output from the remainder operation are natural > numbers, or perhaps integers, and all natural numbers and integers are > finite. > >
Re: %% with zero denominator
Putting aside the edge case of what to do when the divisor is zero, which could also be tested for prior to attempting to call the operator: An "is evenly divisible by" operator is an immensely useful one to have built-in to the language; not only is "x %% y" much more direct to the real question than "(x % y) == 0)", it can also be optimized because we don't actually care what the quotient or remainder values are. This being similar to how asking "is this list empty" is more direct and optimizable than asking for the count of elements and comparing that to zero, and it also has meaning for list-like uncountable collections. -- Darren Duncan On 2017-12-11 1:02 PM, Vittore Scolari wrote: I think that this stems from a confusion between the divisibility problem in integer number (on a ring) and the divisibility problem resolved by the perl6 %% operator. Personally I think that %% is useless while the former is useful and missing. But I have nothing against useless operators On Mon, Dec 11, 2017 at 9:56 PM, Darren Duncan wrote: On 2017-12-11 12:22 PM, Sean McAfee wrote: Well, not really. I don't think x %% 0 should return a Failure at all. 1 / 0 is an expression which can evaluate to no sensible value, so it makes sense to fail there. By the question "Is one divisible by zero?" has the simple answer "No." I strongly disagree with you. First of all, the reason there is no sensible value is that the answer is BOTH "yes" and "no" at the same time, so you can't choose one. Zero DOES divide evenly into anything, and typically does so an infinite number of times. Bottom line, there is no good reason to answer either "yes" or "no" for zero. There are three distinct kinds of answers to the question "is x evenly divisible by y": 1. When dividing x by y there are no leftovers (yes). 2. When dividing x by y there are leftovers (no). 3. When dividing anything by zero there is no sensible value (Failure). It is very important to distinguish the above 3 cases. The main use case of %% is to gate logic where if 2 numbers do evenly divide we do some particular arithmetic with the results and if they don't but it is a valid division then we do other particular arithmetic with the results. The expression "x %% y" is to be equivalent to "(x % y) == 0)". -- Darren Duncan
Re: %% with zero denominator
I think that this stems from a confusion between the divisibility problem in integer number (on a ring) and the divisibility problem resolved by the perl6 %% operator. Personally I think that %% is useless while the former is useful and missing. But I have nothing against useless operators On Mon, Dec 11, 2017 at 9:56 PM, Darren Duncanwrote: > On 2017-12-11 12:22 PM, Sean McAfee wrote: > >> Well, not really. I don't think x %% 0 should return a Failure at all. >> >> 1 / 0 is an expression which can evaluate to no sensible value, so it >> makes >> sense to fail there. By the question "Is one divisible by zero?" has the >> simple >> answer "No." >> > > I strongly disagree with you. > > First of all, the reason there is no sensible value is that the answer is > BOTH "yes" and "no" at the same time, so you can't choose one. Zero DOES > divide evenly into anything, and typically does so an infinite number of > times. Bottom line, there is no good reason to answer either "yes" or "no" > for zero. > > There are three distinct kinds of answers to the question "is x evenly > divisible by y": > > 1. When dividing x by y there are no leftovers (yes). > 2. When dividing x by y there are leftovers (no). > 3. When dividing anything by zero there is no sensible value (Failure). > > It is very important to distinguish the above 3 cases. > > The main use case of %% is to gate logic where if 2 numbers do evenly > divide we do some particular arithmetic with the results and if they don't > but it is a valid division then we do other particular arithmetic with the > results. > > The expression "x %% y" is to be equivalent to "(x % y) == 0)". > > -- Darren Duncan >
Re: %% with zero denominator
On Mon, Dec 11, 2017 at 1:52 AM, Elizabeth Mattijsenwrote: > > On 11 Dec 2017, at 04:42, Sean McAfee wrote: > > The docs say a %% b is True if a % b is 0, so the error is as-designed, > at least. But mightn't it make more sense for %% to just return False when > given a second zero operand? After all, the answer to "is n divisible by > zero" is false for any n--there's no need to try to go through with the > division to ascertain this. > > Note that x %% 0 returns a Failure ( https://docs.perl6.org/type/Failure > ), which will be evaluated to False in a Boolean context. So it is already > doing what you want. Well, not really. I don't think x %% 0 should return a Failure at all. 1 / 0 is an expression which can evaluate to no sensible value, so it makes sense to fail there. By the question "Is one divisible by zero?" has the simple answer "No."
Re: %% with zero denominator
> On 11 Dec 2017, at 04:42, Sean McAfeewrote: > > I think of %% as being Perl 6's is-divisible-by operator, so I was a little > surprised to discover this behavior: > > > 1 %% 0 > Attempt to divide 1 by zero using infix:<%%> > in block at line 1 > > The docs say a %% b is True if a % b is 0, so the error is as-designed, at > least. But mightn't it make more sense for %% to just return False when > given a second zero operand? After all, the answer to "is n divisible by > zero" is false for any n--there's no need to try to go through with the > division to ascertain this. Note that x %% 0 returns a Failure ( https://docs.perl6.org/type/Failure ), which will be evaluated to False in a Boolean context. So it is already doing what you want. Only if you use it in a non-Boolean context, will the Exception of the Failure be thrown. say "foo" if 1 %% 0 # no output say "foo" unless 1 %% 0 # “foo” If you type in 1 %% 0 in the REPL, you are forcing evaluation of the Failure as a string because it will try to “say” it. say 1 %% 0 # Attempt to divide 1 by zero using infix:<%%> Hope this helps. Liz