Re: Math::BigInt and bigint with non-integers
[email protected] (Timothe Litt) Thu, 19 Aug 2021 10:51:43 -0400
| Newsgroups | perl.module-authors |
|---|---|
| Message-ID | <[email protected]> |
--PmsEcNUKzjMjrt9H10YyEhZSXJ8KqNgys Content-Type: multipart/mixed; boundary="KEz0x7jCXON3i1JxCvH8F8IBi0Dkz6fuL"; protected-headers="v1" From: Timothe Litt <[email protected]> To: [email protected] Message-ID: <[email protected]> Subject: Re: Math::BigInt and bigint with non-integers References: <CAEvoY43T_W2PYDoQtvss1r+mLsXpV2LGxpV2jpSr7L-LVCTuFQ@mail.gmail.com> In-Reply-To: <CAEvoY43T_W2PYDoQtvss1r+mLsXpV2LGxpV2jpSr7L-LVCTuFQ@mail.gmail.com> --KEz0x7jCXON3i1JxCvH8F8IBi0Dkz6fuL Content-Type: multipart/alternative; boundary="------------3D9B34530C352837A0E18793" Content-Language: en-US This is a multi-part message in MIME format. --------------3D9B34530C352837A0E18793 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Consistency is good. A case could be made for either truncating to integer, or rounding to integer. I'm inclined toward round - since int() is a builtin (round isn't), which makes it easy to get the truncate behavior.=C2=A0 And one is more likely to get a mathematically sensible result. On the other hand, changing long-standing behavior may wake the dragons. I don't like bug compatibility switches, but in this case a "use Math::Biging qw/roundtoint/" (or whatever you choose) might be worth considering.=C2=A0 Unless you're up for dragonfire... On 19-Aug-21 04:17, Peter John Acklam wrote: > Hi! > > I would like some input on how the Math::BigInt module and bigint pragm= a > should handle non-integers. The current behaviour is rather inconsisten= t. > > The new() constructor converts a non-integer to a Math::BigInt NaN: > > =C2=A0 =C2=A0 $ perl -MMath::BigInt -wle 'print Math::BigInt -> new("3.= 16")' > =C2=A0 =C2=A0 NaN > > A math operation that returns a non-integer, returns a Math::BigInt > with the > truncated value: > > =C2=A0 =C2=A0 $ perl -MMath::BigInt -wle 'print Math::BigInt -> new("10= ") -> > bsqrt()' > =C2=A0 =C2=A0 3 > > Math::BigInt with overloading of constants, leave a non-integer as an > unmodified Perl scalar: > > =C2=A0 =C2=A0 $ perl -MMath::BigInt=3D:constant -wle 'print 3.16' > =C2=A0 =C2=A0 3.16 > > However, when the "bigint" pragma is used for overloading constants, a > non-integer becomes a Math::BigInt with the truncated value: > > =C2=A0 =C2=A0 $ perl -Mbigint -wle 'print 3.16' > =C2=A0 =C2=A0 3 > > I'm not saying that all four cases should return the same value, but > returning three different values seems too much. Any suggestions? > > Cheers, > Peter --------------3D9B34530C352837A0E18793 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable <html> <head> <meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3DUTF= -8"> </head> <body> <p>Consistency is good.</p> <p>A case could be made for either truncating to integer, or rounding to integer.</p> <p>I'm inclined toward round - since int() is a builtin (round isn't), which makes it easy to get the truncate behavior.=C2=A0 And= one is more likely to get a mathematically sensible result.</p> <p>On the other hand, changing long-standing behavior may wake the dragons.</p> <p>I don't like bug compatibility switches, but in this case a "use Math::Biging qw/roundtoint/" (or whatever you choose) might be worth considering.=C2=A0 Unless you're up for dragonfire...</p> <p><br> </p> <div class=3D"moz-cite-prefix">On 19-Aug-21 04:17, Peter John Acklam wrote:<br> </div> <blockquote type=3D"cite" cite=3D"mid:[email protected]= l.com"> <meta http-equiv=3D"content-type" content=3D"text/html; charset=3DU= TF-8"> <div dir=3D"ltr"><span class=3D"gmail_default" style=3D"font-family:verdana,sans-serif"></span>Hi!<br> <br> I would like some input on how the Math::BigInt module and bigint pragma<br> should handle non-integers. The current behaviour is rather inconsistent.<br> <br> The new() constructor converts a non-integer to a Math::BigInt NaN:<br> <br> =C2=A0 =C2=A0 $ perl -MMath::BigInt -wle 'print Math::BigInt ->= ; new("3.16")'<br> =C2=A0 =C2=A0 NaN<br> <br> A math operation that returns a non-integer, returns a Math::BigInt with the<br> truncated value:<br> <br> =C2=A0 =C2=A0 $ perl -MMath::BigInt -wle 'print Math::BigInt ->= ; new("10") -> bsqrt()'<br> =C2=A0 =C2=A0 3<br> <br> Math::BigInt with overloading of constants, leave a non-integer as an<br> unmodified Perl scalar:<br> <br> =C2=A0 =C2=A0 $ perl -MMath::BigInt=3D:constant -wle 'print 3.16'= <br> =C2=A0 =C2=A0 3.16<br> <br> However, when the "bigint" pragma is used for overloading constants, a<br> non-integer becomes a Math::BigInt with the truncated value:<br> <br> =C2=A0 =C2=A0 $ perl -Mbigint -wle 'print 3.16'<br> =C2=A0 =C2=A0 3<br> <br> I'm not saying that all four cases should return the same value, but<br> returning three different values seems too much. Any suggestions?<br> <br> Cheers,<br> Peter</div> </blockquote> </body> </html> --------------3D9B34530C352837A0E18793-- --KEz0x7jCXON3i1JxCvH8F8IBi0Dkz6fuL-- --PmsEcNUKzjMjrt9H10YyEhZSXJ8KqNgys Content-Type: application/pgp-signature; name="OpenPGP_signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="OpenPGP_signature" -----BEGIN PGP SIGNATURE----- wsF5BAABCAAjFiEECPZdIQGWXboL5MgatZPHNt4V12MFAmEeb/8FAwAAAAAACgkQtZPHNt4V12Pd phAAoYTFmna4wGYHzrCIk/HzjwJSGYN0GOiibNCUqBf8LX7gLprboFqxtfwS28KF6bSJACC+Kyim OO8N8IPj8qNKGr8gSaJNOO4iv2yGLBuMnZ81jdOUsOiFSR9wxEWCiA/cQb+s43lo90pW8xjib3wR /8f71lLDWnzwtsVA8Sx1WMaknBm3CvdN0HpqprBb0LQyNyokAS6jsb/brrC9PcaF6MnrWLS0EhWC 36vRfvvqC/lgUyYyt7w0qgLvYjLE9cPWucuzVwM1f7iEzNK16eohZJsGKhFfcpVznCtCXOk/qa6X lsR2yGGSbABjDwG3hVNsTVkh7ULKwNo0sEBCn9eTYH7jPpDOW7upmB1ETKt6+SikcWUj26+ZGpUe oviFIRbSsMCdYt1LEyer+rqQ5ZS1h/n5GwV0l1o5W7uKlm8w7XiqVFmN90jCk/qn7E4tM1GNKLCB P1e6xSYi9klQZakgMC8cCvsyjvsHShvj8NKiwPGA0yg0JnRj66+GomfgKdJjhrGEQf1MKzYUemz2 F6JiKfmmKEC+cn1qBNxPddi7mJyKXwkc4pCoG17/REdLfBcOu1yd3Sm2gSqsqmTsU4i2TYkxtzi3 01A3vR2klANeukLi4t9xcGz+EpcKTbZLlffufVmZ7QPnqVM00E6mdzvoaVQM7AgzruYnKY2CDpl1 4gA= =k5wL -----END PGP SIGNATURE----- --PmsEcNUKzjMjrt9H10YyEhZSXJ8KqNgys--