BTW, mostly for my own personal reference, this is the tool I used to structure my last code blocks into HTML:
http://codeformatter.blogspot.com
Now it's off to zzzz-land.
2018-09-04
Fibonacci sequence, raytracing, templates, and constexpr... oh my!
So I am working my way through the beta of Jamis Buck's incredible "The Ray Tracer Challenge:"
https://pragprog.com/book/jbtracer/the-ray-tracer-challenge
(He's the guy who wrote the best book I've ever read on maze algorithms. His writing style is casual, funny, and a pleasure to read, unlike many other computer science books which are as dry as a stale cracked and agonizing.)
Anyways, it's been a great opportunity for me. I've been working in C++, since I want to improve my C++11, C++14, and C++17 (since the last time I used C++ was probably in 2007, so I had a lot to learn). I'm trying to make implementations of matrix and vector as general as possible, and as much as I can, constexpr everything, which is proving to be difficult. It can be quite surprising what the compiler allows as constexpr and what it doesn't. (The idea of constexpr is that computation happens at compile time rather than run time. This substantially reduces your run time at the cost of somewhat increasing how long compilation takes.)
C++11 brought some interesting concepts to the table and was a huge leap forward from C++98, but that puts me in a position where I really have to catch up and burrow through C++11, C++14, and C++17. The language improvements are fantastic, but in some cases (e.g. constexpr koff), very challenging.
Here's some C++ code for calculating the Nth Fibonacci number:
Now, that works and the fib numbers are calculated at compile time, but it's hardly elegant. Fortunately, with relaxations being made to the constexpr keyword, in C++, this code reduces down to a single method that's much easier to parse:
In conclusion, use the most recent version of C++ at your disposal, You'll thank me for it later.
Happy programming, all!
https://pragprog.com/book/jbtracer/the-ray-tracer-challenge
(He's the guy who wrote the best book I've ever read on maze algorithms. His writing style is casual, funny, and a pleasure to read, unlike many other computer science books which are as dry as a stale cracked and agonizing.)
Anyways, it's been a great opportunity for me. I've been working in C++, since I want to improve my C++11, C++14, and C++17 (since the last time I used C++ was probably in 2007, so I had a lot to learn). I'm trying to make implementations of matrix and vector as general as possible, and as much as I can, constexpr everything, which is proving to be difficult. It can be quite surprising what the compiler allows as constexpr and what it doesn't. (The idea of constexpr is that computation happens at compile time rather than run time. This substantially reduces your run time at the cost of somewhat increasing how long compilation takes.)
C++11 brought some interesting concepts to the table and was a huge leap forward from C++98, but that puts me in a position where I really have to catch up and burrow through C++11, C++14, and C++17. The language improvements are fantastic, but in some cases (e.g. constexpr koff), very challenging.
Here's some C++ code for calculating the Nth Fibonacci number:
1: #include <iostream>
2:
3: template<>
4: struct fibstruct<1> {
5: constexpr static size_t value() {
6: return 1;
7: }
8: };
9:
10: template<>
11: struct fibstruct<0> {
12: constexpr static size_t value() {
13: return 1;
14: }
15: };
16:
17: int main() {
18: std::cout << "Fib(100) = " << fibstruct<100>::value() << '\n';
19: }
Now, that works and the fib numbers are calculated at compile time, but it's hardly elegant. Fortunately, with relaxations being made to the constexpr keyword, in C++, this code reduces down to a single method that's much easier to parse:
1: #include <iostream>
2:
3: template<size_t N>
4: constexpr unsigned long fib() {
5: if constexpr(N < 2) return 1;
6: else return fib<N-1>() + fib<N-2>();
7: }
8:
9: int main()
10: {
11: constexpr unsigned long fb = fib<100>();
12: std::cout << "Fib(100) = " << fb << '\n';
13: }
In conclusion, use the most recent version of C++ at your disposal, You'll thank me for it later.
Happy programming, all!
Subscribe to:
Comments (Atom)