473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Arbitrary precision modulo operation

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

Nov 23 '05 #1
12 2817
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:
On Mon, Apr 26, 2004 at 10:18:52 -0400,
Chadwick Boggs <ch***********@yahoo.com> wrote:

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.


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.

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #2
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:
On Mon, Apr 26, 2004 at 10:18:52 -0400,
Chadwick Boggs <ch***********@yahoo.com> wrote:

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.


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.

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #3
On Mon, Apr 26, 2004 at 13:30:17 -0400,
Chadwick Boggs <ch***********@yahoo.com> wrote:
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):
select '123456789012345678901234567890' % 123;

+----------------------------------------+
| '123456789012345678901234567890' % 123 |
+----------------------------------------+
| 58 |
+----------------------------------------+
1 row in set (0.00 sec)


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

Nov 23 '05 #4
On Mon, Apr 26, 2004 at 13:30:17 -0400,
Chadwick Boggs <ch***********@yahoo.com> wrote:
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):
select '123456789012345678901234567890' % 123;

+----------------------------------------+
| '123456789012345678901234567890' % 123 |
+----------------------------------------+
| 58 |
+----------------------------------------+
1 row in set (0.00 sec)


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

Nov 23 '05 #5
On Mon, Apr 26, 2004 at 10:18:52 -0400,
Chadwick Boggs <ch***********@yahoo.com> wrote:
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.


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.

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #6
On Mon, Apr 26, 2004 at 10:18:52 -0400,
Chadwick Boggs <ch***********@yahoo.com> wrote:
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.


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.

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #7
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):
select '123456789012345678901234567890' % 123; +----------------------------------------+
| '123456789012345678901234567890' % 123 |
+----------------------------------------+
| 58 |
+----------------------------------------+
1 row in set (0.00 sec)
Bruno Wolff III wrote:
On Mon, Apr 26, 2004 at 10:18:52 -0400,
Chadwick Boggs <ch***********@yahoo.com> wrote:

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.


How large is extremely large?
You can cast the strings to a numeric type to solve the string problem.
'numeric' should work for numbers up to about 1000 digits.
For example:
area=> select '1234567890'::numeric % '123'::numeric;
?column?
----------
39
(1 row)

If you are getting wrong results you should post a specific example so
that the developers can figure out what is going wrong.

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #8
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):
select '123456789012345678901234567890' % 123; +----------------------------------------+
| '123456789012345678901234567890' % 123 |
+----------------------------------------+
| 58 |
+----------------------------------------+
1 row in set (0.00 sec)
Bruno Wolff III wrote:
On Mon, Apr 26, 2004 at 10:18:52 -0400,
Chadwick Boggs <ch***********@yahoo.com> wrote:

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.


How large is extremely large?
You can cast the strings to a numeric type to solve the string problem.
'numeric' should work for numbers up to about 1000 digits.
For example:
area=> select '1234567890'::numeric % '123'::numeric;
?column?
----------
39
(1 row)

If you are getting wrong results you should post a specific example so
that the developers can figure out what is going wrong.

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #9
On Mon, Apr 26, 2004 at 10:18:52 -0400,
Chadwick Boggs <ch***********@yahoo.com> wrote:
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.


How large is extremely large?
You can cast the strings to a numeric type to solve the string problem.
'numeric' should work for numbers up to about 1000 digits.
For example:
area=> select '1234567890'::numeric % '123'::numeric;
?column?
----------
39
(1 row)

If you are getting wrong results you should post a specific example so
that the developers can figure out what is going wrong.

---------------------------(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

Nov 23 '05 #10
On Mon, Apr 26, 2004 at 10:18:52 -0400,
Chadwick Boggs <ch***********@yahoo.com> wrote:
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.


How large is extremely large?
You can cast the strings to a numeric type to solve the string problem.
'numeric' should work for numbers up to about 1000 digits.
For example:
area=> select '1234567890'::numeric % '123'::numeric;
?column?
----------
39
(1 row)

If you are getting wrong results you should post a specific example so
that the developers can figure out what is going wrong.

---------------------------(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

Nov 23 '05 #11
I see there are a few misconceptions about numeric and modulus on here:

(1) A modulus operation on a numeric type should NOT have rounding
errors. The whole point of numeric is that it is an arbitrary precision
BASE 10 representation of your number. The modulus returns the (whole
number) remainder as a result of a division.

(2) the modulus operator/function is, AFAIK, supposed to return the
modulus with the SAME SIGN as the divisor, so I think this is a bug.
That's what every other modulus operator that I have ever seen does.
Would you mind doing

foodb=> SELECT version();

(3) MySQL just rounds large numbers to the highest value that the type
will support, and apparently, no arbitrary precision types are listed on
this page:

http://dev.mysql.com/doc/mysql/en/Nu..._overview.html

You can't expect to get a modulus from an out-of-range number.

Paul Tillotson

Chadwick Boggs wrote:
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):
select '123456789012345678901234567890' % 123;

+----------------------------------------+
| '123456789012345678901234567890' % 123 |
+----------------------------------------+
| 58 |
+----------------------------------------+
1 row in set (0.00 sec)
Bruno Wolff III wrote:
On Mon, Apr 26, 2004 at 10:18:52 -0400,
Chadwick Boggs <ch***********@yahoo.com> wrote:

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.

How large is extremely large?
You can cast the strings to a numeric type to solve the string problem.
'numeric' should work for numbers up to about 1000 digits.
For example:
area=> select '1234567890'::numeric % '123'::numeric;
?column?
----------
39
(1 row)

If you are getting wrong results you should post a specific example so
that the developers can figure out what is going wrong.

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #12
I see there are a few misconceptions about numeric and modulus on here:

(1) A modulus operation on a numeric type should NOT have rounding
errors. The whole point of numeric is that it is an arbitrary precision
BASE 10 representation of your number. The modulus returns the (whole
number) remainder as a result of a division.

(2) the modulus operator/function is, AFAIK, supposed to return the
modulus with the SAME SIGN as the divisor, so I think this is a bug.
That's what every other modulus operator that I have ever seen does.
Would you mind doing

foodb=> SELECT version();

(3) MySQL just rounds large numbers to the highest value that the type
will support, and apparently, no arbitrary precision types are listed on
this page:

http://dev.mysql.com/doc/mysql/en/Nu..._overview.html

You can't expect to get a modulus from an out-of-range number.

Paul Tillotson

Chadwick Boggs wrote:
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):
select '123456789012345678901234567890' % 123;

+----------------------------------------+
| '123456789012345678901234567890' % 123 |
+----------------------------------------+
| 58 |
+----------------------------------------+
1 row in set (0.00 sec)
Bruno Wolff III wrote:
On Mon, Apr 26, 2004 at 10:18:52 -0400,
Chadwick Boggs <ch***********@yahoo.com> wrote:

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.

How large is extremely large?
You can cast the strings to a numeric type to solve the string problem.
'numeric' should work for numbers up to about 1000 digits.
For example:
area=> select '1234567890'::numeric % '123'::numeric;
?column?
----------
39
(1 row)

If you are getting wrong results you should post a specific example so
that the developers can figure out what is going wrong.

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Thinkit | last post by:
Are there any good libraries of arbitrary precision binary floats? I'd like to specify the mantissa length and exponent range. Hexadecimal output would be best (decimal is disgusting with binary...
0
by: Thinkit | last post by:
Are there any packages for arbitrary precision binary floats? Something along the lines of Gnu Multi Precision. I saw quite a few rationals classes, but not this. Just looking to be able to use...
5
by: Mattias Brändström | last post by:
Hello! I am trying to find a minimal class/lib that handles arbitrary precision decimal numbers. I would be happy if this class supported as little as addition, subtraction, multiplication,...
3
by: Madan | last post by:
Hi all, I had problem regarding float/double arithmetic only with + and - operations, which gives inaccurate precisions. I would like to know how the arithmetic operations are internally handled...
11
by: Ben Blank | last post by:
I have a loop which iterates over an array in a particular order: for (j = 0; j < 16; j++) T = ...; The loop proceeds normally for j = 0 through j = 4, but gives an IndexOutOfRangeException at...
0
by: Chadwick Boggs | last post by:
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,...
15
by: Alasdair | last post by:
I need to apply the ceiling function to arbitrary sized (long) integers. However, division automatically returns the type of its operands, so that, for example: math.ceil(7/4) returns 1. I can use...
8
by: Martin the Third | last post by:
Hi, I need some help! I'm writing an infinite-precision floating point library called ipfloat (I know infinite is a misnomer - but arbitrary was taken). A quick overview: I'm storing numbers as...
2
by: Rob Clewley | last post by:
Dear Pythonistas, How many times have we seen posts recently along the lines of "why is it that 0.1 appears as 0.10000000000000001 in python?" that lead to posters being sent to the definition...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.