lcm(0,n) gives 0 which should be changed.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

lcm(0,n) gives 0 which should be changed.

Dildar Sk
In MATLAB,
>> lcm(0,1)
Error using lcm (line 16)
Input arguments must contain positive integers.
 
>> lcm(0,11)
Error using lcm (line 16)
Input arguments must contain positive integers.

So, MATLAB chooses to make produce error for other numbers than Integer.

In Octave,

>> lcm(0,1)
ans = 0
>> lcm(0,10)
ans = 0

So Octave gives lcm(0,n) 0. So like this any number x,y should produce lcm
to be 0.
lcm(x,y) = 0 will be always true in this sense. And lcm(x,y) is divisible by
both x and y but we know 0/0 undefined or NaN(Octave uses). Please correct
me so that I can learn.



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-Maintainers-f1638794.html

Reply | Threaded
Open this post in threaded view
|

Re: lcm(0,n) gives 0 which should be changed.

Jordi Gutiérrez Hermoso-2
On Sat, 2018-03-24 at 10:29 -0700, Dildar Sk wrote:
> > > lcm(0,10)
>
> ans = 0

I don't think this is an error and I think Matlab is being excessively
restrictive.

0 is indeed the least common multiple of 0 and 10, since you can
multiply 0 and 10 by something and get 0, and 0 is the smallest number
that can be a multiple of both.

I suppose you can argue that 0 is always the lcm of any two
nonnegative integers and the output of lcm should positive to rule out
0. I think it makes more sense to return something here since one of
the inputs is zero, instead of erroring out.

In general, Matlab considering some inputs to be an error when Octave
does not is acceptable. For example, Octave can handle the Gaussian
integers:

    gcd (7+6i, 6-2i)
    ans = -1 + 2i

I belive Matlab does not accept complex arguments for gcd.


Reply | Threaded
Open this post in threaded view
|

Re: lcm(0,n) gives 0 which should be changed.

Dildar Sk
Jordi Gutiérrez Hermoso-2 wrote
> I suppose you can argue that 0 is always the lcm of any two
> nonnegative integers and the output of lcm should positive to rule out
> 0. I think it makes more sense to return something here since one of
> the inputs is zero, instead of erroring out.

Forgive me, but lcm of x,y lcm(x,y) must be divisible by x and y.
But if lcm(0,n) produces 0 that means 0 is divisible by 0.But
0/0 is undefined.



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-Maintainers-f1638794.html

Reply | Threaded
Open this post in threaded view
|

Re: lcm(0,n) gives 0 which should be changed.

Marco Caliari-4
On Sun, 25 Mar 2018 22:48:15 -0700 (MST)
Dildar Sk <[hidden email]> wrote:

> Jordi Gutiérrez Hermoso-2 wrote
> > I suppose you can argue that 0 is always the lcm of any two
> > nonnegative integers and the output of lcm should positive to rule out
> > 0. I think it makes more sense to return something here since one of
> > the inputs is zero, instead of erroring out.  
>
> Forgive me, but lcm of x,y lcm(x,y) must be divisible by x and y.
> But if lcm(0,n) produces 0 that means 0 is divisible by 0.But
> 0/0 is undefined.

If we stay with the definition, clearly lcm(0,something) is not defined. But
Euclid's algorithm is used: lcm(a,b) = a*b/gcd(a,b). With this algorithm, I
think it is safe to extend the definition to lcm(0,something!=0) and get zero
as result. Then, in the current implementation there is already the
idea to set explicitely to 0 the result for lcm(0,0). This happens, but there
is a warning about division by zero. Finally, current implementation handles
also negative numbers and the result is negative if the inputs have opposite
sign. Here, I would change the definition to lcm(a,b)=|a*b|/gcd(a,b) and write
that we use this definition in the documentation.

Both wolframalpha and SageMath give:

lcm(0,3) = 3
lcm(0,-3) = 0 (the positive 0 in SageMath, not sure in wolframalpha)
lcm(0,0) = 0
lcm(1,-3) = 3
lcm(-1,-3) = 3

without any warning.

Marco

Reply | Threaded
Open this post in threaded view
|

Re: lcm(0,n) gives 0 which should be changed.

Jordi Gutiérrez Hermoso-2
In reply to this post by Dildar Sk
On Sun, 2018-03-25 at 22:48 -0700, Dildar Sk wrote:
> Forgive me, but lcm of x,y lcm(x,y) must be divisible by x and y.

You can define divisibility without using division. The integer X is
divisible by Y if there exists an integer W such that X = WY. By this
definition, 0 is divisible by 0, since any real W is a witness to this
divisibility.