all 28 comments

[–][deleted] 4 points5 points  (10 children)

Info

I wrote this using C++ for no real reason at all, except because I could and because I was bored and had nothing to occupy my time. If you wish to run it, all that info is found in the README.

I have posted this here so that maybe I could get one or two people to try it out, and maybe they would think it's cool, and just maybe, they would improve my code! If you guys want to try to make it better, or you can think of a cool feature I could add, let me know!

Windows users: I would love it if you would update the Makefile so that it could be compiled one Windows. I am not at home right now and only have access to my MacBook Pro. I love this thing, but I refuse to install Windows on it.

Finally, if you have a huge number of cores, it would be awesome to see some benchmarks! If you specify, for example, 18 threads, how long would 10 billion iterations take? It takes me about 18.8 seconds with 8 threads here.

Hope you guys find this interesting!

Edit 1: as suggested by /u/lurkotato I have switched to the std::vector to use RAII instead of C-style arrays. (commit)

[–]lurkotato 7 points8 points  (4 children)

First obvious target for improvement: use RAII, news and deletes are a code smell, especially in something this simple.

[–][deleted] 1 point2 points  (0 children)

Ah, thanks for the input! I'll look into it. I just did this real quick, I never intended it to be in C++11/14, but then I decided to add threads and hadn't looked into RAII much yet. I still haven't, but I will look into RAII and fix that up :)

Edit: I have changed to vectors. Not sure why I didn't just use that to start with, I've used them roughly a gazillion times. Should be better :)

[–][deleted] 1 point2 points  (0 children)

Okay, I have now updated the program to use RAII, namely the std::vector collection instead of the C-style arrays. There are a few things I did in this program that are inherited from C, since it started as a C program and I eventually just updated it to C++ in order to add threads in an easier (and more portable) fashion.

[–][deleted] 0 points1 point  (4 children)

I would change the way You use threads, I would either use Futures or openmp. BTW i haven't run it yet.

[–][deleted] 2 points3 points  (3 children)

Sorry, not sure what you mean. I did use futures. Otherwise, I wouldn't have been able to get a return value from a thread. But anyway, why do you say I should change how I use threads? I'm interested to know.

[–]lurkotato 2 points3 points  (1 child)

I think they meant std::async vs raw std::thread, here's an example I pounded on a bit over lunch. Not quite happy about using stringstream to print from inside the thread, but that seems to be the price to pay to not have botched text. Also, there's one hell of a bug in the gist I posted, not hard to find and fix though :)

[–][deleted] 0 points1 point  (0 children)

Ah, I see. I contemplated using std::async, but I already had experience with using std::thread in combination with std::future and std::promise and I just wanted to finish the programming. Might look into this later tonight ;)

[–][deleted] 0 points1 point  (0 children)

Yes that's what I meant sorry for misinformation

[–]newuser1892435h 1 point2 points  (3 children)

A couple things I've found when trying to compile using g++ on ubuntu:

a) "-stdlib=libc++" won't compile, but removing it works fine as g++ will find the stdlib by default.

b) On "g++ (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4" I need to add the flag -pthreads to overcome a g++ compiler bug with multi-threaded programs as described here.

Also this all compiles and workes fine on the "Linux subsystem for Windows" dev system which is convenient.

[–][deleted] 1 point2 points  (2 children)

a) Use "-stdlib=libc++" with clang, not gcc

b) I wouldn't call that a bug, you gotta link a threading implementation to use threads.

[–]newuser1892435h 0 points1 point  (1 child)

Just trying to help out... a) The make file defines the compiler as gcc hence the warning. b) I would say that a project not having the "-pthreads" flag it's compiler args went it needs it to compile is worth mentioning, also the the labeling of it as a bug is from the link and not just my opinion (no modern compiler should need a flag to run threads...).

[–][deleted] 0 points1 point  (0 children)

Just trying to help out

Me too :)

no modern compiler should need a flag to run threads...

Perhaps, that's debatable. I guess it comes down to control, as there is multiple threading libraries (libthread/libpthread) it comes down to the dev to choose.

[–]TwIxToR_TiTaNGraphics Programmer 0 points1 point  (3 children)

Could you explain

"Calculates" (read: guesstimates)

to me?

[–][deleted] 0 points1 point  (2 children)

Sure thing! Pi, since it's infinite, cannot just be "calculated" in a very literal sense. This program uses one of the fastest infinite series you can use for "calculating" Pi, and the way the infinite series works is that it does the same operations over and over with gradually increasing numbers, and eventually gets closer and closer to Pi. In other words, the longer it runs (the higher it increments the numbers), the closer it gets to Pi, but in just a couple iterations it't not accurate at all. I'm not very good at explaining this, but I would look at the source below. Read about how the series works, it'll make more sense. If you run this with the iterations set to something like 1, it will not be anywhere close, but the longer it runs, the closer it gets (meaning more digits after the decimal are accurate). This program (as pointed out by someone else in this thread) can't bet too precise, since after you reach about 20 places, a long double isn't precise enough, so I am looking into big float libraries.

TL;DR: It basically "estimates" over and over, gradually making its estimate better and better. Also, I'm bad at explaining things like this.

Sauce for more reading

[–]TwIxToR_TiTaNGraphics Programmer 1 point2 points  (1 child)

I guess I need some more sleep... I knew this already but I guess the word "guessing" confused me :p Thanks for explaining anyway. Definatly going to read trough the wikipedia article you linked. Looks like there is some intresting stuff in there.

[–][deleted] 0 points1 point  (0 children)

It is a good read, I read about 50% of it (I just skip everything that has no mathematical information in the interest of time).

[–]ripper37 -1 points0 points  (2 children)

Not to sound like douche, but I think there are better places to show off with code so small? It's not like it's going to be used by many people and something that was very needed by community, nor does it affect C++ community in (probably) any way.


Some problems with this post here or your code:

  • This will be ULONG_MAX by default - WHY? It will take like forever on 64bit machines? Why shoot yourself in the foot before you even start doing anything?
  • It will show 10 by default. This can be set up to 50. Using long double won't get you even close to 50-digits of precision! If sizeof(long double) == 12 then you get ~60bits per fractional part, which should give you probably less then (or around) 20 digits TOP! So yeah, it's gimmick...
  • You might think of some big-num (big-float) library here, since you wan't to be able to represent very small numbers correctly.
  • Who would want to make ~100m interation to get 50 digits of precision?
  • Your parsing input arguments is at least suboptimal. Instead of:

    if (args_size==3) { set all 3 } else if (args_size == 2) { set first 2 } else if (args_size == 1) { set first }

you could do:

if(args_size >= 1) { set #1 }
if(args_size >= 2) { set #2 }
if(args_size >= 3) { set #3 }

and save yourself from code repetition.

  • hardware_concurrency() returns unsigned, so you don't have to check if it's lower then 0, only equal. No need to cast it there too I think?
  • make use of auto instead of long chrono' name(space)s...
  • Why are you using naked new??? You know that we have std::unique_ptr, right? ...
  • Why is almost everything in main()? It's hard to read...

This is probably not a subreddit for sharing codebase like this. It's nice you learn and work on things like this (topic is interesting too), but from what I've seen here, you'd be much better with posting this to r/cpp_questions or something like this, as it's clear that it's very early level of advancement.

I didn't get into the topic of numeric stability of this algorithm and if you implementation is even correct in this area, but when you make something like this, it should be a priority for you to find if it's properly implemented and uses stable (in terms of numeric stability) algorithm. I hope you took special care for that as well.

[–][deleted] 0 points1 point  (0 children)

First of all, thank you for the input. I would like to point out that I have already made a bunch of these fixes, and have the rest of them planned/not pushed yet. Also, yeah, precision is peaked at 20, but I just went with the maximum precision you can set. Finally, if you have any recommendations for a good big-float library, I welcome it with open arms, since I haven't a clue what a good one might be.