Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
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;

Adbot
ADBOT LOVES YOU

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

Fuzzy Mammal
Aug 15, 2001

Lipstick Apathy
python rules because it uses the best sort, timsort.

Fuzzy Mammal
Aug 15, 2001

Lipstick Apathy

hackbunny posted:

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

  • Locked thread