Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 192 193 [194] 195 196 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 887567 times)

dreadmullet

  • Bay Watcher
  • Inadequate Comedian
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2895 on: September 07, 2012, 10:25:31 am »

Hmm.. I'm so used to dot products being from -1 to 1. They're only that way when the vectors are normalized. Sorry.
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #2896 on: September 08, 2012, 01:43:39 am »

I've come again with another question!

Code: [Select]
class ArrayWrapper
{
    public:
        ArrayWrapper (int n)
            : _p_vals( new int[ n ] )
            , _size( n )
        {}
        // copy constructor
        ArrayWrapper (const ArrayWrapper& other)
            : _p_vals( new int[ other._size  ] )
            , _size( other._size )
        {
            for ( int i = 0; i < _size; ++i )
            {
                _p_vals[ i ] = other._p_vals[ i ];
            }
        }
        ~ArrayWrapper ()
        {
            delete [] _p_vals;
        }
    private:
    int *_p_vals;
    int _size;
};

...What are those : _p.vals() and
Code: [Select]
, _size(n)? I don't understand the colon and the comma. What does the {} do? D: Very confused and hard to understand Dx Maybe it's too hard for me right now.


Quaternions and 4x4 matrices ... what the heck do they do? :/

Also, does this mean using matrices you can rotate a point (x, y) around (0, 0) by θ? Because if so, I think I could use that.

Rather(EDIT): and are the formulas used for that rotation?
« Last Edit: September 08, 2012, 01:48:27 am by Skyrunner »
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2897 on: September 08, 2012, 01:47:23 am »

I've come again with another question!

Code: [Select]
class ArrayWrapper
{
    public:
        ArrayWrapper (int n)
            : _p_vals( new int[ n ] )
            , _size( n )
        {}
        // copy constructor
        ArrayWrapper (const ArrayWrapper& other)
            : _p_vals( new int[ other._size  ] )
            , _size( other._size )
        {
            for ( int i = 0; i < _size; ++i )
            {
                _p_vals[ i ] = other._p_vals[ i ];
            }
        }
        ~ArrayWrapper ()
        {
            delete [] _p_vals;
        }
    private:
    int *_p_vals;
    int _size;
};

...What are those : _p.vals() and
Code: [Select]
, _size(n)? I don't understand the colon and the comma. What does the {} do? D: Very confused and hard to understand Dx Maybe it's too hard for me right now.

Nah, it's pretty simple. That's an initializer list. It initializes the member objects listed by the values in the parentheses. The {} is because, even with initializer lists, the constructor still needs a body. The code is mostly the same as this:

Code: [Select]
class ArrayWrapper
{
    public:
        ArrayWrapper (int n) {
            _p_vals = new int[ n ];
            _size = n;
        }
        // copy constructor
        ArrayWrapper (const ArrayWrapper& other) {
            _p_vals = new int[ other._size  ];
            _size = other._size;
           
            for ( int i = 0; i < _size; ++i )
            {
                _p_vals[ i ] = other._p_vals[ i ];
            }
        }
        ~ArrayWrapper ()
        {
            delete [] _p_vals;
        }
    private:
    int *_p_vals;
    int _size;
};

I say mostly because there are a few differences between typical constructors and initializer lists. I'll let someone else with a bit more knowledge on the difference explain it.

ECrownofFire

  • Bay Watcher
  • Resident Dragoness
    • View Profile
    • ECrownofFire
Re: if self.isCoder(): post() #Programming Thread
« Reply #2898 on: September 09, 2012, 12:28:47 pm »

Initializer lists are faster and atomic.

Atomic means an initializer list cannot be interrupted by another thread. When creating a new object, the constructor takes time to execute, which is important in multi-threading because if another thread requests that object, it might not be fully constructed.

Initializer lists are also faster to execute, because it takes one step (initializing a variable to a value) instead of two (initializing the variable, then setting it within the constructor).
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2899 on: September 10, 2012, 03:08:32 am »

Initializer lists are also faster to execute, because it takes one step (initializing a variable to a value) instead of two (initializing the variable, then setting it within the constructor).
I have heard that a smart compiler can see that so this doesn't matter as much. The atomic part is a lot more important, though, didn't even know that (just got into the multithreading).
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #2900 on: September 10, 2012, 03:24:05 am »

I'm still wondering what multithreading is and how to do it. o_o


This Boost thingy I keep hearing about sounds awesome, but I haven't attempted it yet :V
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

malloc

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2901 on: September 10, 2012, 10:30:10 am »

I'm still wondering what multithreading is and how to do it. o_o
More code running simultaneously, useful if you have more cores to execute code on. How to do it? Add some code entry points and feed those to some more threads. Or do it the weird way and have some threadpool you can feed jobs to.

Anyhow, I need to work on this :c

If you are using the new version of c++ you can auto pretty much anything. No need to declare those long iterator names.
Code: [Select]
std::vector<std::pair<std::string, std::vector>> container;
//Instead of
typedef std::vector<std::pair<std::string, std::vector>>::iterator iteratorType;
iteratorType pos = container.begin();

//Just write
auto pos = container.begin();

//You will enjoy c++ much more by using those features.


« Last Edit: September 10, 2012, 10:36:38 am by malloc »
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2902 on: September 10, 2012, 10:51:10 am »

True, but the error messages will still return these huge, sometimes generated constructor and function definitions. They take a bit of getting used to, if you're more used to helpful error messages telling you exactly what went wrong where, using code in their lines that you actually wrote.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2903 on: September 10, 2012, 01:50:54 pm »

True, but the error messages will still return these huge, sometimes generated constructor and function definitions. They take a bit of getting used to, if you're more used to helpful error messages telling you exactly what went wrong where, using code in their lines that you actually wrote.

stlfilt is one of the greatest tools ever written.

ECrownofFire

  • Bay Watcher
  • Resident Dragoness
    • View Profile
    • ECrownofFire
Re: if self.isCoder(): post() #Programming Thread
« Reply #2904 on: September 10, 2012, 03:08:05 pm »

I'm still wondering what multithreading is and how to do it. o_o
Multithreading is basically the idea of having multiple processes running simultaneously. Unlike a single-threaded process, you run into a lot of problems when you start multithreading because they act like completely separate processes. What happens if one thread needs to read some RAM and another thread wants to write to it? Or even worse, two things want to write to one place. This is called a "race condition" and it leads to very unpredictable behavior. They involve "atomic" operations, which cannot be interrupted and are effectively simultaneous in the eyes of the CPU and any other threads running on it. Something is called "threadsafe" if it will not do horrible things if other threads are involved.

So multithreading seeks to eliminate race conditions either by using locks, in which a thread asks "does anybody else need this?" before they go on. But locks are slow, dangerous, and have many other problems. If two separate threads want a lock that the other has, it's a deadlock, and the threads wait forever. If a thread locks something twice, some implementations can cause it to be locked forever. Any kind of bug is extremely hard to replicate because they are dependent on exactly what the rest of the computer is doing. And lastly, they just add overhead to everything because a thread asks for a lock every time it tries to access something.

So there has been a lot of research into using "non-blocking" algorithms. These usually include CPU instructions like CAS (Compare And Swap, checks to see if the register/RAM is equal to A, if it is, then it sets it to B). There are a few algorithms that don't even need specific atomic operations, but those need to be specifically implemented and are very limited.

On that note, it's always a good idea to use multithreading only when necessary. For example, feeding data to the GPU should only be done by one thread, just use a simple queue to feed data to it. Queues are threadsafe if properly implemented, Intel's TBB has an example of this.
Logged

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2905 on: September 10, 2012, 06:54:32 pm »

so, today I was filling out a job application and I found that I used a ; instead of a .
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2906 on: September 10, 2012, 07:16:00 pm »

so, today I was filling out a job application and I found that I used a ; instead of a .

I often have that problem;

kaenneth

  • Bay Watcher
  • Catching fish
    • View Profile
    • Terrible Web Site
Re: if self.isCoder(): post() #Programming Thread
« Reply #2907 on: September 14, 2012, 06:42:04 pm »

Was it for a programming job?

Anyway, another fun thing that makes multithreading harder is that there is no guarantee that the CPU will perform operations in the order you code them in...

if you say: a=b+c; d=e*f; a modern CPU might internally decide to do the multiplication first instead.

(however, if they are dependent, like a=b+c; d=a*a it won't.)

Some mutilthreading is easy, like walking and chewing gum at the same time. Some is harder, like juggling apples while taking bites out of them while playing hopscotch while dodging knives being thrown at you.
Logged
Quote from: Karnewarrior
Jeeze. Any time I want to be sigged I may as well just post in this thread.
Quote from: Darvi
That is an application of trigonometry that never occurred to me.
Quote from: PTTG??
I'm getting cake.
Don't tell anyone that you can see their shadows. If they hear you telling anyone, if you let them know that you know of them, they will get you.

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2908 on: September 14, 2012, 11:18:16 pm »

Was it for a programming job?
Some mutilthreading is easy, like walking and chewing gum at the same time. Some is harder, like juggling apples while taking bites out of them while playing hopscotch while dodging knives being thrown at you.

Very good analogy,
I'll have to remember that one,

and no, it wasn't for a programming job, I'm not good enough yet to even attempt to apply for one, I should really go back to studying, I've taken a month or so break from it.
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2909 on: September 14, 2012, 11:47:32 pm »

Haha, I'm trying to learn C++ now the same way I learned how to mod DF: using a shitload of online resources and referencing the code of pre-existing software made by those more skilled than me.

Except instead of DF wiki it's google and various C++ tutorials and instead of Deon's Genesis Mod it's SLASH'EM.
Pages: 1 ... 192 193 [194] 195 196 ... 796