rgb lighting(scene s, ray r, hit_test h) { rgb result; if (h.miss) return s.bg; vec hit_position = ray_position(r, h.dist); if (shadow(hit_position, s.light, s.spheres)) { result = rgb_modulate(h.surf, s.amb); } else { double dot = vec_dot(h.surf_norm, s.light.direction); double d = double_max(0, dot); rgb diffuse_light = rgb_scale(d, s.light.color); rgb lsum = rgb_add(s.amb, diffuse_light); result = rgb_modulate(h.surf, lsum); } /**** === implement specular reflection here === ****/ if (rgb_nonzero(h.shine)) { rgb ss; vec N = h.surf_norm; vec L = s.light.direction; rgb S = h.shine; vec R = vec_sub( vec_scale(2* vec_dot(N,L),N),L); vec V = vec_neg(r.direction); if (vec_dot(N,L)>0){ ss = rgb_scale( pow( double_max( vec_dot(R,V),0), 6), S); //rgb_print(k); } else ss = rgb_expr(0,0,0); return rgb_add(result,ss); } return result; }
int main() { float float_max; double db_max; float_max = float_max_size(); db_max = double_max(); printf ( "maximal size of float eq:%46f;\n",float_max ); // db_max = double_max(); db_max = double_max(); printf ( "maximal size of double eq:%46g;\n",db_max ); }
void test_fx_fixed_limits_double(ostream& out) { out << "****************** limits fx_fixed<8, 5>_double\n"; sc_fixed<8, 5> zero_min("-0"); SHOW(zero_min); sc_fixed<8, 5> zero_plus("+0"); SHOW(zero_plus); sc_fixed<8, 5> zero(0); SHOW(zero); sc_fixed<8, 5> long_max(LONG_MAX); SHOW(long_max); sc_fixed<8, 5> long_min(LONG_MIN); SHOW(long_min); sc_fixed<8, 5> int_max(INT_MAX); SHOW(int_max); sc_fixed<8, 5> int_min(INT_MIN); SHOW(int_min); sc_fixed<8, 5> uint_max(UINT_MAX); SHOW(uint_max); sc_fixed<8, 5> ulong_max(ULONG_MAX); SHOW(ulong_max); sc_fixed<8, 5> double_min(DBL_MIN); SHOW(double_min); sc_fixed<8, 5> double_max(DBL_MAX); SHOW(double_max); sc_fixed<8, 5> float_min(FLT_MIN); SHOW(float_min); sc_fixed<8, 5> float_max(FLT_MAX); SHOW(float_max); // sc_fixed<8, 5> res; // SHOW_EXPS(double_min); // SHOW_EXPS(double_max); // SHOW_EXPS(float_min); // SHOW_EXPS(float_max); }
void test_fx_fix_limits_long(ostream& out) { out << "****************** limits fx_fix_long\n"; sc_fix zero_min("-0"); SHOW(zero_min); sc_fix zero_plus("+0"); SHOW(zero_plus); sc_fix zero(0); SHOW(zero); sc_fix long_max(LONG_MAX); SHOW(long_max); sc_fix long_min(LONG_MIN); SHOW(long_min); sc_fix int_max(INT_MAX); SHOW(int_max); sc_fix int_min(INT_MIN); SHOW(int_min); sc_fix uint_max(UINT_MAX); SHOW(uint_max); sc_fix ulong_max(ULONG_MAX); SHOW(ulong_max); sc_fix double_min(DBL_MIN); SHOW(double_min); sc_fix double_max(DBL_MAX); SHOW(double_max); sc_fix float_min(FLT_MIN); SHOW(float_min); sc_fix float_max(FLT_MAX); SHOW(float_max); // sc_fix res; // SHOW_EXPS(long_max); // SHOW_EXPS(long_min); // SHOW_EXPS(int_max); // SHOW_EXPS(int_min); // SHOW_EXPS(uint_max); // SHOW_EXPS(ulong_max); }
void test_fx_float_limits_zero(ostream& out) { cerr << "****************** limits fx_float_zero\n"; sc_fxval zero_min("-0"); SHOW(zero_min); sc_fxval zero_plus("+0"); SHOW(zero_plus); sc_fxval zero(0); SHOW(zero); sc_fxval nan("NaN"); SHOW(nan); sc_fxval inf_plus("+Inf"); SHOW(inf_plus); sc_fxval inf_min("-Inf"); SHOW(inf_min); sc_fxval inf("Inf"); SHOW(inf); sc_fxval long_max(LONG_MAX); SHOW(long_max); sc_fxval long_min(LONG_MIN); SHOW(long_min); sc_fxval int_max(INT_MAX); SHOW(int_max); sc_fxval int_min(INT_MIN); SHOW(int_min); sc_fxval uint_max(UINT_MAX); SHOW(uint_max); sc_fxval ulong_max(ULONG_MAX); SHOW(ulong_max); sc_fxval double_min(DBL_MIN); SHOW(double_min); sc_fxval double_max(DBL_MAX); SHOW(double_max); sc_fxval float_min(FLT_MIN); SHOW(float_min); sc_fxval float_max(FLT_MAX); SHOW(float_max); sc_fxval res; SHOW_EXPS(zero_min); SHOW_EXPS(zero_plus); SHOW_EXPS(zero); }
static signed char window_spike( const double *sub_array, size_t window_len, size_t window_index, double N, double ACC) { size_t j=0; double focus; double max=0; double min=0; double mean=0; double R=0; char initialized=0; for(j=0;j<window_len;j++) { if(j==window_index) { /* * If j is the focus of this window then set it and continue */ focus = sub_array[j]; continue; } if(!initialized) { /* * If we haven't initialized max, min do so now */ max = min = sub_array[j]; initialized = 1; } if(sub_array[j] > max) /* * Keep track of the max value in the sub_array */ max = sub_array[j]; if(sub_array[j] < min) /* * Keep track of the min as well */ min = sub_array[j]; /* * Sum the elements to make a mean later */ mean+= sub_array[j]; } mean = mean/(window_len-1); R = max - min; R = double_max(R, ACC); if( double_abs(focus - mean) > (N*R)) { /* * If the deviation of the focus exceeds N*R then it is a spike value */ return 0; } return 1; }
int main() { printf("Floating point information:\n"); printf("float radix: %d\n", FLT_RADIX); printf("float exponent size: %d (%d bits)\n", FLT_MAX_EXP, bits(FLT_MAX_EXP)); printf("double exponent size: %d (%d bits)\n", DBL_MAX_EXP, bits(DBL_MAX_EXP)); printf("long double exponent size: %d (%d bits)\n", LDBL_MAX_EXP, bits(LDBL_MAX_EXP)); printf("Ranges:\n"); printf("float: [%f, %f]\n", float_min(), float_max()); printf("float: [%f, %f] (system)\n", FLT_MIN, FLT_MAX); printf("double: [%f, %f]\n", double_min(), double_max()); printf("double: [%f, %f] (system)\n", DBL_MIN, DBL_MAX); printf("long double: [%Lf, %Lf]\n", dlong_min(), dlong_max()); printf("long double: [%Lf, %Lf] (system)\n", LDBL_MIN, LDBL_MAX); return 0; }
int main() { printf("\nFixed Point\n"); printf("____________\n\n"); printf("\nBy Calculation.\n\n"); fixed_range(); printf("\nFrom limits.h\n\n"); header_limits_range(); printf("\nFloating Point"); printf("\n________________\n\n"); printf("\nBy Calculation.\n\n"); float_max(); float_min(); double_max(); double_min(); long_double_max(); printf("\nFrom float.h\n\n"); header_float_range(); return 0; }
int main (int argc, char * argv[]) { MSKtask_t task = NULL; MSKenv_t env = NULL; MSKrescodee r = MSK_RES_OK; if (argc <= 1) { printf ("Missing argument. The syntax is:\n"); printf (" simple inputfile [ solutionfile ]\n"); } else { /* Create the mosek environment. The `NULL' arguments here, are used to specify customized memory allocators and a memory debug file. These can safely be ignored for now. */ r = MSK_makeenv(&env, NULL, NULL, NULL, NULL); /* Initialize the environment */ if ( r==MSK_RES_OK ) MSK_initenv (env); /* Create a task object linked to the environment env. Initially we create it with 0 variables and 0 columns, since we do not know the size of the problem. */ if ( r==MSK_RES_OK ) r = MSK_maketask (env, 0,0, &task); if (r == MSK_RES_OK) MSK_linkfunctotaskstream(task,MSK_STREAM_LOG,NULL,printstr); /* We assume that a problem file was given as the first command line argument (received in `argv'). */ if ( r==MSK_RES_OK ) r = MSK_readdata (task, argv[1]); /* Solve the problem */ if ( r==MSK_RES_OK ) { MSKrescodee trmcode; MSK_optimizetrm(task,&trmcode); } /* Print a summary of the solution. */ MSK_solutionsummary(task, MSK_STREAM_MSG); if (r == MSK_RES_OK) { MSKprostae prosta; MSKsolstae solsta; MSKrealt primalobj,maxpbi,maxpcni,maxpeqi,maxinti, dualobj, maxdbi, maxdcni, maxdeqi; MSKintt isdef; MSKsoltypee whichsol = MSK_SOL_BAS; int accepted = 1; MSK_getsolutioninf ( task, whichsol, &prosta, &solsta, &primalobj, &maxpbi, &maxpcni, &maxpeqi, &maxinti, &dualobj, &maxdbi, &maxdcni, &maxdeqi); switch(solsta) { case MSK_SOL_STA_OPTIMAL: case MSK_SOL_STA_NEAR_OPTIMAL: { double max_primal_infeas = 0.0; /* maximal primal infeasibility */ double max_dual_infeas = 0.0; /* maximal dual infeasibility */ double obj_gap = fabs(dualobj-primalobj); max_primal_infeas = double_max(max_primal_infeas,maxpbi); max_primal_infeas = double_max(max_primal_infeas,maxpcni); max_primal_infeas = double_max(max_primal_infeas,maxpeqi); max_dual_infeas = double_max(max_dual_infeas,maxdbi); max_dual_infeas = double_max(max_dual_infeas,maxdcni); max_dual_infeas = double_max(max_dual_infeas,maxdeqi); /* Assume the application needs the solution to be within 1e-6 ofoptimality in an absolute sense. Another approach would be looking at the relative objective gap */ printf("Objective gap: %e\n",obj_gap); if (obj_gap > 1e-6) { printf("Warning: The objective gap is too large."); accepted = 0; } printf("Max primal infeasibility: %e\n", max_primal_infeas); printf("Max dual infeasibility: %e\n" , max_dual_infeas); /* We will accept a primal infeasibility of 1e-8 and dual infeasibility of 1e-6 */ if (max_primal_infeas > 1e-8) { printf("Warning: Primal infeasibility is too large"); accepted = 0; } if (max_dual_infeas > 1e-6) { printf("Warning: Dual infeasibility is too large"); accepted = 0; } } if (accepted && r == MSK_RES_OK) { MSKintt numvar,j; MSKrealt *xx = NULL; MSK_getnumvar(task,&numvar); xx = (double *) malloc(numvar*sizeof(MSKrealt)); MSK_getsolutionslice(task, MSK_SOL_BAS, /* Request the basic solution. */ MSK_SOL_ITEM_XX,/* Which part of solution. */ 0, /* Index of first variable. */ numvar, /* Index of last variable+1. */ xx); printf("Optimal primal solution\n"); for(j=0; j<numvar; ++j) printf("x[%d]: %e\n",j,xx[j]); free(xx); } else { /* Print detailed information about the solution */ if (r == MSK_RES_OK) r = MSK_analyzesolution(task,MSK_STREAM_LOG,whichsol); } break; case MSK_SOL_STA_DUAL_INFEAS_CER: case MSK_SOL_STA_PRIM_INFEAS_CER: case MSK_SOL_STA_NEAR_DUAL_INFEAS_CER: case MSK_SOL_STA_NEAR_PRIM_INFEAS_CER: printf("Primal or dual infeasibility certificate found.\n"); break; case MSK_SOL_STA_UNKNOWN: printf("The status of the solution could not be determined.\n"); break; default: printf("Other solution status"); break; } } else { printf("Error while optimizing.\n"); } MSK_deletetask(&task); MSK_deleteenv(&env); } return r; }