- Fuzzy Mammal
- Aug 15, 2001
-
-
Lipstick Apathy
|
we can all become better shitcoders with a coding lesson!
came across a fun little fuckup today. what are the two bugs in this code??
code:uint32 jerkiness = diff_sec ?
(listener.frame_count() / static_cast<uint32>(diff_sec)) : 0;
|
#
¿
May 9, 2013 00:24
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
¿
May 2, 2024 05:58
|
|
- Fuzzy Mammal
- Aug 15, 2001
-
-
Lipstick Apathy
|
ok the answer is
1) diff_sec is a float so it can evaluate to nonzero path but still round to zero in the cast and kaboom
2) jerkiness is a measure of differences in frame intervals, while frame_count / time is a rate. Since 30fps ~= 33ms nobody noticed that the jerkiness value was measuring totally the wrong thing
|
#
¿
May 9, 2013 00:32
|
|
- Fuzzy Mammal
- Aug 15, 2001
-
-
Lipstick Apathy
|
python rules because it uses the best sort, timsort.
|
#
¿
May 9, 2013 05:27
|
|
- Fuzzy Mammal
- Aug 15, 2001
-
-
Lipstick Apathy
|
C++ code:#include <algorithm>
template<class RandomIt, class Compare, class URNG>
void bogosort(RandomIt first, RandomIt last, Compare comp, URNG&& g) {
while (!std::is_sorted(first, last, comp)) {
std::shuffle(first, last, g);
}
}
use like this:
C++ code:#include <algorithm>
#include <iterator>
#include <random>
#include <iostream>
#include <functional>
template<class RandomIt, class Compare, class URNG>
void bogosort(RandomIt first, RandomIt last, Compare comp, URNG&& g) {
while (!std::is_sorted(first, last, comp)) {
std::shuffle(first, last, g);
}
}
template<class RandomIt, class URNG>
void bogosort(RandomIt first, RandomIt last, URNG&& g) {
bogosort(first, last, std::less<typename std::iterator_traits<RandomIt>::value_type>{}, g);
}
int main() {
std::random_device rd;
std::mt19937 g{rd()};
std::vector<int> v;
std::uniform_int_distribution<> dis{0, 99};
std::generate_n(std::back_inserter(v), 10, [&]() { return dis(g); });
std::cout << "Before:" << "\n";
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
bogosort(v.begin(), v.end(), g);
std::cout << "After:" << "\n";
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
}
no gratuitous use of std::endl 4/5
|
#
¿
May 6, 2016 19:52
|
|