Re: irrational nubmer?

2020-02-20 Thread Tobias Boege
On Thu, 20 Feb 2020, ToddAndMargo via perl6-users wrote:
> > > On Fri, 21 Feb 2020 at 13:31, ToddAndMargo via perl6-users
> > > mailto:perl6-users@perl.org>> wrote:
> > > 
> > > $ perl6 -e 'say sqrt(2).base-repeating();'
> > > No such method 'base-repeating' for invocant of type 'Num'
> > > in block  at -e line 1
> > > 
> 
> On 2020-02-20 19:07, Norman Gaywood wrote:
> > 
> > perl6 -e 'say sqrt(2).Rat.base-repeating();'
> > (1.4 
> > 14213197969543147208121827411167512690355329949238578680203045685279187817258883248730964467005076)
> 
> Hi Norman,
> 
> Much better!
> 
> Question: Rat:
> https://docs.raku.org/type/Rat
> 
>   Rat objects store rational numbers as a pair
>   of a numerator and denominator. Number literals
>   with a dot but without exponent produce Rats.
> 
> What does Rat do to sqrt(2) to give it a numerator
> and a denominator?
> 

I'm not sure if you have digested what others have said, so I'll repeat it:
sqrt(2) already has a numerator and denominator. sqrt(2) takes the integer
2 and computes its square root as a floating point number using magic IEEE
powder. If you look up the IEEE 754 float representations, you'll notice
that every (non-NaN, non-Inf) float is a rational number. So sqrt(2) is
already a rational number approximating the positive real number whose
square is 2.

That said, just taking the obvious numerator and denominator from the float
might give you unnecessarily large numbers, and the Rat conversion on Num
has to support a user-supplied tolerance as well. Reading the documentation,
it seems that Raku does not require IEEE 754 to be in effect at all.

So what Rakudo does (I recommend reading the source code [1]) is a variant
of finding a continued fraction for the Num [2], except that it computes
the Rat instead and stops when it reached the requested accuracy. It starts
from scratch and iteratively approximates the Num, which has the advantage
that you can stop when you don't have to continue and that it only relies
on arithmetic on the Num working, not that it is represented according to
some standard internally.

Note also that Num.Rat supports a :fat argument to give you a FatRat back,
but you cannot actually increase the precision of your rational approximation
of √2 beyond the source you put in, which is a double precision float:

  > sqrt(2).Rat.nude # precision 1e-6, the default
  (1393 985)
  > sqrt(2).Rat(1e-8).nude   # a bit better requested, request granted
  (19601 13860)
  > sqrt(2).Rat(1e-16).nude  # still better but 1e-16 is ca. the limit
  (131836323 93222358)

  > sqrt(2).Rat(1e-20).nude  # no improvement
  (131836323 93222358)
  > sqrt(2).Rat(1e-100, :fat).nude  # fatter but not better, because
  (131836323 93222358)  # the source sqrt(2) is too lossy

Regards,
Tobias

[1] https://github.com/rakudo/rakudo/blob/cdbd60c1/src/core.c/Num.pm6#L46
[2] 
https://en.wikipedia.org/wiki/Continued_fraction#Calculating_continued_fraction_representations

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk


Re: irrational nubmer?

2020-02-20 Thread ToddAndMargo via perl6-users
On Fri, 21 Feb 2020 at 13:31, ToddAndMargo via perl6-users 
mailto:perl6-users@perl.org>> wrote:


$ perl6 -e 'say sqrt(2).base-repeating();'
No such method 'base-repeating' for invocant of type 'Num'
in block  at -e line 1



On 2020-02-20 19:07, Norman Gaywood wrote:


perl6 -e 'say sqrt(2).Rat.base-repeating();'
(1.4 
14213197969543147208121827411167512690355329949238578680203045685279187817258883248730964467005076)


Hi Norman,

Much better!

Question: Rat:
https://docs.raku.org/type/Rat

  Rat objects store rational numbers as a pair
  of a numerator and denominator. Number literals
  with a dot but without exponent produce Rats.

What does Rat do to sqrt(2) to give it a numerator
and a denominator?

And I see base-repeating wants to be fed a Rational:
https://docs.raku.org/routine/base-repeating

Also I much prefer:
$ perl6 -e 'say <1/7>.base-repeating();'
(0. 142857)
over
$ perl6 -e 'say (1/7).base-repeating();'
(0. 142857)

As to me (1/7) means solve the equation and then
send it on.

Also, what is base-repeating telling me?  Where the
repeat is?  What the repeat is?

And what if I only want to see if a repeat occurs
in a single number, not a Rat/Rational?

$ perl6 -e 'say <0.9876123412341234/1>.base-repeating();'
(0.9876123412341234 )

I am doing something wrong.

Many thanks,
-T


Re: irrational nubmer?

2020-02-20 Thread Norman Gaywood
On Fri, 21 Feb 2020 at 13:31, ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> $ perl6 -e 'say sqrt(2).base-repeating();'
> No such method 'base-repeating' for invocant of type 'Num'
>in block  at -e line 1
>

perl6 -e 'say sqrt(2).Rat.base-repeating();'
(1.4
14213197969543147208121827411167512690355329949238578680203045685279187817258883248730964467005076)

-- 
Norman Gaywood, Computer Systems Officer
School of Science and Technology
University of New England
Armidale NSW 2351, Australia

ngayw...@une.edu.au  http://turing.une.edu.au/~ngaywood
Phone: +61 (0)2 6773 2412  Mobile: +61 (0)4 7862 0062

Please avoid sending me Word or Power Point attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html


Re: irrational nubmer?

2020-02-20 Thread ToddAndMargo via perl6-users

On 2020-02-20 16:27, William Michels via perl6-users wrote:

mbook:~ homedir$ perl6 -e 'say (1/7).base-repeating();'
(0. 142857)
mbook:~ homedir$ perl6 -e 'say (1/7).base-repeating(10);'
(0. 142857)
mbook:~ homedir$ perl6 -e 'say (1/7).base-repeating(10).perl;'
("0.", "142857")
mbook:~ homedir$ perl6 -e 'say (5/2).base-repeating(10).perl;'
("2.5", "")
mbook:~ homedir$


Hi William,

   I love it!  Thank you!

-T


$ perl6 -e 'say (1/7).base-repeating();'
(0. 142857)



Oh now this ain't fair!

$ perl6 -e 'say sqrt(2).base-repeating();'
No such method 'base-repeating' for invocant of type 'Num'
  in block  at -e line 1


Re: irrational nubmer?

2020-02-20 Thread William Michels via perl6-users
On Thu, Feb 20, 2020 at 2:25 PM ToddAndMargo via perl6-users
 wrote:
>
> On 2020-02-19 23:21, Shlomi Fish wrote:
> > Hi Paul,
> >
>
> > Well, it is not unthinkable that a
> > https://en.wikipedia.org/wiki/Computer_algebra_system (CAS)-like system 
> > will be
> > able to tell that the abstract number sqrt(2) is irrational, as well as some
> > derivative numbers such as 3 + sqrt(2). E.g:
>
> Hi Shlomi,
>
> Those "academic exercises" where enterprising college
> students run pi out to a bazillion digits to see if they
> can find a repeating patterns came up with some
> way of handling an "unbounded" number.  A "Real" (Cap R)
> perhaps?
>
> -T

You can identify repeating patterns in decimal fractions using the
method "base-repeating":

mbook:~ homedir$ perl6 -e 'say (1/7).base-repeating();'
(0. 142857)
mbook:~ homedir$ perl6 -e 'say (1/7).base-repeating(10);'
(0. 142857)
mbook:~ homedir$ perl6 -e 'say (1/7).base-repeating(10).perl;'
("0.", "142857")
mbook:~ homedir$ perl6 -e 'say (5/2).base-repeating(10).perl;'
("2.5", "")
mbook:~ homedir$

According to the Raku docs, "If no repetition occurs, the second
string is empty... ."
And the docs say: "The precision for determining the repeating group
is limited to 1000 characters, above that, the second string is ???."
https://docs.raku.org/type/Rational#method_base-repeating

HTH, Bill.

PS For those following along at home--unless it's been added since
Rakudo 2019.07--I don't see the "is_irrational()" function in the Raku
language referred to by Shlomi Fish. Or maybe I/we was/were to
understand that there isn't an "is_irrational()" function in the Raku
language as of yet.

https://stackoverflow.com/questions/42302488/identify-a-irrational-or-complex-number
https://mathoverflow.net/questions/91915/detecting-recognizing-irrational-number-by-computers
https://reference.wolfram.com/language/guide/ContinuedFractionsAndRationalApproximations.html
https://www.wolframalpha.com/examples/mathematics/numbers/irrational-numbers/
https://www.wolframalpha.com/examples/mathematics/number-theory/continued-fractions/


Re: irrational nubmer?

2020-02-20 Thread ToddAndMargo via perl6-users

On 2020-02-19 23:21, Shlomi Fish wrote:

Hi Paul,




Well, it is not unthinkable that a
https://en.wikipedia.org/wiki/Computer_algebra_system (CAS)-like system will be
able to tell that the abstract number sqrt(2) is irrational, as well as some
derivative numbers such as 3 + sqrt(2). E.g:


Hi Shlomi,

Those "academic exercises" where enterprising college
students run pi out to a bazillion digits to see if they
can find a repeating patterns came up with some
way of handling an "unbounded" number.  A "Real" (Cap R)
perhaps?

-T


Re: irrational nubmer?

2020-02-20 Thread ToddAndMargo via perl6-users

On 2020-02-20 00:41, Darren Duncan wrote:

On 2020-02-20 12:10 a.m., Tobias Boege wrote:

Granted, Todd would not have anticipated this answer if he calls
arbitrary length integers "magic powder" and the question "I have
computed this Int/Num/Rat in Raku, is it rational?" does indeed
not make any sense.  But there are computer languages that can do
better.  Given FatRats, such modules can be written for Raku today.


Actually the question "is it rational" DOES make sense, however its 
answer is trivial, the answer is always "yes"; EVERY (not-NaN/Inf/etc) 
number a language-defined Raku data type can represent is exactly 
expressible as the ratio of 2 integers. -- Darren Duncan


Well, I wonder if there is an overflow bit that would
tell your is the number was going on and on after
you did an operation on it.


Re: Rational numbers... was Re: irrational nubmer?

2020-02-20 Thread ToddAndMargo via perl6-users

On 2020-02-20 05:53, Richard Hainsworth wrote:
However, my question to you is: when would you come across an irrational 
number in a computer? How would you express it? Suppose I gave you a 
function  sub irrational( $x ) which returns true for an irrational 
number. What would you put in for $x? Bear in mind that anything like pi 
or sqrt(2) is either going to be infinitely long or a rational 
approximation.


Hi Richard,

The question was meant as trivia.  Raku never ceased
to amaze me in its capabilities, so I though it had
come up with some elegant/impressive way (also
known as Magic Larry Powder) of handling it.
And yes, UInt and Int do fall into that category
with me.

On the practical side, I am an engineer and what
I am interested is the "tolerance" of a number.
For example, 1.0 and 1.000 are not the same
number.  The first one is 1.0 ± 0.05 and the
second one is 1.000 ± 0.0005.  And when
doing math on such numbers, the tolerance
of the result always takes on the worst tolerance
of the numbers being manipulated.

So it is the square root of two taken to
the length of the tolerance of the other
variables.

So rational or irrational has no practical
meaning to me.  And why the rounding in Raku
is so adored.

Oh and if I wanted to run the trivia up the flag
pole, I'd remark on the difference between
common rounding and scientific rounding.  Scientific
routing is actually the correct way, as it is
balanced, but few understand what it is so using
common rounding keeps you on their good side.

-T


Re: irrational nubmer?

2020-02-20 Thread Paul Procacci
Every system that uses a fixed finite number of bits to represent numbers
has to represent them as implicit rationals...that is
unless it goes through the trouble of having a finite list of irrational
constants that it represented specially.

sqrt is not equivalent to the mathematical definition of √.  It's an
approximation of the latter.
Most computer languages except for CAS work this way.

Truth by told, a lot of what I'm saying is general knowledge and shouldn't
be news to anyone.
Somewhat kiddingly, Raku should introduce the LazilyComputableReal datatype
which returns True to maybe_irrational ... until it isn't.  ;)


On Thu, Feb 20, 2020 at 2:21 AM Shlomi Fish  wrote:

> Hi Paul,
>
> On Thu, 20 Feb 2020 00:22:34 -0500
> Paul Procacci  wrote:
>
> > If you wouldn't mind, please stop referring things as being "magical".
> > There's nothing magical about Raku/Perl6 other than the devs that put in
> > their time to give you that perception.
> > They are to be commended for their time and effort.
> >
> > Also, being condescending as in "he gave up" is uncalled for.
> >
> > It's an IMPOSSIBILITY using today's technology to tell you whether a
> number
> > is irrationalperiod.
>
> Well, it is not unthinkable that a
> https://en.wikipedia.org/wiki/Computer_algebra_system (CAS)-like system
> will be
> able to tell that the abstract number sqrt(2) is irrational, as well as
> some
> derivative numbers such as 3 + sqrt(2). E.g:
>
> ```
> > my $num = sqrt(2);
> > say $num.is_irrational()
> True
> ```
>
> It won't be able to give its exact value, but may still be able to reason
> about
> it.
>
> --
>
> Shlomi Fish   https://www.shlomifish.org/
> Perl Elems to Avoid - https://perl-begin.org/tutorials/bad-elements/
>
> The cool thing about Vim is — you find something interesting with every
> typo.
> — Su‐Shee on Freenode’s #perl .
>
> Please reply to list if it's a mailing list post - http://shlom.in/reply .
>


-- 
__

:(){ :|:& };:


Rational numbers... was Re: irrational nubmer?

2020-02-20 Thread Richard Hainsworth
Hi Todd,

This is going to be hard for an intuitive guy like you, but it can be
proven that 100% of all numbers are irrational (see
https://math.stackexchange.com/questions/1556670/100-of-the-real-numbers-between-0-and-1-are-irrational
).

Except the ones that a computer can do operations on, which are by
definition Rational, as others in the list have already said.

The apparent paradox between these statements arises because of the logic
of infinities.

However, my question to you is: when would you come across an irrational
number in a computer? How would you express it? Suppose I gave you a
function  sub irrational( $x ) which returns true for an irrational number.
What would you put in for $x? Bear in mind that anything like pi or sqrt(2)
is either going to be infinitely long or a rational approximation.

So I could argue that raku already has a function irrational. It is called
False.

Regards,
Richard

On Thu, Feb 20, 2020, 02:58 ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> Hi All,
>
> This is a complete trivia question.
>
> Is there a test to see if a number is irrational,
> such as the square root of two?
>
> And how does Int handle a irrational number?  Is
> there a limit to magic Larry powder?
>
> Many thanks,
> -T
>


Re: irrational nubmer?

2020-02-20 Thread Darren Duncan

On 2020-02-20 12:10 a.m., Tobias Boege wrote:

Granted, Todd would not have anticipated this answer if he calls
arbitrary length integers "magic powder" and the question "I have
computed this Int/Num/Rat in Raku, is it rational?" does indeed
not make any sense.  But there are computer languages that can do
better.  Given FatRats, such modules can be written for Raku today.


Actually the question "is it rational" DOES make sense, however its answer is 
trivial, the answer is always "yes"; EVERY (not-NaN/Inf/etc) number a 
language-defined Raku data type can represent is exactly expressible as the 
ratio of 2 integers. -- Darren Duncan


Re: irrational nubmer?

2020-02-20 Thread Tobias Boege
On Wed, 19 Feb 2020, Paul Procacci wrote:
>  >> Is there a test to see if a number is irrational
> There is no such thing as an irrational number in computing.
> 
> Surely there are "close approximations", but that's the best any computer
> language can currently do.
> 

It all depends on representation.  Of course there are ways to write
algebraic numbers as their minimal polynomials over the rationals.
You may not call that a "number", but you can do effective arithmetic
with such a representation, so for many purposes it's as good as a
"chunk of bits" number and it is an exact symbolic representation of
a (possibly irrational) number from which you can extract rational
approximations of any wanted precision.  And in that representation,
a number is irrational if and only if the minimal polynomial has
degree at least two, very easy to check.  (Note, however, that you
do not get all irrationals in this way. π and e are still out of
reach, for example.)

Granted, Todd would not have anticipated this answer if he calls
arbitrary length integers "magic powder" and the question "I have
computed this Int/Num/Rat in Raku, is it rational?" does indeed
not make any sense.  But there are computer languages that can do
better.  Given FatRats, such modules can be written for Raku today.

There seems to be a slogan of some sort that, if you search IRC logs
or the documentation, pops up a number of times: "Perl 6 (Raku) is not
a computer algebra system" ("RINACAS"?).  I don't know what happened
in the history of Perl 6 development, but it seems that irrationals
were sought in core at some point and it was Larry who stopped that
motion [1].

Regards,
Tobias

[1] https://github.com/Raku/problem-solving/issues/4#issuecomment-474892811

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk