Jump to content

April 2017 »


Recent Comments

Recent Comments

- - - - -

Wow Long Time

4: Adsense

Holy crap has it been a long time since I posted here. I have been so tied up with school and work that I kind of just fell of the face of the earth
being totally swamped with no real time to do much of anything.

I just recently due to school got back into doing some programming. Partially because of the nature of the class and me being as lazy as I could possibly be just not wanting to go through all the repeditive steps.

Right now I am taking a statistics class and calculating all of the probability stuff can get very very long and repedative to find out the various different answers. For instance when finding the binomial probability of a range of numbers in a set you might have to calculated 12 different binomial probabilities and then add them together so you can then caluculate the complement of that probability to find the other side of the range of numbers. It is just way too repedative in my liking.

The advantage of this is it really re-kindled my love of the Python language. I just wish the language was a bit more useful for game development sadly. The performance hits are just way too high when you progress onto 3D.

After I finished my homework I decided to do a comparison of the Python and C++ code required for calculating the binomial probability of a number in a set. This is the overall gist of the post because it is really amazing to see the difference in the code of two examples of the same program and it is simple enough to really demonstrate both in a reasonable amount of time. The interesting thing here is from a outside perspective runing both they appear to be run instantaniously with no performance difference at all. So here is the code btw it is indeed a night and day difference in readability and understandability.

Python (2.7.3)
def factorial(n):
    if n < 1:
	    n = 1
    return 1 if n == 1 else n * factorial(n - 1)

def computeBinomialProb(n, p, x):
    nCx = (factorial(n) / (factorial(n-x) * factorial(x)))
    px = p ** x
    q = float(1-p)
    qnMinx = q ** (n-x)
    return nCx * px * qnMinx

if __name__ == '__main__':
    n = float(raw_input("Value of n?:"))
    p = float(raw_input("Value of p?:"))
    x = float(raw_input("Value of x?:"))
    print "result = ", computeBinomialProb(n, p, x)

#include <iostream>
#include <math.h>
int factorial(int n)
    if (n < 1)
	    n = 1;
    return (n == 1 ? 1 : n * factorial(n - 1));

float computeBinomialProb(float n, float p, float x)
    float nCx = (factorial(n) / (factorial(n - x) * factorial(x)));
    float px = pow(p, x);
    float q = (1 - p);
    float qnMinx = pow(q, (n - x));
    return nCx * px * qnMinx;

int main()
    float n = 0.0;
    float p = 0.0;
    float x = 0.0;
    float result = 0.0;
    std::cout << "Please enter value of n: ";
    std::cin >> n;
    std::cout << "Please enter value of p: ";
    std::cin >> p;
    std::cout << "Please enter value of x: ";
    std::cin >> x;
    result = computeBinomialProb(float(n), float(p), float(x));
    std::cout << "result = " << result << "\n\n";
    return 0;

Sorry for no syntax highlighting I forget how to do this.
The bigest thing you can notice is that in Python you don't need all the type information which allows for really easy and quick variable declarations which actually slims the code down quite a bit. Another thing to notice is you can prompt and gather information in one go with the Python where in C++ you need to use two different streams to do so. I think the Python is much more readible but the C++ is quite crisp as well.

Nov 30 2012 01:53 PM
Nice comparison. Two comments:
A) C++11 added 'auto', so you don't need to write what type of variable a new variable is. You do still need to type 'auto' though.
B) The whole 'raw_input' vs C++ streams... 'raw_input' could just be a wrapper function around the streams (and once written, could be a permanent part of your own code base) so it isn't a pro or con of either language.

The difference would be:
n = float(raw_input("Value of n?:"))
float n = raw_input<float>("Value of n?:");
//'C' language style:
float n = raw_input_float("Value of n?:");

Python definitely has a place in game development - mostly in the higher level logic, while C++ or C would do the heavy lifting of your engine.

I scarcely know Python, but I recognize it's use and have it in my plans to learn in a few years from now. The right tool for the right job - I currently use C++ for everything, despite C++ only being the right tool for about half my code.
Nov 30 2012 02:20 PM
I agree Python does have a place just not in the core of the engine. Right now I am not sure the direction I am going to go. My end goal is learning low level 3D development just need to choose between OpenGL and Direct3D. Probably will go the OpenGL route not positive. I want to build a very basic render engine and will probably wrap Python on top of it for the logic. I have a vision for my next game it is just getting to that point. The other alternative I have is running with the UDK but my real interest is really the low level rendering process compared to the final product of the game. It is just in my nature to prefer the boring work over the actual fun stuff. I love the internal workings of a game/render system. I enjoy the process of knowing how any why things are done the way they are.

Note: GameDev.net moderates comments.