473,708 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

integer type conversion problem/question

Hi,

I'm not sure what would be more appropriate, so I'm ccing it to both
alt.comp.lang.l earn.c-c++ and comp.lang.pytho n, with followup to
alt.comp.lang.l earn.c-c++.

While working with a random number generator in the context of a mixed
Python/C++ programming problem. I encountered a vexing type
conversion problem.

Briefly, the situation is as follows.

I have a integer in Python (Python integers are implemented as C long
ints).

This is passed to a function in C++ which converts it (supposedly) to
an unsigned int, modifies it and then passes it back to Python. This
is done since the random number generator uses unsigned ints.

Python attempts to convert it to a Python integer, and if it is too
large, converts it into a Python long. (Both integer and long are
Python types).

Now, my program crashes, because at some point the unsigned integer
passed to Python becomes too long to be represented as an (unsigned)
int, and I get an overflow error.

OverflowError: long int too large to convert to int

An example of such a number is 2321871520. Python thinks this should
be a long, but my C++ code seems to handle it as an unsigned int, and
passes it to Python as such. When Python converts it to a long and
tries to pass it back, I get a runtime error.

Python gives:

In [1]: 2321871520
Out[1]: 2321871520L
I tried compiling the following fragment of code (header ommitted) and
got a compiler warning: warning: this decimal constant is unsigned
only in ISO C90.

I'm not sure what to make of this.

*************** *************** *
int main()
{
unsigned int a = 2321871520;
cout << a << endl;
return 0;
}
*************** *************** ***

I find all this a little strange. Since in theory Python ints are
larger than C ints (since Python ints are implemented as C long ints),
there should be (in theory) no problem converting C unsigned ints to
Python integers (corresponding to C long integers) but in practice
there is. Can anyone enlighten me as to this puzzling situation?
Thanks.

I'd be happy to give more details as necessary. I realise the above
may not be entirely clear, but excessive detail may be confusing.
Please CC me on any reply. Thanks.

Faheem.
Jul 18 '05 #1
11 2531
>Since in theory Python ints are larger than C ints
(since Python ints are implemented as C long ints),
Wrong premise. On many (most?) systems today, C long == C int == 32 bits.
there should be (in theory) no problem converting C unsigned ints to
Python integers (corresponding to C long integers)


Hence wrong conclusion, as you discovered. Unsigned C int > 2**31 does not
convert properly to C long when C long == C int.

If you must program your own RNG, simplest solution is to limit it to range
[0, 2**31-1] so signed/unsigned does not matter.

Terry J. Reedy

Jul 18 '05 #2
[Screwed up setting the followup, sorry. Really setting followups to
alt.comp.lang.l earn.c-c++ this time.]

On Sat, 09 Oct 2004 06:50:20 GMT, Faheem Mitha <fa****@email.u nc.edu> wrote:
Hi,

I'm not sure what would be more appropriate, so I'm ccing it to both
alt.comp.lang.l earn.c-c++ and comp.lang.pytho n, with followup to
alt.comp.lang.l earn.c-c++.

While working with a random number generator in the context of a mixed
Python/C++ programming problem. I encountered a vexing type
conversion problem.


[snip]

Thanks to Alwyn and Terry Reedy for explaining things to me. I think
I understand the main points. Harbison and Steele's "C A Reference
Manual" (I have the 4th Edn) had a clear explanation of how C
implements unsigned and signed ints, including two's complement and
all that.

It looks like using a random number generator which uses unsigned ints
as its seeds with Python is probably close to impossible then. Can
anyone suggest a good C/C++ random number implementation which can be
used easily with Python in this fashion? I want something that is
full-featured, ie. has reasonable support for different random number
distributions. Also, something that was already packaged in a
reasonable fashion as part of a shared library would be nice. I
suppose something whose seeds are stored as ints or longs would work
Ok.

I was trying to use r-mathlib
(http://packages.debian.org/unstable/math/r-mathlib), the Debian
package corresponding to the standalone C Mathlib (math/stat library)
from R (www.r-project.org). Unfortunately the random number
implementation uses unsigned ints, hence all the kerfuffle. I'm
including the source code at the end of this message, for the record.

I did have one followup question. If Python implements its integers as
signed C ints then surely 2^31 - 1 should be an integer rather than a
long? But I get

In [9]: 2**31 - 1
Out[9]: 2147483647L

In [10]: type(2**31 - 1)
Out[10]: <type 'long'>

Thanks for the help.
Faheem.

*************** *************** *************** *************** ***********
src/nmath/standalone/sunif.c
*************** *************** *************** *************** ***********
/*
* Mathlib : A C Library of Special Functions
* Copyright (C) 2000, 2003 The R Development Core Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/

/* A version of Marsaglia-MultiCarry */

static unsigned int I1=1234, I2=5678;

void set_seed(unsign ed int i1, unsigned int i2)
{
I1 = i1; I2 = i2;
}

void get_seed(unsign ed int *i1, unsigned int *i2)
{
*i1 = I1; *i2 = I2;
}
double unif_rand(void)
{
I1= 36969*(I1 & 0177777) + (I1>>16);
I2= 18000*(I2 & 0177777) + (I2>>16);
return ((I1 << 16)^(I2 & 0177777)) * 2.3283064370807 97e-10; /* in [0,1) */
}
Jul 18 '05 #3
On Sat, 09 Oct 2004 19:31:30 GMT, Faheem Mitha <fa****@email.u nc.edu> wrote:

I did have one followup question. If Python implements its integers as
signed C ints then surely 2^31 - 1 should be an integer rather than a
long? But I get

In [9]: 2**31 - 1
Out[9]: 2147483647L

In [10]: type(2**31 - 1)
Out[10]: <type 'long'>


Belated followup to my own message. The following behaves as expected.

In [13]: 2147483647
Out[13]: 2147483647

In [14]: 2147483648
Out[14]: 2147483648L

Apparently there is something about the ** notation that makes Python
think of this as a long. Since Python people are more likely to know
the answer, I'm ccing comp.lang.pytho n despite the followup. Thanks.

Faheem.
Jul 18 '05 #4
> It looks like using a random number generator which uses unsigned ints
as its seeds with Python is probably close to impossible then. Can
anyone suggest a good C/C++ random number implementation which can be
used easily with Python in this fashion? I want something that is
full-featured, ie. has reasonable support for different random number
distributions. Also, something that was already packaged in a
reasonable fashion as part of a shared library would be nice. I
suppose something whose seeds are stored as ints or longs would work
Ok.
Mersenne Twister is included with Python 2.3 and later (maybe even 2.2,
I can't remember that far back).

You can use it via:
import random

It includes various distributions, read the documentation.

I did have one followup question. If Python implements its integers as
signed C ints then surely 2^31 - 1 should be an integer rather than a
long? But I get

In [9]: 2**31 - 1
Out[9]: 2147483647L

In [10]: type(2**31 - 1)
Out[10]: <type 'long'>


It first creates 2**31, then subtracts 1.
Try 2**30 + (2**30 -1).

Jul 18 '05 #5
On Sat, 09 Oct 2004 19:36:13 GMT, Faheem Mitha <fa****@email.u nc.edu> wrote:
On Sat, 09 Oct 2004 19:31:30 GMT, Faheem Mitha <fa****@email.u nc.edu> wrote:

I did have one followup question. If Python implements its integers as
signed C ints then surely 2^31 - 1 should be an integer rather than a
long? But I get

In [9]: 2**31 - 1
Out[9]: 2147483647L

In [10]: type(2**31 - 1)
Out[10]: <type 'long'>


Belated followup to my own message. The following behaves as expected.

In [13]: 2147483647
Out[13]: 2147483647

In [14]: 2147483648
Out[14]: 2147483648L

Apparently there is something about the ** notation that makes Python
think of this as a long. Since Python people are more likely to know
the answer, I'm ccing comp.lang.pytho n despite the followup. Thanks.


Eric Brewer kindly replied (directly to me), so I'm copying it here.

*************** *************** *************** *************** ********
This is because 2**31 is a long (and *then* you subtract 1)
2**31-1 2147483647L 2**30 1073741824 2**30 + 2**30 2147483648L

Here is one way to get 2**31-1 as a regular int:
2**30 + (2**30-1)

2147483647
*************** *************** *************** *************** **********

Faheem.
Jul 18 '05 #6


On Sat, 9 Oct 2004, Josiah Carlson wrote:
It looks like using a random number generator which uses unsigned ints
as its seeds with Python is probably close to impossible then. Can
anyone suggest a good C/C++ random number implementation which can be
used easily with Python in this fashion? I want something that is
full-featured, ie. has reasonable support for different random number
distributions. Also, something that was already packaged in a
reasonable fashion as part of a shared library would be nice. I
suppose something whose seeds are stored as ints or longs would work
Ok.


Mersenne Twister is included with Python 2.3 and later (maybe even 2.2,
I can't remember that far back).

You can use it via:
import random

It includes various distributions, read the documentation.


I'd looked at this earlier, but they didn't seem to have implemented a C
API. If there is one, I haven't been able to find it in
http://python.org/doc/2.3.4/api/api.html or anywhere else. I don't want to
import Python code into C/C++ even if that is possible. I think it is
easiest to work with a straight C/C++ library and interface it with
Python, assuming that I don't run into type conversion issues.

A random number C module does exist in Python 2.3. On my system (Debian
Sarge) it is /usr/lib/python2.3/lib-dynload/_random.so. However, it is
probably not set up to be directly accessed from C/C++ code.

Faheem.
Jul 18 '05 #7
Faheem Mitha <fa****@email.u nc.edu> writes:
I'd looked at this earlier, but they didn't seem to have implemented a
C API. If there is one, I haven't been able to find it in
http://python.org/doc/2.3.4/api/api.html or anywhere else.


Mersenne Twister is written in C and there's a Python wrapper for it.
If you have the Python source distro, you can just compile the
Mersenne Twister code into your C program.
Jul 18 '05 #8
Faheem Mitha <fa****@email.u nc.edu> wrote in message news:<sl******* ************@Ch restomanci.home .earth>...
On Sat, 09 Oct 2004 19:36:13 GMT, Faheem Mitha <fa****@email.u nc.edu> wrote:
On Sat, 09 Oct 2004 19:31:30 GMT, Faheem Mitha <fa****@email.u nc.edu> wrote:

I did have one followup question. If Python implements its integers as
signed C ints then surely 2^31 - 1 should be an integer rather than a
long? But I get

In [9]: 2**31 - 1
Out[9]: 2147483647L

In [10]: type(2**31 - 1)
Out[10]: <type 'long'>


Belated followup to my own message. The following behaves as expected.

In [13]: 2147483647
Out[13]: 2147483647

In [14]: 2147483648
Out[14]: 2147483648L

Apparently there is something about the ** notation that makes Python
think of this as a long. Since Python people are more likely to know
the answer, I'm ccing comp.lang.pytho n despite the followup. Thanks.


Eric Brewer kindly replied (directly to me), so I'm copying it here.

*************** *************** *************** *************** ********
This is because 2**31 is a long (and *then* you subtract 1)
2**31-1 2147483647L 2**30 1073741824 2**30 + 2**30 2147483648L

Here is one way to get 2**31-1 as a regular int:
2**30 + (2**30-1)

2147483647
*************** *************** *************** *************** **********


I'm sorry but this is off-topic in alt.comp.lang.l earn.c-c++.
We do not discuss limbless reptiles in here. :)
Jul 18 '05 #9
snip previous comments
I'm sorry but this is off-topic in alt.comp.lang.l earn.c-c++.
We do not discuss limbless reptiles in here. :)


Fear is an incredible emotion isn't it? :)

Mark Lawrence
Jul 18 '05 #10

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

Similar topics

4
10465
by: Ray | last post by:
When a single-bit bitfield that was formed from an enum is promoted/cast into an integer, does ANSI C say anything about whether that integer should be signed or unsigned? SGI IRIX cc thinks it is an unsigned integer, so I see a +1 if the bit is set. Microsoft VC++ thinks it's signed, so I see -1 if the bit is set. Ex. typedef enum {
28
2438
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions 1: Why cannot I do the following:
4
3373
by: rz0 | last post by:
Hi all, This is a question about both C89 and C99 and is based on my partial reading of the standard drafts (one from before C89 but mainly N1124). If appropriate, please give a separate answer for each version of the language. Let's consider the conversion from a given floating real type to a specific integer type.
21
4120
by: Frederick Gotham | last post by:
I set about trying to find a portable way to set the value of UCHAR_MAX. At first, I thought the following would work: #define UCHAR_MAX ~( (unsigned char)0 ) However, it didn't work for me. Could someone please explain to me what's going on? I would have thought that the following happens: (1) The literal, 0, whose type is int, gets converted to an unsigned char.
1
3781
by: charles_gero | last post by:
Hi all, I had a question about the topics in the subject and posted to comp.std.c, but feel it may also be appropriate here. Please excuse this crosspost if it is in bad form. I have a question about whether or not I am interpreting a nuance of the standard correctly, and the implications of said nuance. The sections in the C99 standard (and possibly older standards) that I will reference are as follows (typed out hopefully to avoid...
7
12281
by: Spoon | last post by:
Hello everyone, In my code, I use uint16_t (exactly 16-bit-wide unsigned integer type) and I need to print their value in base 10. http://www.opengroup.org/onlinepubs/009695399/basedefs/inttypes.h.html As far as I understand, the recommended method is: #include <inttypes.h>
39
3564
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody out there understands what happens. Essentially, when I subtract the (double) function value GRID_POINT(2) from a variable which has been assigned the same value before this gives a non-zero result and I really do not understand why.
12
3291
by: lithiumcat | last post by:
Hi, I bothered you a while back about storing integer values in void*. Now in a completely unrelated context, I'm trying to store pointer values in an integer type. So the basic question is, is it possible to convert a pointer into an integer, and then later (but on the same execution environment, ie the program has not exited, thus it's the same architecture, same compiler, same binary representations and so on) retrieve from the
9
5336
by: tsuyois | last post by:
Hi, I just signed in to this excellent network. I hope I could get some answers to many questions I have in writing C compilers. My first question is: Is "integer demotion" required in ANSI-C? Assumption: - CPU: 32-bit RISC (int = long = 4 bytes, short = 2 bytes, char = 1 byte)
0
9290
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9159
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7925
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6615
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5939
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4454
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4713
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3151
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2097
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.