Skip to content

Commit

Permalink
remove pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
tlming16 committed Mar 7, 2022
1 parent f64c8f1 commit 9f9daec
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 253 deletions.
4 changes: 2 additions & 2 deletions cpp/coroutine/c1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct example {
value =v;
return std::suspend_never{};
}
std::suspend_always final_suspend(){
std::suspend_always final_suspend() noexcept{
cout<<"@ finale_suspend is called\n";
return std::suspend_always{};
}
Expand Down Expand Up @@ -110,4 +110,4 @@ int main(){
cout<<a.get()<<endl;
}

}
}
4 changes: 2 additions & 2 deletions cpp/coroutine/c3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct example {
value=v;
return std::suspend_always{};
}
auto final_suspend()->std::suspend_always{
auto final_suspend() noexcept ->std::suspend_always{
return std::suspend_always{};
}
void unhandled_exception(){
Expand Down Expand Up @@ -67,7 +67,7 @@ struct awaitble_obj {

example<int> await_routine(){
awaitble_obj a = awaitble_obj();
for (int i=1;i<5;i++){
for (int i=1;i<100;i++){
auto v = co_await a;
co_yield v;
}
Expand Down
4 changes: 2 additions & 2 deletions cpp/coroutine/c5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ struct task
{
task get_return_object() { return {}; }
std::suspend_never initial_suspend() { return {}; }
std::suspend_never final_suspend() { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void return_void() {}
void unhandled_exception() {}
};
Expand Down Expand Up @@ -186,4 +186,4 @@ task consumer()
int main(){
auto c = consumer();
producer();
}
}
162 changes: 83 additions & 79 deletions cpp/pi.cpp
Original file line number Diff line number Diff line change
@@ -1,132 +1,136 @@
// this is a plain translation from pi.d
// this is a plain translation from pi.d
// all rights reserved
# include <string>
# include <vector>
# include <iostream>
# include <chrono>
// g++ pi.cpp -O3 -std=c++11
#include <chrono>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int LONG_TIME =4000;
const int LONG_TIME = 4000;
vector<char> p;
vector<char> t;
int q;

int tiszero(){
int tiszero() {
int k;
for (k=0;k<=q;k++){
if (t[k]!=0) return false;
for (k = 0; k <= q; k++) {
if (t[k] != 0)
return false;
}
return true;
}

void mul4(){
int i,c,d;
d=c=0;
for (i=q;i>=0;i--){
d = (p[i]*4+c)%10;
c= (p[i]*4+c)/10;
p[i]= static_cast<char>(d);
void mul4() {
int i, c, d;
d = c = 0;
for (i = q; i >= 0; i--) {
d = (p[i] * 4 + c) % 10;
c = (p[i] * 4 + c) / 10;
p[i] = static_cast<char>(d);
}
}

void div4(){
int i,c,d=0;
for( int i=0;i<=q;i++){
c = (10*d +p[i])/4;
d= (10*d +p[i])%4;
p[i]= static_cast<char>(c);
void div4() {
int i, c, d = 0;
for (int i = 0; i <= q; i++) {
c = (10 * d + p[i]) / 4;
d = (10 * d + p[i]) % 4;
p[i] = static_cast<char>(c);
}
}
void div( int divisor){
int i,b;
int quotient,remainder=0;
for ( auto & x:t){
b= (10* remainder +x);
quotient = b/divisor;
remainder= b%divisor;
x= static_cast<char>(quotient);
void div(int divisor) {
int i, b;
int quotient, remainder = 0;
for (auto &x : t) {
b = (10 * remainder + x);
quotient = b / divisor;
remainder = b % divisor;
x = static_cast<char>(quotient);
}
}

void mul(int multiplier){
int b,i,carry=0,digit=0;
for ( i=q;i>=0;i--){
b= (t[i]*multiplier+carry);
digit=b%10;
carry= b/10;
t[i]= static_cast<char>(digit);
void mul(int multiplier) {
int b, i, carry = 0, digit = 0;
for (i = q; i >= 0; i--) {
b = (t[i] * multiplier + carry);
digit = b % 10;
carry = b / 10;
t[i] = static_cast<char>(digit);
}
}
void sub(){
void sub() {
int j;
for ( j=q;j>=0;j--){
if (p[j]<t[j]){
p[j] -=t[j]-10;
p[j-1]-=1;
}else {
p[j]-=t[j];
for (j = q; j >= 0; j--) {
if (p[j] < t[j]) {
p[j] -= t[j] - 10;
p[j - 1] -= 1;
} else {
p[j] -= t[j];
}
}
}

void add( ){
void add() {
int j;
for (j=q;j>=0;j--){
if ( t[j]+p[j]>9){
p[j]+=t[j]-10;
p[j-1]+=1;
}else {
p[j]+=t[j];
for (j = q; j >= 0; j--) {
if (t[j] + p[j] > 9) {
p[j] += t[j] - 10;
p[j - 1] += 1;
} else {
p[j] += t[j];
}
}
}

void arctan(int s){
void arctan(int s) {
int n;
t[0]=1;
t[0] = 1;
div(s);
add();
n=1;
n = 1;
do {
mul(n);
div(s*s);
div(n+=2);
if ( ((n-1)/2)%2==0){
div(s * s);
div(n += 2);
if (((n - 1) / 2) % 2 == 0) {
add();
}else {
} else {
sub();
}
}while (!tiszero());
} while (!tiszero());
}


int main(int argc,char * argv[]){
if (argc==2){
q= atoi(argv[1]);
}else {
cout<< "usage: pi [precision]\n";
int main(int argc, char *argv[]) {
if (argc == 2) {
q = atoi(argv[1]);
} else {
cout << "usage: pi [precision]\n";
exit(55);
}
if (q<0){
cout<<" precision too low\n";
q=3;
}else if (q>LONG_TIME){
cout<<" be patient to wait a while \n";
if (q < 0) {
cout << " precision too low\n";
q = 3;
} else if (q > LONG_TIME) {
cout << " be patient to wait a while \n";
}
q++;
p.resize(q+1);
t.resize(q+1);
p.resize(q + 1);
t.resize(q + 1);
auto start_time = std::chrono::system_clock::now();
arctan(2);
arctan(3);
mul4();
auto end_time = std::chrono::system_clock::now();
auto end_time = std::chrono::system_clock::now();
q--;
cout<<"pi = "<< static_cast<int>(p[0])<<".";
for( int i=1;i<=q;i++){
cout<<static_cast<int>(p[i]);
cout << "pi = " << static_cast<int>(p[0]) << ".";
for (int i = 1; i <= q; i++) {
cout << static_cast<int>(p[i]);
}
cout<<"\n time is "<< std::chrono::duration_cast<std::chrono::seconds>(end_time-start_time).count()<< "s\n";
cout << "\n time is "
<< std::chrono::duration_cast<std::chrono::seconds>(end_time -
start_time)
.count()
<< "s\n";
return 0;

}
}
Loading

0 comments on commit 9f9daec

Please sign in to comment.