Page 179 of 251

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 9:40 pm UTC
by ahammel
Fancy wrote:
ahammel wrote:In what way is kernel style not Python compliant?

Tabs are 8 wide. I tried it, and it didn't work. I'll go find some example code and try again.

Tab stop is a display thing, not a difference in the underlying file.

Edit: I'm quite wrong about the above, but the Python interpreter will happily parse an 8-space tab, I promise you. Mixed tabs and spaces is a syntax error in py3k, though.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 9:45 pm UTC
by Fancy
ahammel wrote:
Fancy wrote:
ahammel wrote:In what way is kernel style not Python compliant?

Tabs are 8 wide. I tried it, and it didn't work. I'll go find some example code and try again.

Tab stop is a display thing, not a difference in the underlying file.

Edit: I'm quite wrong about the above, but the Python interpreter will happily parse an 8-space tab, I promise you. Mixed tabs and spaces is a syntax error in py3k, though.

I don't even know. Now I'm thinking about ditching kernel style because this:

Code: Select all

if this
    if another thing
        then this
else
    this other thing


This gets all janked up and leads to buggy code. For a while I used 1TBS, and it was nice.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 10:15 pm UTC
by Thesh
Allman is obviously the superior indent style.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 10:31 pm UTC
by Fancy
Thesh wrote:Allman is obviously the superior indent style.

All that wasted whitespace...my small terminal, with 20% of it wasted on whitespace?

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 10:56 pm UTC
by Thesh
Unless you put everything on the same line and word wrap, most of your terminal will be whitespace regardless of the indent style. If you have a tiny terminal, that's your fault.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 11:23 pm UTC
by Jplus
On top of what all others have already said...

Fancy wrote:I don't like Python because "ha indents actually matter" and this makes it a pain to edit.

It isn't a pain to edit if you use a proper editor with automatic indentation. The best editors even let you set the tab width on a per-language base.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 11:40 pm UTC
by Fancy
I'm not sure if I should keep trying with C. Maybe I should try C++, or give up the computer hobby alotgether.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 11:43 pm UTC
by Dason
Fancy wrote:I'm not sure if I should keep trying with C. Maybe I should try C++, or give up the computer hobby alotgether.

Nah. Honestly if you use a better editor I don't think you'll have any issues with python. With that said my language of choice is R but I wouldn't necessarily recommend it as a first language unless you're planning on doing data analysis.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 11:44 pm UTC
by Fancy
Dason wrote:
Fancy wrote:I'm not sure if I should keep trying with C. Maybe I should try C++, or give up the computer hobby alotgether.

Nah. Honestly if you use a better editor I don't think you'll have any issues with python. With that said my language of choice is R but I wouldn't necessarily recommend it as a first language unless you're planning on doing data analysis.

But I'm just horrible at this. Bad at C, bad at Python, bad at bash scripting, web design.

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 11:46 pm UTC
by Dason
Fancy wrote:
Dason wrote:
Fancy wrote:I'm not sure if I should keep trying with C. Maybe I should try C++, or give up the computer hobby alotgether.

Nah. Honestly if you use a better editor I don't think you'll have any issues with python. With that said my language of choice is R but I wouldn't necessarily recommend it as a first language unless you're planning on doing data analysis.

But I'm just horrible at this. Bad at C, bad at Python, bad at bash scripting, web design.

Everybody is horrible at first. Don't get down. Have you found any of it enjoyable yet? Why do you want to program in the first place?

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 11:48 pm UTC
by Fancy
Dason wrote:
Fancy wrote:
Dason wrote:
Fancy wrote:I'm not sure if I should keep trying with C. Maybe I should try C++, or give up the computer hobby alotgether.

Nah. Honestly if you use a better editor I don't think you'll have any issues with python. With that said my language of choice is R but I wouldn't necessarily recommend it as a first language unless you're planning on doing data analysis.

But I'm just horrible at this. Bad at C, bad at Python, bad at bash scripting, web design.

Everybody is horrible at first. Don't get down. Have you found any of it enjoyable yet? Why do you want to program in the first place?

I've been horrible at it for two years. All I want to do is have a useful skill that I enjoy and can make a career out of, and here I am writing "hello world" all over again

Re: Coding: Fleeting Thoughts

Posted: Tue Jul 15, 2014 11:59 pm UTC
by Dason
Have you had any concrete goals during that time? A task you would like to achieve via programming?

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 12:02 am UTC
by Fancy
Dason wrote:Have you had any concrete goals during that time? A task you would like to achieve via programming?

Lots of things I know I'll never do. I'd like to contribute to some open-source, is one.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 12:08 am UTC
by Dason
Fancy wrote:
Dason wrote:Have you had any concrete goals during that time? A task you would like to achieve via programming?

Lots of things I know I'll never do. I'd like to contribute to some open-source, is one.

That doesn't sound like a concrete goal quite yet.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 12:11 am UTC
by Fancy
Dason wrote:
Fancy wrote:
Dason wrote:Have you had any concrete goals during that time? A task you would like to achieve via programming?

Lots of things I know I'll never do. I'd like to contribute to some open-source, is one.

That doesn't sound like a concrete goal quite yet.

I'd like to contribute to the Linux kernel, if you're looking for something specific.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 12:12 am UTC
by Dason
Fancy wrote:
Dason wrote:
Fancy wrote:
Dason wrote:Have you had any concrete goals during that time? A task you would like to achieve via programming?

Lots of things I know I'll never do. I'd like to contribute to some open-source, is one.

That doesn't sound like a concrete goal quite yet.

I'd like to contribute to the Linux kernel, if you're looking for something specific.

That sounds interesting but why do you want to contribute to the kernel? Is there some feature that you think should be implemented that currently isn't?

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 12:14 am UTC
by Fancy
Dason wrote:
Fancy wrote:
Dason wrote:
Fancy wrote:
Dason wrote:Have you had any concrete goals during that time? A task you would like to achieve via programming?

Lots of things I know I'll never do. I'd like to contribute to some open-source, is one.

That doesn't sound like a concrete goal quite yet.

I'd like to contribute to the Linux kernel, if you're looking for something specific.

That sounds interesting but why do you want to contribute to the kernel? Is there some feature that you think should be implemented that currently isn't?

I think something unifying that brings all the distros together. I know that the kernel already does that, but something more. Maybe a way to install the kernel and mange multiple partitions with multiple distros, but I assume that is impossible.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 12:26 am UTC
by Dason
Fancy wrote:
Dason wrote:
Fancy wrote:
Dason wrote:
Fancy wrote:
Dason wrote:Have you had any concrete goals during that time? A task you would like to achieve via programming?

Lots of things I know I'll never do. I'd like to contribute to some open-source, is one.

That doesn't sound like a concrete goal quite yet.

I'd like to contribute to the Linux kernel, if you're looking for something specific.

That sounds interesting but why do you want to contribute to the kernel? Is there some feature that you think should be implemented that currently isn't?

I think something unifying that brings all the distros together. I know that the kernel already does that, but something more. Maybe a way to install the kernel and mange multiple partitions with multiple distros, but I assume that is impossible.

It sounds like you're thinking way too abstract and I'm not sure you're sure what you're looking to do. Honestly it would probably be better to start with something a little smaller - make a concrete goal that is actually attainable (no offense to your other goal... you might be able to do that once you're more familiar with programming and the kernel in general). I know one thing I like to do is program a minesweeper clone when I'm learning a language. I start small by making the data structure to represent the game and its current state and then add some functions that modify it for a guess, then implement a simple console based version of the game and then move on to adding graphics. It makes a decent progression for learning some pieces of the language in my opinion.

I try to use the language to do some simple tasks too like rename a bunch of files in a folder according to a better naming convention. Anytime you have a repetitive task you should ask "how can I have the computer do this for me instead". I've done things like had code generate what readings I would need to do for a bible reading plan on any given day and use that to create a calendar in google cal that has the reading for the day as an event. I've made some fun fractal generators for the hell of it. I'm not a great programmer - but I find that having an actual task in mind really helps.

I know there was a thread or two documenting somebody's (I forget who and am too lazy to search) goal of programming a simple rogue-like game. That might be good to look through as well.

Most of these tasks, by the way, are much easier to think about and implement in a language like Python. C probably isn't the best place to start when learning how to think like a programmer. The main thing to learn is how to take a big task and break it down into smaller pieces that are fairly simple to do.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 12:27 am UTC
by Fancy
Dason wrote:
Fancy wrote:
Dason wrote:
Fancy wrote:
Dason wrote:
Fancy wrote:
Dason wrote:Have you had any concrete goals during that time? A task you would like to achieve via programming?

Lots of things I know I'll never do. I'd like to contribute to some open-source, is one.

That doesn't sound like a concrete goal quite yet.

I'd like to contribute to the Linux kernel, if you're looking for something specific.

That sounds interesting but why do you want to contribute to the kernel? Is there some feature that you think should be implemented that currently isn't?

I think something unifying that brings all the distros together. I know that the kernel already does that, but something more. Maybe a way to install the kernel and mange multiple partitions with multiple distros, but I assume that is impossible.

It sounds like you're thinking way too abstract and I'm not sure you're sure what you're looking to do. Honestly it would probably be better to start with something a little smaller - make a concrete goal that is actually attainable (no offense to your other goal... you might be able to do that once you're more familiar with programming and the kernel in general). I know one thing I like to do is program a minesweeper clone when I'm learning a language. I start small by making the data structure to represent the game and its current state and then add some functions that modify it for a guess, then implement a simple console based version of the game and then move on to adding graphics. It makes a decent progression for learning some pieces of the language in my opinion.

I try to use the language to do some simple tasks too like rename a bunch of files in a folder according to a better naming convention. Anytime you have a repetitive task you should ask "how can I have the computer do this for me instead". I've done things like had code generate what readings I would need to do for a bible reading plan on any given day and use that to create a calendar in google cal that has the reading for the day as an event. I've made some fun fractal generators for the hell of it. I'm not a great programmer - but I find that having an actual task in mind really helps.

I know there was a thread or two documenting somebody's (I forget who and am too lazy to search) goal of programming a simple rogue-like game. That might be good to look through as well.

Most of these tasks, by the way, are much easier to think about and implement in a language like Python. C probably isn't the best place to start when learning how to think like a programmer. The main thing to learn is how to take a big task and break it down into smaller pieces that are fairly simple to do.

I can't do python either. Too hard.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 12:30 am UTC
by Dason
If python is too hard then I don't know how you think you'll have a chance with C.

What exactly do you find hard about python? You mentioned the forced indentation before but honestly that shouldn't be an issue. If you use a decent editor it won't cause an issue and it will force you to make your code more readable. Indentation is something you should be doing anyways.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 12:32 am UTC
by korona
Coding low level stuff like kernels can be a very enjoyable exercise. I wrote a small operating system some years ago. (well it grew quite big - about 35k sloc - in particular because I built an own language and tool chain with compiler, assembler and linker for it).

However starting programming with low level stuff does not make much sense or at least it will have a very steep learning curve. I recommend coding some simple but interesting high level programs. I always found games to be fun to write and they will teach you a lot of skills that doing PE problems won't. You could code some text adventures or maybe a Tetris or Pacman clone.

About the language: If you want to get into low level stuff staying with C is perfectly fine. If you start with a higher level language you might have to unlearn a lot of stuff like automatic memory management, exception handling, RTTI/reflection, dynamic binding or non-trivial property accessors.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 1:17 am UTC
by KnightExemplar
Fancy wrote:
Dason wrote:
Fancy wrote:I'm not sure if I should keep trying with C. Maybe I should try C++, or give up the computer hobby alotgether.

Nah. Honestly if you use a better editor I don't think you'll have any issues with python. With that said my language of choice is R but I wouldn't necessarily recommend it as a first language unless you're planning on doing data analysis.

But I'm just horrible at this. Bad at C, bad at Python, bad at bash scripting, web design.


Then get less bad. It took a few years when I started before I got anything useful done. (Where useful was dumb little calculator programs for various math classes). Have you tried writing a quadratic-equation solver? That sort of thing. I mean, we can cheer-lead all day long, but that's not the issue. The issue is you writing more programs. Even dumb programs that are less than 20 lines of code are worthwhile.

When you're comfortable with dumb stuff, write a more complicated piece of code. Write command-line Tic-Tac-Toe and other very simple games (guess the number). Then write more complicated stuff. Recreate Tetris or 2048 or Flappy Bird. Then you'd be set from there on out.

Sitting around writing "Hello World" for years doesn't help. You've got to move up and stretch your brain.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 1:23 am UTC
by Fancy
Dason wrote:If python is too hard then I don't know how you think you'll have a chance with C.

What exactly do you find hard about python? You mentioned the forced indentation before but honestly that shouldn't be an issue. If you use a decent editor it won't cause an issue and it will force you to make your code more readable. Indentation is something you should be doing anyways.

Python is just hard. Indetation is fine. but not when it affects the actual program/

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 1:24 am UTC
by Fancy
KnightExemplar wrote:
Fancy wrote:
Dason wrote:
Fancy wrote:I'm not sure if I should keep trying with C. Maybe I should try C++, or give up the computer hobby alotgether.

Nah. Honestly if you use a better editor I don't think you'll have any issues with python. With that said my language of choice is R but I wouldn't necessarily recommend it as a first language unless you're planning on doing data analysis.

But I'm just horrible at this. Bad at C, bad at Python, bad at bash scripting, web design.


Then get less bad. It took a few years when I started before I got anything useful done. (Where useful was dumb little calculator programs for various math classes). Have you tried writing a quadratic-equation solver? That sort of thing. I mean, we can cheer-lead all day long, but that's not the issue. The issue is you writing more programs. Even dumb programs that are less than 20 lines of code are worthwhile.

When you're comfortable with dumb stuff, write a more complicated piece of code. Write command-line Tic-Tac-Toe and other very simple games (guess the number). Then write more complicated stuff. Recreate Tetris or 2048 or Flappy Bird. Then you'd be set from there on out.

Sitting around writing "Hello World" for years doesn't help. You've got to move up and stretch your brain.


there is no place to move up to. I don't know anything useful, I just keep restarting books.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 1:40 am UTC
by Thesh
Well, if you are doing hello world programs, move further:

Implement Insertion Sort, Merge Sort, and QuickSort
Implement a CSV Parser
Implement a linked-list
Implement Insertion-Sort and in-place Merge Sort on the linked list
Implement a hash table with insert, delete, and find operations
Implement a binary tree with insert, delete, and find operations
Write a program to solve the 8-queens puzzle
Write a Sudoku Solver

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 1:42 am UTC
by Fancy
Thesh wrote:Well, if you are doing hello world programs, move further:

Implement Insertion Sort, Merge Sort, and QuickSort
Implement a CSV Parser
Implement a linked-list
Implement Insertion-Sort and in-place Merge Sort on the linked list
Implement a hash table with insert, delete, and find operations
Implement a binary tree with insert, delete, and find operations
Write a program to solve the 8-queens puzzle
Write a Sudoku Solver

You think I'm demotivated now, wait til I try those.
I wrote insertion sort. It took 3 fucking weeks. I begged and pleaded for help, and everybody jsut kept saying my mistake was obvious. When I finally got a fucking answer, some dicknugget completely rewrote my code and just said "here ya go, just a slight modification ;)" and it went from being an insertion sort to some hellish abomination.
And then I came hre, hoping that it would be different, but for fuck sake look at you fucks. "Look, all you gotta do is write a flimfloop inter-piler that gazoonks the kerschnuckle." "Oh, don't read books, not helpful." How the fuck do you learn the language without reading acctual books you fuckstick? Jesus christ, assholes.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 1:54 am UTC
by phlip
Fancy: Are you actually looking for help or advice with how to get into coding? Or are you just looking for a place to rant about how much trouble you're having with it?

Either is fine, but it would be good to be clearer about exactly what you're after here... otherwise people are going to keep giving you advice you may not want, and that's just going to be a bad time for both of us.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 1:57 am UTC
by Fancy
I'm looking for a community of people who are NOT assholes who are also into computer things. Those are not overlapping sets, apparently. StackExchange is assholes, IRC is assholes, you're all assholes.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 2:18 am UTC
by phlip
I'm not seeing any assholery so far in this discussion...

People have made a number of suggestions for you - try a language like Python over C as it's easier for beginners, and any number of suggestions of things you could try to code as practise. These are not "asshole" things, these are people trying to be helpful.

All of those suggestions, you've immediately rejected. Which is fine, you have your reasons, and not every suggestion is going to be useful to everyone. People may disagree with some of those reasons, or try to offer additional suggestions to mitigate them (like using a programmer-focused text editor to alleviate the problems you have with Python's indenting rules). This is still not assholery. This is people seeing you say you have a problem, and offering ways to either reduce, or work around the problem.

But when every suggestion that is made gets immediately turned down... that's when I start to think that you're probably not after actual advice... but just want a place to vent. Which is also fine... I know I get in that state from time to time, where I just want to complain about a thing, and don't want to be offered advice on how to fix it from people who don't know the whole story... and I don't feel like explaining the whole story to anyone... and then the advice just makes me angrier, until it all ends up a miserable time for everyone. Which is why I asked whether you're actually after advice here. (In the Rant thread in General, for instance, it has actually become the de facto norm to not offer advice unless someone actually asks for it, for exactly this reason. Unfortunately we don't have a rant thread here in Coding, it all gets rolled up into this FT thread, so it can be hard to tell what you're after without asking.)

All that said... if there's something I am missing that you think is someone being an asshole, please feel free to point it out (ideally via private message, or by reporting the post in question) and I'll look into it.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 2:23 am UTC
by Fancy
phlip wrote:I'm not seeing any assholery so far in this discussion...

People have made a number of suggestions for you - try a language like Python over C as it's easier for beginners, and any number of suggestions of things you could try to code as practise. These are not "asshole" things, these are people trying to be helpful.

All of those suggestions, you've immediately rejected. Which is fine, you have your reasons, and not every suggestion is going to be useful to everyone. People may disagree with some of those reasons, or try to offer additional suggestions to mitigate them (like using a programmer-focused text editor to alleviate the problems you have with Python's indenting rules). This is still not assholery. This is people seeing you say you have a problem, and offering ways to either reduce, or work around the problem.

But when every suggestion that is made gets immediately turned down... that's when I start to think that you're probably not after actual advice... but just want a place to vent. Which is also fine... I know I get in that state from time to time, where I just want to complain about a thing, and don't want to be offered advice on how to fix it from people who don't know the whole story... and I don't feel like explaining the whole story to anyone... and then the advice just makes me angrier, until it all ends up a miserable time for everyone. Which is why I asked whether you're actually after advice here. (In the Rant thread in General, for instance, it has actually become the de facto norm to not offer advice unless someone actually asks for it, for exactly this reason. Unfortunately we don't have a rant thread here in Coding, it all gets rolled up into this FT thread, so it can be hard to tell what you're after without asking.)

All that said... if there's something I am missing that you think is someone being an asshole, please feel free to point it out (ideally via private message, or by reporting the post in question) and I'll look into it.

Like it fucking matters any more. Dick fuck shit cunt fuck. "Try Python" Python is some ullshit that doesn't teach anything I want to know.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 3:01 am UTC
by Fancy
I'd like to offer a formal apology for my festival back there.
It'll porbably happen again next week.

So let me ask a good question: What books are there on C that are good? Oh, and if I start a new book, do I have to start over form the beginning?

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 3:09 am UTC
by brenok
Fancy wrote:I'd like to offer a formal apology for my festival back there.
It'll porbably happen again next week.

A false apology is worse than no apology, y'know.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 3:13 am UTC
by SecondTalon
In our PM conversation, I was actually serious about taking a day or two off from the forum. Not mod "take a day off" just as another person, but.... it'd be a good idea to take a day or two off.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 3:46 am UTC
by Fancy
SecondTalon wrote:In our PM conversation, I was actually serious about taking a day or two off from the forum. Not mod "take a day off" just as another person, but.... it'd be a good idea to take a day or two off.

Oh, but alas, what will I do without glorious internet to entertain me?

I only visit a limited number of sites, but fine. Maybe I'll check out reddit.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 5:17 am UTC
by NecklaceOfShadow
...on a completely different topic, is this snippet vanilla C, or is it a compiler extension? gcc, clang, and tcc all give the same output.

Code: Select all

#include <stdio.h>

int main(void) {
    int x = 1, y = 0;

    switch(x) {
        case 0 ... 5:
            y = 1;
            break;
    }

    printf("x = %d, y = %d\n", x, y); /* prints out 'x = 1, y = 1' */
} 

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 5:27 am UTC
by Volcano99
Apparently that's a GCC extension. Other compilers may have picked up on it (is that a real english expression?)
https://gcc.gnu.org/onlinedocs/gcc-4.1. ... anges.html

According to this post [0], compiling with -pedantic will give you an error.

[0] http://stackoverflow.com/questions/5924 ... andard-c-c

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 5:40 am UTC
by NecklaceOfShadow
Volcano99 wrote:Apparently that's a GCC extension. Other compilers may have picked up on it (is that a real english expression?)
https://gcc.gnu.org/onlinedocs/gcc-4.1. ... anges.html

According to this post [0], compiling with -pedantic will give you an error.

[0] http://stackoverflow.com/questions/5924 ... andard-c-c

Damn. It seems like a nifty feature, but I'm not really comfortable using it if it's compiler dependent.

And yes, that's a real english expression.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 1:05 pm UTC
by Diadem
brenok wrote:
Fancy wrote:I'd like to offer a formal apology for my festival back there.
It'll porbably happen again next week.

A false apology is worse than no apology, y'know.

This is a complete and utter off-topic tangent (but it is 'fleeting thoughts' so I hope that's ok), but I disagree.

A false apology shows that at the very least you care about that persons opinion. Even if an apology is known by all parties to be completely insincere, it is still an olive branch. You're leaving a door open for further contact.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 2:12 pm UTC
by Yakk
So I was coding up a FFT for no reason in particular (code golf), and did some googling and ran into this:
http://rosettacode.org/wiki/Fast_Fourie ... rm#C.2B.2B

Code: Select all

#include <complex>
#include <iostream>
#include <valarray>
 
const double PI = 3.141592653589793238460;
 
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;
 
// Cooley–Tukey FFT (in-place)
void fft(CArray& x)
{
    const size_t N = x.size();
    if (N <= 1) return;
 
    // divide
    CArray even = x[std::slice(0, N/2, 2)];
    CArray  odd = x[std::slice(1, N/2, 2)];
 
    // conquer
    fft(even);
    fft(odd);
 
    // combine
    for (size_t k = 0; k < N/2; ++k)
    {
        Complex t = std::polar(1.0, -2 * PI * k / N) * odd[k];
        x[k    ] = even[k] + t;
        x[k+N/2] = even[k] - t;
    }
}
 
// inverse fft (in-place)
void ifft(CArray& x)
{
    // conjugate the complex numbers
    x = x.apply(std::conj);
 
    // forward fft
    fft( x );
 
    // conjugate the complex numbers again
    x = x.apply(std::conj);
 
    // scale the numbers
    x /= x.size();
}
 

That's beautiful.

Re: Coding: Fleeting Thoughts

Posted: Wed Jul 16, 2014 3:24 pm UTC
by letterX
So, the code claims to be an in-place FFT, but from looking at the documentation for valarray, the lines:

Code: Select all

    CArray even = x[std::slice(0, N/2, 2)];
    CArray  odd = x[std::slice(1, N/2, 2)];

seem to create a copy of the data? Maybe I just misunderstand valarray and slices, though.