Re: Math::BigInt and bigint with non-integers

[email protected] (Dan Book) Thu, 19 Aug 2021 10:40:22 -0400
Newsgroups perl.module-authors
Message-ID <CABMkAVXwjYA7+md_Vb9k0JGkMNGmmTQArcnRDRwHOH=iDxykEA@mail.gmail.com>
--000000000000731aa005c9ea8877
Content-Type: text/plain; charset="UTF-8"

On Thu, Aug 19, 2021 at 4:18 AM Peter John Acklam <[email protected]>
wrote:

> Hi!
>
> I would like some input on how the Math::BigInt module and bigint pragma
> should handle non-integers. The current behaviour is rather inconsistent.
>
> The new() constructor converts a non-integer to a Math::BigInt NaN:
>
>     $ perl -MMath::BigInt -wle 'print Math::BigInt -> new("3.16")'
>     NaN
>
> A math operation that returns a non-integer, returns a Math::BigInt with
> the
> truncated value:
>
>     $ perl -MMath::BigInt -wle 'print Math::BigInt -> new("10") -> bsqrt()'
>     3
>
> Math::BigInt with overloading of constants, leave a non-integer as an
> unmodified Perl scalar:
>
>     $ perl -MMath::BigInt=:constant -wle 'print 3.16'
>     3.16
>
> However, when the "bigint" pragma is used for overloading constants, a
> non-integer becomes a Math::BigInt with the truncated value:
>
>     $ perl -Mbigint -wle 'print 3.16'
>     3
>
> I'm not saying that all four cases should return the same value, but
> returning three different values seems too much. Any suggestions?
>

Truncation to integer seems the most useful and expected behavior to me.
Not sure if the "overloading of constants" case should be affected, would
people depend on it staying unmodified?

-Dan

--000000000000731aa005c9ea8877
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr">On Thu, Aug 19, 2021 at 4:18 AM Peter Joh=
n Acklam &lt;<a href=3D"mailto:[email protected]">[email protected]</a>&g=
t; wrote:<br></div><div class=3D"gmail_quote"><blockquote class=3D"gmail_qu=
ote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,20=
4);padding-left:1ex"><div dir=3D"ltr"><span class=3D"gmail_default" style=
=3D"font-family:verdana,sans-serif"></span>Hi!<br><br>I would like some inp=
ut on how the Math::BigInt module and bigint pragma<br>should handle non-in=
tegers. The current behaviour is rather inconsistent.<br><br>The new() cons=
tructor converts a non-integer to a Math::BigInt NaN:<br><br>=C2=A0 =C2=A0 =
$ perl -MMath::BigInt -wle &#39;print Math::BigInt -&gt; new(&quot;3.16&quo=
t;)&#39;<br>=C2=A0 =C2=A0 NaN<br><br>A math operation that returns a non-in=
teger, returns a Math::BigInt with the<br>truncated value:<br><br>=C2=A0 =
=C2=A0 $ perl -MMath::BigInt -wle &#39;print Math::BigInt -&gt; new(&quot;1=
0&quot;) -&gt; bsqrt()&#39;<br>=C2=A0 =C2=A0 3<br><br>Math::BigInt with ove=
rloading of constants, leave a non-integer as an<br>unmodified Perl scalar:=
<br><br>=C2=A0 =C2=A0 $ perl -MMath::BigInt=3D:constant -wle &#39;print 3.1=
6&#39;<br>=C2=A0 =C2=A0 3.16<br><br>However, when the &quot;bigint&quot; pr=
agma is used for overloading constants, a<br>non-integer becomes a Math::Bi=
gInt with the truncated value:<br><br>=C2=A0 =C2=A0 $ perl -Mbigint -wle &#=
39;print 3.16&#39;<br>=C2=A0 =C2=A0 3<br><br>I&#39;m not saying that all fo=
ur cases should return the same value, but<br>returning three different val=
ues seems too much. Any suggestions?</div></blockquote><div><br></div><div>=
Truncation to integer seems the most useful and expected behavior to me. No=
t sure if the &quot;overloading of constants&quot; case should be affected,=
 would people depend on it staying unmodified?</div><div><br></div><div>-Da=
n=C2=A0</div></div></div>

--000000000000731aa005c9ea8877--