473,586 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

About PostgreSQL's limit on arithmetic operations

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,

We were performing some tests on PostgreSQL and found that it fails on the
following query:

SELECT 512*18014398509 481984 AS result;

and the result is:

result
- ----------------------
-922337203685477 5808

It should be 922337203685477 5808... This is PostgreSQL 7.4.5 on Fedora
Core 2.

We tested the same query on some other database servers:

Oracle 9 ias r2 : 9,2233720368547 8E18
Oracle 9i WinXP, 32 bit : 922337203685477 5808
Oracle 9.2.0.3 on Redhat 9 : 922337203685477 5808
Oracle 9.2.0.3 on Redhat Advanced Server 2.1 : 922337203685477 5808
MS-SQL Server 2000 : 922337203685477 5808
MySQL on Sun Solaris 6 : -922337203685477 5808
MySQL on Red Hat Enterprise Linux 3.0 : -922337203685477 5808

It seems that MySQL and PostgreSQL fails on this query. Is this a bug, or
a lack of something?

.... or is there a limit?

Regards,
- --
Devrim GUNDUZ
devrim~gunduz.o rg devrim.gunduz~l inux.org.tr
http://www.tdmsoft.com
http://www.gunduz.org

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFBWmc4tl8 6P3SPfQ4RAliCAJ 9JJA3111mxrcBnn WptI8cTQzdpoQCg sBQV
HMKeNGowkd35BhM wW6kWwbs=
=BWS3
-----END PGP SIGNATURE-----

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #1
6 2393
On Wednesday 29 Sep 2004 1:11 pm, Devrim GUNDUZ wrote:
Hi,

We were performing some tests on PostgreSQL and found that it fails on the
following query:

SELECT 512*18014398509 481984 AS result;

and the result is:

result
----------------------
-922337203685477 5808

It should be 922337203685477 5808... This is PostgreSQL 7.4.5 on Fedora
Core 2.

shridhar@ps0499 :~$ psql template1
Welcome to psql 8.0.0beta2, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

template1=# SELECT 512*18014398509 481984 AS result;
result
----------------------
-922337203685477 5808
(1 row)

template1=# SELECT 512*18014398509 481984::bigint AS result;
result
----------------------
-922337203685477 5808
(1 row)

template1=# SELECT 512*18014398509 481984::numeric (20) AS result;
result
---------------------
922337203685477 5808
(1 row)

template1=# select version();
version
-----------------------------------------------------------------------------
PostgreSQL 8.0.0beta2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.3.4
(1 row)

Shridhar
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,

On Wed, 29 Sep 2004, Shridhar Daithankar wrote:

SELECT 512*18014398509 481984 AS result;

and the result is:

result
----------------------
-922337203685477 5808

It should be 922337203685477 5808... This is PostgreSQL 7.4.5 on Fedora
Core 2.

shridhar@ps0499 :~$ psql template1


<snip>
template1=# SELECT 512*18014398509 481984::numeric (20) AS result;
result
---------------------
922337203685477 5808
(1 row)


Ok, I got the same result in 7.4.5... But... Why do we have to cast it
into numeric? The results from other databases shows that they can perform
it without casting...

Anyway, thanks for the tip.

Regards,

- --
Devrim GUNDUZ
devrim~gunduz.o rg devrim.gunduz~l inux.org.tr
http://www.tdmsoft.com
http://www.gunduz.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFBWniTtl8 6P3SPfQ4RAt9/AJ0dG8bXaDVgqAv tgIDiEAX7MwU4YA CfeR+l
c4nhYwft3NvJWhO dHJ90Lm8=
=4uVc
-----END PGP SIGNATURE-----

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #3
On Wednesday 29 Sep 2004 2:25 pm, Devrim GUNDUZ wrote:
template1=# SELECT 512*18014398509 481984::numeric (20) AS result;
result
---------------------
922337203685477 5808
(1 row)


Ok, I got the same result in 7.4.5... But... Why do we have to cast it
into numeric? The results from other databases shows that they can perform
it without casting...


Probably because the normal integer is 4 bytes long and bigint is 8 bytes
long. The value above is exactly 2^63 at which a 8 bytes long signed bigint
should flip sign/overflow. I am still puzzled with correct value and negative
sign..

For arbitrary precision integer, you have to use numeric. It is not same as
oracle.

Furthermore if your number fit in range, then numbers like precision(4,0) in
oracle to smallint in postgresql would buy you huge speed
improvement(com pared to postgresql numeric I mean)

Please correct me if I am wrong..

Shridhar

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

Nov 23 '05 #4
On Wed, Sep 29, 2004 at 04:51:30PM +0530, Shridhar Daithankar wrote:
Probably because the normal integer is 4 bytes long and bigint is 8 bytes
long. The value above is exactly 2^63 at which a 8 bytes long signed bigint
should flip sign/overflow. I am still puzzled with correct value and negative
sign..
Because in signed integer arithmatic the negative numbers go one
further than the positive numbers. So the largest positive number is
2^63-1 and the largest negative number is -2^63.

Or put it another way: -2^63 = +2^63. It's the far end of the loop,
exactly opposite the zero.
For arbitrary precision integer, you have to use numeric. It is not same as
oracle.
I would say that it should produce an overflow error rather than just
returning the wrong answer, but that's not my call...

Have a nice day,
--
Martijn van Oosterhout <kl*****@svana. org> http://svana.org/kleptog/ Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
tool for doing 5% of the work and then sitting around waiting for someone
else to do the other 95% so you can sue them.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFBWp/PY5Twig3Ge+YRAi fNAKCJtc4JzQJVZ USbDAt/BNy2eOIl8QCcD3m w
Alh4n5aFI86azoe jWegmhaU=
=qLuU
-----END PGP SIGNATURE-----

Nov 23 '05 #5

On Wed, 29 Sep 2004, Devrim GUNDUZ wrote:
We were performing some tests on PostgreSQL and found that it fails on the
following query:

SELECT 512*18014398509 481984 AS result;

and the result is:

result
- ----------------------
-922337203685477 5808


This query should either return the "correct" answer or error. The
constants are probably getting typed as int4 and int8 but the result
overflows int8. If we say that the result of an int8 and int4 must be
within the int8 precision (which appears to be allowed by the spec,
although may not be what was inteneded) then it should error.

We don't currently check the overflow cases for int<n> to generate the
error, and we don't upgrade smaller integer types into numerics if it
would have overflowed. At some point we probably need to do one of those,
but AFAIK noone's been particularly motivated to do it.

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

Nov 23 '05 #6
Signed integers use a number system known as "two's compliment" and in
order to
make room for negative numbers the most significant bit is used to
indicate a negative
value and the compliment of the rest of the bits minus 1 {there is no
such number as -0}
evaluate to the negative numbers. This method is used because it makes
addition and
subtraction simpler for computers to perform on integers.

I am not sure how to declare an unsigned bigint, but if you can it would
produce
the expected result.

Only Oracle developers could tell you why they would generate an invalid
result when
doing math using 64 bit signed integers {92233720368547 75808 is not a
valid value
for a 64bit signed integer.} Just because Oracle gives you the number
you want it
in no way makes the result correct. Since the result is outside the
scope of a 64 bit
signed integer an overflow error could be a valid result, or the value
postgresql
returns could also be a valid result. If you are multiplying by 512 in
order to perform
a binary right shift of 9 bits on the integer you don't want scope
validation, because if
were enforced a right shift would not work, and the result would produce
an error.
Please read the documentation on data types, it details the scope
{range} for all data
types. If you use the proper data type for your data set you can reduce
the amount of
storage required for small numbers and be fairly certain that the math
will work as
expected using large numbers.

Shridhar Daithankar wrote:
On Wednesday 29 Sep 2004 2:25 pm, Devrim GUNDUZ wrote:

template1= # SELECT 512*18014398509 481984::numeric (20) AS result;
result
---------------------
922337203685 4775808
(1 row)

Ok, I got the same result in 7.4.5... But... Why do we have to cast it
into numeric? The results from other databases shows that they can perform
it without casting...


Probably because the normal integer is 4 bytes long and bigint is 8 bytes
long. The value above is exactly 2^63 at which a 8 bytes long signed bigint
should flip sign/overflow. I am still puzzled with correct value and negative
sign..

For arbitrary precision integer, you have to use numeric. It is not same as
oracle.

Furthermore if your number fit in range, then numbers like precision(4,0) in
oracle to smallint in postgresql would buy you huge speed
improvement(co mpared to postgresql numeric I mean)

Please correct me if I am wrong..

Shridhar


--
Guy Fraser
Network Administrator
The Internet Centre
780-450-6787 , 1-888-450-6787

There is a fine line between genius and lunacy, fear not, walk the
line with pride. Not all things will end up as you wanted, but you
will certainly discover things the meek and timid will miss out on.


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

Nov 23 '05 #7

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

Similar topics

125
14640
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
8
2232
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions. The reference should be 3.9.1 (Fundamental types), and 4.7 (Integral conversions). It seems to me that the Standard doesn't specify: 1) The...
22
1944
by: James H. | last post by:
Greetings! I'm new to Python and am struggling a little with "and" and "or" logic in Python. Since Python always ends up returning a value and this is a little different from C, the language I understand best (i.e. C returns non-zero as true, and zero as false), is there anything I should be aware of given Python's different approach? ...
10
2312
by: Massimiliano Alberti | last post by:
Are there rules on how should the C behave with the arithmetic operations? Are the overflows always ignored? And are they simply truncated? (if I have a 16-bits unsigned short ints unsigned short int a = 65535, b = 10; is a + b defined? And a * b? and b - a?) bye
81
7262
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there be any advantage in having strcat and strcpy return a pointer to the "end" of the destination string rather than returning a
3
23733
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 by C# or they are hardware (processor) dependent. Basic addition operation errors, for ex: 3.6 - 2.4 = 1.19999999999 or 1.20000000003 There are the...
1
2393
by: Satpreet | last post by:
I'm looking to simulate the behavior of a digital hardware arithmetic block in a C/C++ program. I was just wondering if there are any libraries (with datatypes and overloaded operators etc.) available for arithmetic operations on Fixed/Block-Floating Point variables ?? Alternatively could anyone throw me a pointer to sample code if...
90
3394
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
27
2030
by: jacob navia | last post by:
As Richard Bos rightly pointed out, I had left in my classification of types the C99 types Complex and boolean. Here is a new classification. Those are not mentioned in the classification of Plauger and Brody, probably because their work predates C99. Since there are no examples of this in the literature (known to me) please take a look. ...
0
7911
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8200
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8215
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.