I understand that nearbyint allows me to round integers without throwing exceptions. It is possible to use feclearexcept to check for errors or see if rounding took place (which will always be the case for nearbyint).
Can anyone show me an example of when an exception would have been thrown which has been avoided by using nearbyint?
Here is an example of the normal use of the function:
#include <cfenv>
#include <cmath>
#include <iostream>
void test(const char* title, int round_mode)
{
std::feclearexcept(FE_ALL_EXCEPT);
std::fesetround(round_mode);
std::cout << title << std::endl;
std::cout << "nearbyint(2.5) = " << std::nearbyint(2.5) << std::endl;
std::cout << "nearbyint(-2.5) = " << std::nearbyint(-2.5) << std::endl;
std::cout << "FE_INEXACT = " << std::boolalpha << (std::fetestexcept(FE_INEXACT) != 0) << std::endl << std::endl; // This will always be true.
}
#define test(mode) test(#mode, mode)
int main()
{
#ifdef FE_DOWNWARD
test(FE_DOWNWARD);
#endif
#ifdef FE_TONEAREST
test(FE_TONEAREST);
#endif
#ifdef FE_TOWARDZERO
test(FE_TOWARDZERO);
#endif
#ifdef FE_UPWARD
test(FE_UPWARD);
#endif
}
Related
I have code using mutex for self learning.
Link is : https://baptiste-wicht.com/posts/2012/04/c11-concurrency-tutorial-advanced-locking-and-condition-variables.html
I wrote the example: main_deadlock.cpp
#include <iostream>
#include <thread>
#include <mutex>
struct Complex {
std::mutex mutex;
int i;
Complex() : i(0) {}
void mul(int x){
std::cout << "mul : before lock_guard" << std::endl;
std::lock_guard<std::mutex> lock(mutex);
std::cout << "mul : after lock_guard, before operation" << std::endl;
i *= x;
std::cout << "mul : after operation" << std::endl;
}
void div(int x){
std::cout << "div : before lock_guard" << std::endl;
std::lock_guard<std::mutex> lock(mutex);
std::cout << "div : after lock_guard, before operation" << std::endl;
i /= x;
std::cout << "div : after operation" << std::endl;
}
void both(int x, int y)
{
std::cout << "both : before lock_guard" << std::endl;
std::lock_guard<std::mutex> lock(mutex);
std::cout << "both : after lock_guard, before mul()" << std::endl;
mul(x);
std::cout << "both : after mul(), before div()" << std::endl;
div(y);
std::cout << "both : after div()" << std::endl;
}
};
int main(){
std::cout << "main : starting" << std::endl;
Complex complex;
std::cout << "main : calling both()" << std::endl;
complex.both(32, 23);
return 0;
}
I would expect this code will have a deadlock when calling mul() from both() cause both() already acquire the mutex, so mul() should be blocked.
Now I am using: ubuntu 17.10.1 with g++ (Ubuntu 7.2.0-8ubuntu3.2) 7.2.0 (g++ --version output)
If I am using the compile command:
user#user: g++ -o out_deadlock main_deadlock.cpp
I get no deadlock at all!
But if I use the compile command:
user#user: g++ -std=c++11 -pthread -o out_deadlock main_deadlock.cpp
All works - means I see deadlock.
Can you explain?
also, How the first command makes the code compile? I didn't "mention" pthreads and didn't mention -std=c++11 although code is using c++ 11 lib? I would expect fail of compilation/linkage?
Thanks.
The answer is that if you do not compile and link with -pthread then you are not using actual pthread locking functions.
The GNU Linux C library is set up that way so that libraries can call all of the locking functions, but unless they are actually linked into a multithreaded program, none of the locks actually happen.
#include <iostream>
#include <cstring>
int main()
{
const char *str = "Hello world";
char *str1 = nullptr;
std::cout << str << std::endl; // when i output to screan i see hello world
std::cout << str1 << std::endl; // when i output to scream i sen nothing
str1 = new char[strlen(str) + 1];
strcpy(str1, str);
std::cout << str << std::endl; // when i output to scream again i sen nothing
std::cout << str1 << std::endl; // The same
}
Currently I am programming Gin Rummy in C++11 and when dealing out cards my deck removes cards initially so the player gets a real like hand but when moving to dealing cards to the next player it gives that player the same Cards.
Here is a link to a github repo which contains my files. I will attach the output. If you wish to compile the code please you the ./compile script, it contains the necessary files and excludes the table.cpp. This is the output
Github Repo: Gin Rummy
Currently I am not including table.cpp to table.h to be compilied.
I am confused on why it is not removing the Card objects from the Deck vector.
Deck.cpp
#include <iostream>
#include <vector>
#include "deck.h"
#include "card.h"
using namespace std;
//Addes 52 cards to the deck
Deck::Deck() {
cardsInDeck = {};
}
//Remove top card from deck
Card Deck::drawCard(){
Card topCard = getCards()[cardsInDeck.size()-1];
popCard();
getCards().shrink_to_fit();
vector<Card> c2 = getCards();
int size = getCards().size();
cout << size << endl;
return topCard;
}
void Deck::popCard(){
this->cardsInDeck.pop_back();
}
//Add card to a deck
void Deck::addCard(Card addedCard) {
this->cardsInDeck.push_back(addedCard);
}
vector<Card> Deck::getCards(){
return cardsInDeck;
}
Main.cpp
#include <iostream>
#include <ctime>
#include "player.h"
#include "deck.h"
#include "card.h"
#include "computer.h"
using namespace std;
int main() {
srand(time(NULL));
Deck testDeck;
testDeck.newDeck();
testDeck.shuffleDeck();
cout << testDeck.deckSize() <<endl;
//testDeck.displayCards();
//cout << endl << endl;
Player player1;
player1.firstHand(testDeck);
player1.showHand();
cout << endl << testDeck.deckSize() << endl;
cout << endl << endl;
Player player2;
player2.firstHand(testDeck);
player2.showHand();
cout << endl << testDeck.deckSize() << endl;
return 0;
};
I am new to OpenMP... Please help me with this dumb question. Thank you :)
Basically, I want to use OpenMP to speed up two for loops. But I do not know why it keeps saying: invalid controlling predicate for the for loop.
By the way, my GCC version is gcc (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005, and OS I am using is Ubuntu 16.10.
Basically, I generate a toy data that has a typical Key-Value style, like this:
Data = {
"0": ["100","99","98","97",..."1"];
"1": ["100","99","98","97",..."1"];
...
"999":["100","99","98","97",..."1"];
}
Then, for each key, I want to compare its value with the rest of the keys. Here, I sum them up through "user1_list.size()+user2_list.size();". As for each key, the sum-up process is totally independent of other keys, which means this works for parallelism.
Here is my toy example code.
#include <map>
#include <vector>
#include <string>
#include <iostream>
#include "omp.h"
using namespace std;
int main(){
// Create Data
map<string, vector<string>> data;
for(int i=0; i != 1000; i++){
vector<string> list;
for (int j=100; j!=0; j--){
list.push_back(to_string(j));
}
data[to_string(i)]=list;
}
cout << "Data Total size: " << data.size() << endl;
int count = 1;
#pragma omp parallel for private(count)
for (auto it=data.begin(); it!=data.end(); it++){
//cout << "Evoke Thread: " << omp_get_thread_num();
cout << " count: " << count << " / " << data.size() << endl;
count ++;
string user1 = it->first;
vector<string> user1_list = it->second;
for (auto it2=data.begin(); it2!=data.end(); it2++){
string user2 = it2->first;
vector<string> user2_list = it2->second;
cout << "u1:" << user1 << " u2:" << user2;
int total_size = user1_list.size()+user2_list.size();
cout << " total size: " << total_size << endl;
}
}
return 0;
}
I have this minimal code:
#include <mutex>
#include <iostream>
std::mutex themutex;
void f1()
{
std::cout << "1" << std::endl;
std::lock_guard<std::mutex> local_mutex(themutex);
std::cout << "2" << std::endl;
}
void f2()
{
std::cout << "3" << std::endl;
std::lock_guard<std::mutex> local_mutex(themutex);
std::cout << "4" << std::endl;
f1();
std::cout << "5" << std::endl;
}
int main(void)
{
f2();
return 0;
}
I compile and run with
g++ -std=c++11 test_mutex.cc -o test_mutex && ./test_mutex
and I get this output:
3
4
1
2
5
Why?
I expect the program to lock after printing "1" and never to return.
From 30.4.1 ("Mutex requirements"):
The expression m.lock() shall be well-formed and have the following semantics:
Requires: If m is of type std::mutex or std::timed_mutex, the calling thread does not own the mutex.
You're violating the requirements, and so you cannot expect any behaviour guaranteed by the standard.