473,386 Members | 1,842 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.

Why doesn't this work--Extending Python--?

I have written a simple C++ program in my efforts to learn how to
extend Python. It is shown below. Everything compiles and installs
correctly, but I get strange answers. I know the function "Pi" is
correct because when I call it from a C++ code it gives the correct
answers. This is what I get when I run it in Python:

Python 2.3 (#1, Sep 13 2003, 00:49:11)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
import Pie
Pie.Pi(100) 4.244022824665725e-314 Pie.Pi(100000) 6.3659915186006512e-314 Pie.Pi(1000000)

6.3659881268399925e-314

Thanks for the help,
Jeremy

1 /* PiPython.cpp*/

2 #include<Python.h>

3
4 using namespace std;

5
6 double Pi(int Particles)

7 {

8 double PiEst = 0.0;

9 double x,y;

10 int CircleArea = 0;
11 for ( int i = 0; i <= Particles; i++)

12 {
13 x = (double) rand()/RAND_MAX;

14 y = (double) rand()/RAND_MAX;

15

16 if (sqrt(pow(x,2)+pow(y,2)) <= 1.0)

17 {

18 CircleArea++;
19 }
20 }

21 return 4.0*(double(CircleArea)/double(Particles));

22 }

23
24
25 PyObject *wrap_Pi(PyObject *self, PyObject *args)

26 {

27 int Particles;
28 int Pie;
29 if (!PyArg_ParseTuple(args, "i", &Particles))

30 return NULL;

31 Pie = Pi(Particles);

32 return Py_BuildValue("d", Pie);
33 }
34

35 /* List of all functions in the module */
36 static PyMethodDef PieMethods[] =
37 {
38 {"Pi", wrap_Pi, METH_VARARGS},
39 {NULL, NULL}
40 };
41
42 /* Module Initialization function */
43 PyMODINIT_FUNC initPie(void)
44 {
45 Py_InitModule("Pie", PieMethods);
46 }

Jan 4 '06 #1
2 940
jeremito wrote:
I have written a simple C++ program in my efforts to learn how to
extend Python. It is shown below. Everything compiles and installs
correctly, but I get strange answers. I know the function "Pi" is
correct because when I call it from a C++ code it gives the correct
answers. This is what I get when I run it in Python: 27 int Particles;
28 int Pie;
29 if (!PyArg_ParseTuple(args, "i", &Particles))

30 return NULL;

31 Pie = Pi(Particles);

32 return Py_BuildValue("d", Pie);


Just scanning over this, looks like what you want is double Pie. You
have int. The compiler probably gave a warning.

http://www.signalsguru.net/

Jan 4 '06 #2
Well what do you know, that worked! It's one of those errors that you
can't see yourself, but someone else can see it instantly.
Thanks,
Jeremy

Jan 4 '06 #3

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

Similar topics

23
by: Yannick Patois | last post by:
Hi, Under some naming conditions of module files, it seems that python lost class static variables values. It seems only to append when importing a "badly" named module that itself import a...
81
by: julio | last post by:
Sorry but there is no another way, c# .net and mono are going to rip python, not because python is a bad lenguage, but because is to darn old and it refuses to innovate things, to fix wrong things,...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 391 open ( +7) / 3028 closed (+12) / 3419 total (+19) Bugs : 906 open ( -3) / 5519 closed (+19) / 6425 total (+16) RFE : 207 open...
34
by: glomde | last post by:
i I would like to extend python so that you could create hiercical tree structures (XML, HTML etc) easier and that resulting code would be more readable than how you write today with packages like...
10
by: placid | last post by:
Hi all, Can someone tell me what * in the following code means/does a Google search didnt turn up anything as i dont know what the * is called (related to Python and i dont think Python has...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 421 open ( +3) / 3530 closed ( +8) / 3951 total (+11) Bugs : 963 open ( +4) / 6426 closed (+21) / 7389 total (+25) RFE : 255 open...
5
by: glomde | last post by:
Hi, I tried to write a decorator for that should be for methods but for some reasons it doens seem to work when you try to do it on the __getattr__ method in a class. Could anybody give some...
4
by: notnorwegian | last post by:
>>x what do i need to do?
61
by: giveitawhril2008 | last post by:
I'm just learning about Python now and it sounds interesting. But I just read (on the Wiki page) that mainstream Python was written in C. That's what I was searching for: Python was written in what...
0
by: hrishy | last post by:
Hi Tim I am not a LINQ expert just a LINQ user and (was a little envious why the langauge i fantasize doesnt have it (pardon my ignorance of python)) LINQ as far as i know allows you to query...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
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...

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.