int main(int argc, char const *argv[]) { int N = 1000; double approximated_pi = calc_pi(N); printf("pi=%f\n", approximated_pi); return 0; }
int main(int argc, char** argv) { size_t size = MAX_STR; unsigned long int n = 0; char *str; float calculated_pi; str = (char *)malloc(size); if(!str) { fprintf(stderr, "malloc() failed. exiting\n"); exit(EXIT_FAILURE); } printf("Enter number of points to check> "); getline(&str, &size, stdin); sscanf(str, "%lu", &n); if(n == 0) { fprintf(stderr, "Please Enter a number greater than zero.\n"); exit(EXIT_FAILURE); } initrand(); calculated_pi = calc_pi(n); printf("PI calculated using the Monte Carlo method with %lu points: %f\n", n, calculated_pi); return 0; }
static void test(int step) { if (test_sync_trivial) { test_sync_trivial_run(step); } if (test_write_) { test_write(512); } if (test_leak_malloc) { leak_malloc(SLICE, step); } if (test_leak_calloc) { leak_calloc(SLICE, step); } if (test_leak_realloc) { leak_realloc(SLICE, step); } if (test_sync) { test_sync_step(step); } if (test_dl_) { test_dl(step); } if (test_pi) { int num_steps = 1000000; printf("Calculating PI (%d steps)...\n", num_steps); calc_pi(num_steps); } }
int main (int argc, char *argv[]) { GOptionContext *ctx; gboolean res; GError *error; bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); setlocale (LC_ALL, ""); /* Option parsing */ ctx = g_option_context_new (_("- New GDM login")); g_option_context_set_translation_domain (ctx, GETTEXT_PACKAGE); g_option_context_add_main_entries (ctx, options, NULL); g_option_context_parse (ctx, &argc, &argv, NULL); g_option_context_free (ctx); if (show_version) { g_print ("%s %s\n", argv [0], VERSION); exit (1); } /* don't support commands other than VERSION */ if (send_command != NULL) { if (strcmp (send_command, "VERSION") == 0) { g_print ("GDM %s \n", VERSION); return 0; } else { g_warning ("No longer supported"); } return 1; } if (monte_carlo_pi) { calc_pi (); return 0; } if (use_xnest) { g_warning ("Not yet implemented"); return 1; } error = NULL; res = gdm_goto_login_session (&error); if (! res) { g_printerr ("%s", error->message); } else { maybe_lock_screen (); } return 1; }
int main(int argc, char *argv[]) { int my_rank; /* rank of process */ int num_procs; /* number of processes */ int source; /* rank of sender */ int dest = 0; /* rank of receiver */ int tag = 0; /* tag for messages */ char message[100]; /* storage for message */ MPI_Status status ; /* return status for receive */ /* start up MPI */ MPI_Init(&argc, &argv); /* find out process rank */ MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); /* find out number of processes */ MPI_Comm_size(MPI_COMM_WORLD, &num_procs); if (my_rank != 0) { /* create message */ sprintf(message, "Greetings from process %d!", my_rank); /* use strlen+1 so that '\0' get transmitted */ MPI_Send(message, strlen(message)+1, MPI_CHAR, dest, tag, MPI_COMM_WORLD); } else { printf("Num processes: %d\n",num_procs); for (source = 1; source < num_procs; source++) { MPI_Recv(message, 100, MPI_CHAR, source, tag, MPI_COMM_WORLD, &status); printf("Process 0 received \"%s\"\n",message); } /* now return the compliment */ sprintf(message, "Hi, how are you?"); } MPI_Bcast(message, strlen(message)+1, MPI_CHAR, dest, MPI_COMM_WORLD); if (my_rank != 0) { printf("Process %d received \"%s\"\n", my_rank, message); } /* calculate PI */ calc_pi(my_rank, num_procs); /* shut down MPI */ MPI_Finalize(); return 0; }
const T& get_constant_pi() { static T result; static bool b = false; if(!b) { calc_pi(result, geofeatures_boost::multiprecision::detail::digits2<number<T, et_on> >::value); b = true; } constant_initializer<T, &get_constant_pi<T> >::do_nothing(); return result; }
double *mlpg(PStream * pst) { int doupdate(PStream *, int); void calc_pi(PStream *, int); void calc_k(PStream *, int); void update_P(PStream *, int); void update_c(PStream *, int); int tcur, tmin, tmax; int d, m, u; pst->sm.t++; tcur = pst->sm.t & pst->sm.mask; tmin = (pst->sm.t - pst->range) & pst->sm.mask; tmax = (pst->sm.t + pst->dw.maxw[WRIGHT]) & pst->sm.mask; for (u = -pst->range * 2; u <= pst->range * 2; u++) { for (m = 0; m <= pst->order; m++) pst->sm.P[u][tmax][m] = 0.0; } for (m = 0; m < pst->vSize; m++) { pst->sm.mseq[tmax][m] = pst->mean[m]; pst->sm.ivseq[tmax][m] = pst->ivar[m]; } for (m = 0; m <= pst->order; m++) { if (pst->iType != 2) pst->sm.c[tmax][m] = pst->mean[m]; else pst->sm.c[tmax][m] = pst->mean[m] * finv(pst->ivar[m]); pst->sm.P[0][tmax][m] = finv(pst->ivar[m]); } for (d = 1; d < pst->dw.num; d++) { if (doupdate(pst, d)) { calc_pi(pst, d); calc_k(pst, d); update_P(pst, d); update_c(pst, d); } } pst->par = pst->sm.c[tmin]; return (pst->par); }
const T& get_constant_pi() { static BOOST_MP_THREAD_LOCAL T result; static BOOST_MP_THREAD_LOCAL long digits = 0; #ifndef BOOST_MP_USING_THREAD_LOCAL static BOOST_MP_THREAD_LOCAL bool b = false; constant_initializer<T, &get_constant_pi<T> >::do_nothing(); if (!b || (digits != boost::multiprecision::detail::digits2<number<T> >::value())) { b = true; #else if ((digits != boost::multiprecision::detail::digits2<number<T> >::value())) { #endif calc_pi(result, boost::multiprecision::detail::digits2<number<T, et_on> >::value()); digits = boost::multiprecision::detail::digits2<number<T> >::value(); } return result; }
int main (int argc, char *argv[]) { GtkWidget *dialog; char *command; char *version; char *ret; const char *message; GOptionContext *ctx; bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); /* Option parsing */ ctx = g_option_context_new ("- New mdm login"); g_option_context_add_main_entries (ctx, options, _("main options")); g_option_context_parse (ctx, &argc, &argv, NULL); g_option_context_free (ctx); if (monte_carlo_pi) { calc_pi (); return 0; } mdm_log_init (); mdm_log_set_debug (debug_in); if (args_remaining != NULL && args_remaining[0] != NULL) server = args_remaining[0]; if (send_command != NULL) { if ( ! mdmcomm_check (FALSE)) { mdm_common_error (_("Error: MDM (MDM Display Manager) is not running.")); mdm_common_error (_("You might be using a different display manager.")); return 1; } } else { /* * The --command argument does not display anything, so avoid * running gtk_init until it finishes. Sometimes the * --command argument is used when there is no display so it * will fail and cause the program to exit, complaining about * "no display". */ gtk_init (&argc, &argv); if ( ! mdmcomm_check (TRUE)) { return 1; } } /* Start reading config data in bulk */ mdmcomm_comm_bulk_start (); /* Process --command option */ g_type_init (); if (send_command != NULL) { /* gdk_init is needed for cookie code to get display */ gdk_init (&argc, &argv); if (authenticate) auth_cookie = mdmcomm_get_auth_cookie (); /* * If asking for a translatable config value, then try to get * the translated value first. If this fails, then go ahead * and call the normal sockets command. */ if (strncmp (send_command, MDM_SUP_GET_CONFIG " ", strlen (MDM_SUP_GET_CONFIG " ")) == 0) { gchar *value = NULL; const char *key = &send_command[strlen (MDM_SUP_GET_CONFIG " ")]; if (is_key (MDM_KEY_WELCOME, key) || is_key (MDM_KEY_REMOTE_WELCOME, key)) { value = mdm_config_get_translated_string ((gchar *)key); if (value != NULL) { ret = g_strdup_printf ("OK %s", value); } } /* * If the above didn't return a value, then must be a * different key, so call mdmcomm_call_mdm. */ if (value == NULL) ret = mdmcomm_call_mdm (send_command, auth_cookie, "1.0.0.0", 5); } else { ret = mdmcomm_call_mdm (send_command, auth_cookie, "1.0.0.0", 5); } /* At this point we are done using the socket, so close it */ mdmcomm_comm_bulk_stop (); if (ret != NULL) { g_print ("%s\n", ret); return 0; } else { dialog = hig_dialog_new (NULL /* parent */, GTK_DIALOG_MODAL /* flags */, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Cannot communicate with MDM " "(The MDM Display Manager)"), _("Perhaps you have an old version " "of MDM running.")); gtk_widget_show_all (dialog); gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_destroy (dialog); return 1; } } /* * Now process what mdmflexiserver is more frequently used to * do, start VT (Virtual Terminal) sesions - at least on * systems where it is supported. On systems where it is not * supporteed VT stands for "Very Tight" and will mess up your * display if you use it. Tight! So do not use it. * * I would accept a patch to disable it on such systems, but it * is easy to avoid not using it as long as your distro does not * put the menu choice in the application launch button on the * panel (don't ship the desktop file). */ /* * Always attempt to get cookie and authenticate. On remote * servers */ auth_cookie = mdmcomm_get_auth_cookie (); if (use_xnest) { char *cookie = mdmcomm_get_a_cookie (FALSE /* binary */); if (cookie == NULL) { /* At this point we are done using the socket, so close it */ mdmcomm_comm_bulk_stop (); dialog = hig_dialog_new (NULL /* parent */, GTK_DIALOG_MODAL /* flags */, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("You do not seem to have the " "authentication needed for this " "operation"), _("Perhaps your .Xauthority " "file is not set up correctly.")); gtk_widget_show_all (dialog); gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_destroy (dialog); return 1; } command = g_strdup_printf (MDM_SUP_FLEXI_XNEST " %s %d %s %s", mdmcomm_get_display (), (int)getuid (), cookie, XauFileName ()); g_free (cookie); version = "1.0.0.0"; auth_cookie = NULL; } else { /* check for other displays/logged in users */ check_for_users (); if (auth_cookie == NULL) { /* At this point we are done using the socket, so close it */ mdmcomm_comm_bulk_stop (); dialog = hig_dialog_new (NULL /* parent */, GTK_DIALOG_MODAL /* flags */, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("You do not seem to be logged in on the " "console"), _("Starting a new login only " "works correctly on the console.")); gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE); gtk_widget_show_all (dialog); gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_destroy (dialog); return 1; } read_servers (); server = choose_server (); if (server == NULL) command = g_strdup (MDM_SUP_FLEXI_XSERVER); else command = g_strdup_printf (MDM_SUP_FLEXI_XSERVER " %s", server); version = "1.0.0.0"; } ret = mdmcomm_call_mdm (command, auth_cookie, version, 5); g_free (command); g_free (auth_cookie); g_strfreev (args_remaining); /* At this point we are done using the socket, so close it */ mdmcomm_comm_bulk_stop (); if (ret != NULL && strncmp (ret, "OK ", 3) == 0) { /* if we switched to a different screen as a result of this, * lock the current screen */ if ( ! no_lock && ! use_xnest) { maybe_lock_screen (); } /* all fine and dandy */ g_free (ret); return 0; } message = mdmcomm_get_error_message (ret, use_xnest); dialog = hig_dialog_new (NULL /* parent */, GTK_DIALOG_MODAL /* flags */, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, _("Cannot start new display"), message); gtk_widget_show_all (dialog); gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_destroy (dialog); g_free (ret); return 1; }
int main(int argc,char **argv){ unsigned int calc_mod = 0; double idata = 0; double kdata = 0; double result = 0; if(argc <2){ printf("enter cmd numbers must >= 2!!\n"); return -1; } if(2 == argc){ argv++; calc_mod = atoi(*argv); switch(calc_mod){ case 5: printf("Calc PI is %f\n",calc_pi()); break; case 14: result = calc_rand(); printf("Calc rand result is %f\n", result); break; default: printf("Input abnormally\n"); break; } } if(3 == argc){ argv++; calc_mod = atoi(*argv); argv++; idata = atof(*argv); switch(calc_mod){ case 6: result = calc_sin((double) idata); printf("Calc sin (%f) result is %f\n",idata,result); break; case 7: result= calc_cos((double) idata); printf("Calc cos (%f) result is %f\n",idata,result); break; case 8: if(PI/2 == idata ||-PI/2 == idata){ printf("Input abnormally\n"); break; } result = calc_tan((double) idata); printf("Calc tan (%f) result is %f\n",idata,result); break; case 9: result = calc_fabs((double) idata); printf("Calc tan (%f) result is %f\n",idata,result); break; case 10: result = calc_exp((double) idata); printf("Calc exp (%f) result is %f\n",idata,result); break; case 11: if(idata <= 0){ printf("Input abnormally\n"); break; } result = calc_ln((double) idata); printf("Calc ln (%f) result is %f\n",idata,result); break; case 12: if(idata <= 0){ printf("Input abnormally\n"); break; } result = calc_log10((double) idata); printf("Calc log10 (%f) result is %f\n",idata,result); break; default: printf("Input abnormally\n"); break; } } if(4 == argc){ argv++; calc_mod = atoi(*argv); argv++; idata = atof(*argv); argv++; kdata = atof(*argv); switch(calc_mod){ case 1: result = calc_add(idata, kdata); printf("Calc add (%f + %f) result is %f\n", idata, kdata, result); break; case 2: result = calc_sub(idata, kdata); printf("Calc sub (%f - %f) result is %f\n", idata, kdata, result); break; case 3: result = calc_mul(idata,kdata); printf("Calc mul (%f * %f) result is %f\n", idata, kdata, result); break; case 4: if(0 == kdata){ printf("Input abnormaly\n"); break; } result = calc_div(idata,kdata); printf("Calc div (%f / %f) result is %f\n", idata, kdata, result); case 13: result = calc_pow(idata,kdata); printf("Calc pow (%f / %f) result is %f\n", idata, kdata, result); break; default: printf("Input abnormally\n"); break; } } return 0; }