任意精度模运算 [英] Arbitrary precision modulo operation
问题描述
我需要对非常大的数字执行模运算。 %
运算符给出了数字超出范围的错误,mod(x,y)
函数似乎只是返回错误的结果。另外,我的分子
采用带引号的字符串格式,mod函数无法使用。
绝望地搜索解决方案,
查德威克。
---------------------------(广播结束) - -------------------------
提示3:如果通过Usenet发布/阅读,请发送相应的>
subscribe-nomail命令 ma*******@postgresql.org 以便您的
消息可以干净利落地到达邮件列表
I need to perform modulo operations on extremely large numbers. The %
operator is giving me number out of range errors and the mod(x, y)
function simply seems to return the wrong results. Also, my numerator
is in the format of a quoted string, which the mod function can''t take.
Desparately searching for solutions,
Chadwick.
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly
推荐答案
布鲁诺,也许圆是一个问题。谢谢。这是一个例子,
应该不涉及舍入,确实有效:
乘以10个最大整数的素数:
>
#select 2147483477 :: numeric * 2147483489 :: numeric * 2147483497 :: numeric
* 2147483543 :: numeric * 2147483549 :: numeric * 2147483563 :: numeric *
2147483579 :: numeric * 2147483587 :: numeric * 2147483629 :: numeric *
2147483647 :: numeric;
?column?
-------------------------------------------------- ----------------------------------------------
20859239461389889161491906055619604751181652985829 29035878182900998428077414994652962618167119
(1行)
现在,通过任何因子模数正确返回0:
#select
''2085923946138988916149190605561960475118165298582 929035878182900998428077414994652962618167119'':: nu meric%
2147483563;
?column?
----------
0
(1排)
另外,正确分配所有因素会返回1:
#select
20859239461389889161491906055619604751181652985829 29035878182900998428077414994652962618167119
/ 2147483477 / 2147483489/2147483497/2147483543/2147483549/
2147483563 / 2147483579/2147483587/2147483629/2147483647;
?栏?
------------------------
1.00000000000000000000
(1行)
这为我的问题提供了解决方案:我只需要投出所有
的数字产品和模数操作的数字。
谢谢,
Chadwick。
Bruno Wolff III写道:
Bruno, perhaps round is an issue. Thank you. Here is an example that
should involve no rounding and indeed it works:
Multiply the ten largest integer scale prime numbers:
# select 2147483477::numeric * 2147483489::numeric * 2147483497::numeric
* 2147483543::numeric * 2147483549::numeric * 2147483563::numeric *
2147483579::numeric * 2147483587::numeric * 2147483629::numeric *
2147483647::numeric;
?column?
------------------------------------------------------------------------------------------------
20859239461389889161491906055619604751181652985829 29035878182900998428077414994652962618167119
(1 row)
Now, modulo this by any of the factors correctly returns 0:
# select
''2085923946138988916149190605561960475118165298582 929035878182900998428077414994652962618167119''::nu meric%
2147483563;
?column?
----------
0
(1 row)
Also, diviing out all of the factors correctly returns 1:
# select
20859239461389889161491906055619604751181652985829 29035878182900998428077414994652962618167119
/ 2147483477 / 2147483489 / 2147483497 / 2147483543 / 2147483549 /
2147483563 / 2147483579 / 2147483587 / 2147483629 / 2147483647;
?column?
------------------------
1.00000000000000000000
(1 row)
This provides the solution to my problem: I merely needed to cast all
of the numbers to numberic for product and modulo operations.
Thank you,
Chadwick.
Bruno Wolff III wrote:
2004年4月26日星期一上午10:18:52 - 0400,Chadwick Boggs< ch *********** @ yahoo.com>写道:
On Mon, Apr 26, 2004 at 10:18:52 -0400,
Chadwick Boggs <ch***********@yahoo.com> wrote:
我需要对非常大的数字执行模运算。 %
运算符给出了数字超出范围的错误,而mod(x,y)
函数似乎只是返回错误的结果。另外,我的分子
采用带引号的字符串格式,mod函数无法使用。
I need to perform modulo operations on extremely large numbers. The %
operator is giving me number out of range errors and the mod(x, y)
function simply seems to return the wrong results. Also, my numerator
is in the format of a quoted string, which the mod function can''t take.
我尝试了一些大的值并且似乎得到了合理的结果。
我确实得到了一些负面的余数,但我没有在
mod文档中找到关于这些是否被允许的任何内容。对于较小的值
我总是得到正余数,所以它不会一直返回最接近零的
余数。我的猜测是,在分割数字时必须进行四舍五入。
I tried some large values and seem to be getting reasonable results.
I did get some negative remainders, but I didn''t find anything in the
mod documentation on whether or not these were allowed. For small values
I always got positive remainders so it isn''t consistantly return the
remainder closest to zero. My guess is that it has to do rounding when
dividing numerics.
----------------- ----------(广播结束)---------------------------
提示4:不要杀死-9''邮政局长
---------------------------(end of broadcast)---------------------------
TIP 4: Don''t ''kill -9'' the postmaster
布鲁诺,也许圆是一个问题。谢谢。这是一个例子,
应该不涉及舍入,确实有效:
乘以10个最大整数的素数:
>
#select 2147483477 :: numeric * 2147483489 :: numeric * 2147483497 :: numeric
* 2147483543 :: numeric * 2147483549 :: numeric * 2147483563 :: numeric *
2147483579 :: numeric * 2147483587 :: numeric * 2147483629 :: numeric *
2147483647 :: numeric;
?column?
-------------------------------------------------- ----------------------------------------------
20859239461389889161491906055619604751181652985829 29035878182900998428077414994652962618167119
(1行)
现在,通过任何因子模数正确返回0:
#select
''2085923946138988916149190605561960475118165298582 929035878182900998428077414994652962618167119'':: nu meric%
2147483563;
?column?
----------
0
(1排)
另外,正确分配所有因素会返回1:
#select
20859239461389889161491906055619604751181652985829 29035878182900998428077414994652962618167119
/ 2147483477 / 2147483489/2147483497/2147483543/2147483549/
2147483563 / 2147483579/2147483587/2147483629/2147483647;
?栏?
------------------------
1.00000000000000000000
(1行)
这为我的问题提供了解决方案:我只需要投出所有
的数字产品和模数操作的数字。
谢谢,
Chadwick。
Bruno Wolff III写道:
Bruno, perhaps round is an issue. Thank you. Here is an example that
should involve no rounding and indeed it works:
Multiply the ten largest integer scale prime numbers:
# select 2147483477::numeric * 2147483489::numeric * 2147483497::numeric
* 2147483543::numeric * 2147483549::numeric * 2147483563::numeric *
2147483579::numeric * 2147483587::numeric * 2147483629::numeric *
2147483647::numeric;
?column?
------------------------------------------------------------------------------------------------
20859239461389889161491906055619604751181652985829 29035878182900998428077414994652962618167119
(1 row)
Now, modulo this by any of the factors correctly returns 0:
# select
''2085923946138988916149190605561960475118165298582 929035878182900998428077414994652962618167119''::nu meric%
2147483563;
?column?
----------
0
(1 row)
Also, diviing out all of the factors correctly returns 1:
# select
20859239461389889161491906055619604751181652985829 29035878182900998428077414994652962618167119
/ 2147483477 / 2147483489 / 2147483497 / 2147483543 / 2147483549 /
2147483563 / 2147483579 / 2147483587 / 2147483629 / 2147483647;
?column?
------------------------
1.00000000000000000000
(1 row)
This provides the solution to my problem: I merely needed to cast all
of the numbers to numberic for product and modulo operations.
Thank you,
Chadwick.
Bruno Wolff III wrote:
2004年4月26日星期一上午10:18:52 - 0400,Chadwick Boggs< ch *********** @ yahoo.com>写道:
On Mon, Apr 26, 2004 at 10:18:52 -0400,
Chadwick Boggs <ch***********@yahoo.com> wrote:
我需要对非常大的数字执行模运算。 %
运算符给出了数字超出范围的错误,而mod(x,y)
函数似乎只是返回错误的结果。另外,我的分子
采用带引号的字符串格式,mod函数无法使用。
I need to perform modulo operations on extremely large numbers. The %
operator is giving me number out of range errors and the mod(x, y)
function simply seems to return the wrong results. Also, my numerator
is in the format of a quoted string, which the mod function can''t take.
我尝试了一些大的值并且似乎得到了合理的结果。
我确实得到了一些负面的余数,但我没有在
mod文档中找到关于这些是否被允许的任何内容。对于较小的值
我总是得到正余数,所以它不会一直返回最接近零的
余数。我的猜测是,在分割数字时必须进行四舍五入。
I tried some large values and seem to be getting reasonable results.
I did get some negative remainders, but I didn''t find anything in the
mod documentation on whether or not these were allowed. For small values
I always got positive remainders so it isn''t consistantly return the
remainder closest to zero. My guess is that it has to do rounding when
dividing numerics.
----------------- ----------(广播结束)---------------------------
提示4:不要杀死-9''邮政局长
---------------------------(end of broadcast)---------------------------
TIP 4: Don''t ''kill -9'' the postmaster
2004年4月26日星期一13:30:17 -0400 ,
Chadwick Boggs< ch *********** @ yahoo.com>写道:
On Mon, Apr 26, 2004 at 13:30:17 -0400,
Chadwick Boggs <ch***********@yahoo.com> wrote:
任意精度模数运算错误结果的例子
#select''123456789012345678901234567890'':: numeric%123;
?专栏?
----------
-6
(1排)
#select mod(''123456789012345678901234567890'': :数字,123);
mod
-----
-6
(1行)
正确的结果(至少根据另一个,未命名的,RDBMS):
Example of wrong results from modulo operation of arbitrary precision
numbers:
# select ''123456789012345678901234567890''::numeric % 123;
?column?
----------
-6
(1 row)
# select mod(''123456789012345678901234567890''::numeric, 123);
mod
-----
-6
(1 row)
The correct result (at least according to another, unnamed, RDBMS):
选择''123456789012345678901234567890''%123;
select ''123456789012345678901234567890'' % 123;
+ ------------- --------------------------- +
| ''123456789012345678901234567890''%123 |
+ ------------------------------------- --- +
| 58 |
+ ---------------------------------------- +
1行(0.00秒)
+----------------------------------------+
| ''123456789012345678901234567890'' % 123 |
+----------------------------------------+
| 58 |
+----------------------------------------+
1 row in set (0.00 sec)
我用bc检查了这个,我得到-6(和117)是正确的。我会
认为其他数据库是错误的。
它不会是MYSQL吗?
---------------------------(广播结束)--------------- ------------
提示1:订阅和取消订阅命令转到 ma*******@postgresql.org
I checked this with bc and I got -6 (and 117) as being correct. I would
think the other database was wrong.
It wouldn''t happen to be MYSQL would it?
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org
这篇关于任意精度模运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!