bool _test_valid_finite( const float& ) { float d0 = 0.0; float d1 = 1.0; float d2; assert( CGAL::is_valid( d0) ); assert( CGAL::is_valid( d1) ); assert( CGAL_NTS is_finite( d0) ); assert( CGAL_NTS is_finite( d1) ); if ( CGAL::is_valid( d1/d0 - d1/d0 )) { d2 = d1/d0 - d1/d0; show( reinterpret_cast<IEEE_754_float*>(&d2)); } if ( CGAL_NTS is_finite( d1/d0 )) { d2 = d1/d0; show( reinterpret_cast<IEEE_754_float*>(&d2)); } assert( CGAL::is_valid( d1/d0 )); assert( !CGAL::is_valid( d1/d0 - d1/d0 )); assert( !CGAL_NTS is_finite( d1/d0 )); return true; }
void SampleRenderer::render_tile(CtxG&, Recti tile_rect, Recti tile_film_rect, Film& tile_film, Sampler& sampler) const { StatTimer t(TIMER_RENDER_TILE); Vec2 film_res(float(this->film->x_res), float(this->film->y_res)); for(int32_t y = tile_rect.p_min.y; y < tile_rect.p_max.y; ++y) { for(int32_t x = tile_rect.p_min.x; x < tile_rect.p_max.x; ++x) { sampler.start_pixel(); for(uint32_t s = 0; s < sampler.samples_per_pixel; ++s) { sampler.start_pixel_sample(); Vec2 pixel_pos = sampler.get_sample_2d(this->pixel_pos_idx); Vec2 film_pos = Vec2(float(x), float(y)) + pixel_pos; Ray ray(this->scene->camera->cast_ray(film_res, film_pos)); Spectrum radiance = this->get_radiance(*this->scene, ray, 0, sampler); assert(is_finite(radiance)); assert(is_nonnegative(radiance)); if(is_finite(radiance) && is_nonnegative(radiance)) { Vec2 tile_film_pos = film_pos - Vec2(float(tile_film_rect.p_min.x), float(tile_film_rect.p_min.y)); tile_film.add_sample(tile_film_pos, radiance); } } } } }
bool _test_valid_finite( const double& ) { double d0 = 0.0; double d1 = 1.0; double d2; // d0 = 0.0; // d1 = 1.0; assert( CGAL::is_valid( d0) ); assert( CGAL::is_valid( d1) ); assert( CGAL_NTS is_finite( d0) ); assert( CGAL_NTS is_finite( d1) ); if ( CGAL::is_valid( d1/d0 - d1/d0 )) { d2 = d1/d0 - d1/d0; show( reinterpret_cast<IEEE_754_double*>(&d2)); } if ( CGAL_NTS is_finite( d1/d0 )) { d2 = d1/d0; show( reinterpret_cast<IEEE_754_double*>(&d2)); } assert( CGAL::is_valid( d1/d0 )); assert( !CGAL::is_valid( d1/d0 - d1/d0 )); assert( !CGAL_NTS is_finite( d1/d0 )); assert( CGAL::is_valid( std::sqrt( d1)) ); assert( !CGAL::is_valid( std::sqrt( -d1)) ); return true; }
int Update_init_manually(em_phyclust_struct *empcs, Q_matrix_array *QA, em_control *EMC, em_fp *EMFP){ int n_X, k, ret_stop = 0; for(n_X = 0; n_X < empcs->N_X; n_X++){ for(k = 0; k < empcs->K; k++){ empcs->Z_normalized[n_X][k] = 0.0; } empcs->Z_normalized[n_X][empcs->class_id[empcs->map_X_to_X_org[n_X]]] = 1.0; } reset_Q_matrix_array(QA); if(EMC->se_type == SE_YES){ reset_SE_P_matrix(empcs->SE_P); } assign_Mu_by_class(empcs->N_X_org, empcs->K, empcs->L, empcs->ncode, empcs->gap_index, empcs->class_id, empcs->X_org, empcs->Mu); ret_stop = init_m_step(empcs, QA, EMC, EMFP); if(ret_stop > 0){ #if PRINT_ERROR > 0 fprintf_stderr("PE: Initialization error.\n"); #endif return(ret_stop); } if(!is_finite(EMFP->LogL_observed(empcs, QA))){ #if PRINT_ERROR > 0 fprintf_stderr("PE: manual initialization leads to non-finite observed log likelihood\n"); #endif return(1); } return(ret_stop); } /* End of Update_init_manually(). */
DEF_TEST(SkFloatToHalf_finite_ftz, r) { #if 0 for (uint64_t bits = 0; bits <= 0xffffffff; bits++) { #else SkRandom rand; for (int i = 0; i < 1000000; i++) { uint32_t bits = rand.nextU(); #endif float f; memcpy(&f, &bits, 4); uint16_t expected = SkFloatToHalf(f); if (!is_finite(expected)) { // _finite_ftz() only works for values that can be represented as a finite half float. continue; } uint16_t alternate = expected; if (is_denorm(expected)) { // _finite_ftz() may flush denorms to zero, and happens to keep the sign bit. alternate = signbit(f) ? 0x8000 : 0x0000; } uint16_t actual = SkFloatToHalf_finite_ftz(Sk4f{f})[0]; // _finite_ftz() may truncate instead of rounding, so it may be one too small. REPORTER_ASSERT(r, actual == expected || actual == expected - 1 || actual == alternate || actual == alternate - 1); } }
int main() { double zero = 0; double inf = 1/zero; double nan = zero*inf; bool b = true; b = b && is_valid(inf); b = b && !is_valid(nan); b = b && !is_finite(inf); b = b && !is_finite(nan); if (!b) return -1; return 0; }
bool List::get_seed (Point<float>& p, Point<float>& d) { if (is_finite()) { for (std::vector<Base*>::iterator i = seeders.begin(); i != seeders.end(); ++i) { if ((*i)->get_seed (p, d)) return true; } p.invalidate(); return false; } else { if (seeders.size() == 1) return seeders.front()->get_seed (p, d); do { float incrementer = 0.0; const float sample = rng.uniform() * total_volume; for (std::vector<Base*>::iterator i = seeders.begin(); i != seeders.end(); ++i) { if ((incrementer += (*i)->vol()) > sample) return (*i)->get_seed (p, d); } } while (1); return false; } }
void bi::renormalise(V1 lws) { thrust::replace_if(lws.begin(), lws.end(), is_not_finite_functor<real>(), bi::log(0.0)); real mx = max_reduce(lws); if (is_finite(mx)) { sub_elements(lws, mx, lws); } }
int main(int argc,char* argv[]) { const char* filename = (argc > 1) ? argv[1] : "data/points.xy"; std::ifstream input(filename); Triangulation t; Filter is_finite(t); Finite_triangulation ft(t, is_finite, is_finite); Point p ; while(input >> p){ t.insert(p); } vertex_iterator vit, ve; // Associate indices to the vertices int index = 0; // boost::tie assigns the first and second element of the std::pair // returned by boost::vertices to the variables vit and ve for(boost::tie(vit,ve)=boost::vertices(ft); vit!=ve; ++vit ){ vertex_descriptor vd = *vit; vertex_id_map[vd]= index++; } // Dijkstra's shortest path needs property maps for the predecessor and distance // We first declare a vector std::vector<vertex_descriptor> predecessor(boost::num_vertices(ft)); // and then turn it into a property map boost::iterator_property_map<std::vector<vertex_descriptor>::iterator, VertexIdPropertyMap> predecessor_pmap(predecessor.begin(), vertex_index_pmap); std::vector<double> distance(boost::num_vertices(ft)); boost::iterator_property_map<std::vector<double>::iterator, VertexIdPropertyMap> distance_pmap(distance.begin(), vertex_index_pmap); // start at an arbitrary vertex vertex_descriptor source = *boost::vertices(ft).first; std::cout << "\nStart dijkstra_shortest_paths at " << source->point() <<"\n"; boost::dijkstra_shortest_paths(ft, source, distance_map(distance_pmap) .predecessor_map(predecessor_pmap) .vertex_index_map(vertex_index_pmap)); for(boost::tie(vit,ve)=boost::vertices(ft); vit!=ve; ++vit ){ vertex_descriptor vd = *vit; std::cout << vd->point() << " [" << vertex_id_map[vd] << "] "; std::cout << " has distance = " << boost::get(distance_pmap,vd) << " and predecessor "; vd = boost::get(predecessor_pmap,vd); std::cout << vd->point() << " [" << vertex_id_map[vd] << "]\n "; } return 0; }
expr * user_sort_factory::get_some_value(sort * s) { if (is_finite(s)) { value_set * set = nullptr; m_sort2value_set.find(s, set); SASSERT(set != 0); SASSERT(!set->m_values.empty()); return *(set->m_values.begin()); } return simple_factory<unsigned>::get_some_value(s); }
inline void MP_Float::construct_from_builtin_fp_type(T d) { if (d == 0) return; // Protection against rounding mode != nearest, and extended precision. Set_ieee_double_precision P; CGAL_assertion(is_finite(d)); // This is subtle, because ints are not symetric against 0. // First, scale d, and adjust exp accordingly. exp = 0; while (d < trunc_min || d > trunc_max) { ++exp; d /= base; } while (d >= trunc_min/base && d <= trunc_max/base) { --exp; d *= base; } // Then, compute the limbs. // Put them in v (in reverse order temporarily). T orig = d, sum = 0; while (true) { int r = my_nearbyint(d); if (d-r >= T(base/2-1)/(base-1)) ++r; v.push_back(r); // We used to do simply "d -= v.back();", but when the most significant // limb is 1 and the second is -32768, then it can happen that // |d - v.back()| > |d|, hence a bit of precision can be lost. // Hence the need for sum/orig. sum += v.back(); d = orig-sum; if (d == 0) break; sum *= base; orig *= base; d *= base; --exp; } // Reverse v. std::reverse(v.begin(), v.end()); CGAL_assertion(v.back() != 0); }
inline arma_warn_unused eT mean(const subview_row<eT>& A) { arma_extra_debug_sigprint(); arma_debug_check( (A.n_elem == 0), "mean(): given object has no elements" ); const eT mu = accu(A) / eT(A.n_cols); return is_finite(mu) ? mu : op_mean::direct_mean_robust(A); }
bool SkRect::setBoundsCheck(const SkPoint pts[], int count) { SkASSERT((pts && count > 0) || count == 0); bool isFinite = true; if (count <= 0) { sk_bzero(this, sizeof(SkRect)); } else { Sk4s min, max, accum; if (count & 1) { min = Sk4s(pts[0].fX, pts[0].fY, pts[0].fX, pts[0].fY); pts += 1; count -= 1; } else { min = Sk4s::Load(&pts[0].fX); pts += 2; count -= 2; } accum = max = min; accum *= Sk4s(0); count >>= 1; for (int i = 0; i < count; ++i) { Sk4s xy = Sk4s::Load(&pts->fX); accum *= xy; min = Sk4s::Min(min, xy); max = Sk4s::Max(max, xy); pts += 2; } /** * With some trickery, we may be able to use Min/Max to also propogate non-finites, * in which case we could eliminate accum entirely, and just check min and max for * "is_finite". */ if (is_finite(accum)) { float minArray[4], maxArray[4]; min.store(minArray); max.store(maxArray); this->set(SkTMin(minArray[0], minArray[2]), SkTMin(minArray[1], minArray[3]), SkTMax(maxArray[0], maxArray[2]), SkTMax(maxArray[1], maxArray[3])); } else { // we hit a non-finite value, so zero everything and return false this->setEmpty(); isFinite = false; } } return isFinite; }
bool user_sort_factory::get_some_values(sort * s, expr_ref & v1, expr_ref & v2) { if (is_finite(s)) { value_set * set = nullptr; if (m_sort2value_set.find(s, set) && set->m_values.size() >= 2) { obj_hashtable<expr>::iterator it = set->m_values.begin(); v1 = *it; ++it; v2 = *it; return true; } return false; } return simple_factory<unsigned>::get_some_values(s, v1, v2); }
DEF_TEST(SkHalfToFloat_finite_ftz, r) { for (uint32_t h = 0; h <= 0xffff; h++) { if (!is_finite(h)) { // _finite_ftz() only works for values that can be represented as a finite half float. continue; } // _finite_ftz() may flush denorms to zero. 0.0f will compare == with both +0.0f and -0.0f. float expected = SkHalfToFloat(h), alternate = is_denorm(h) ? 0.0f : expected; float actual = SkHalfToFloat_finite_ftz(h)[0]; REPORTER_ASSERT(r, actual == expected || actual == alternate); } }
void List::add (Base* const in) { if (seeders.size() && !(in->is_finite() == is_finite())) throw Exception ("Cannot use a combination of seed types where some are number-limited and some are not!"); if (!App::get_options ("max_seed_attempts").size()) { for (std::vector<Base*>::const_iterator i = seeders.begin(); i != seeders.end(); ++i) { if ((*i)->get_max_attempts() != in->get_max_attempts()) throw Exception ("Cannot use a combination of seed types where the default maximum number of sampling attempts per seed is inequal, unless you use the -max_seed_attempts option."); } } seeders.push_back (in); total_volume += in->vol(); total_count += in->num(); }
static HRESULT Number_toPrecision(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { NumberInstance *number; INT prec = 0, size; jsstr_t *str; DOUBLE val; HRESULT hres; if(!(number = number_this(jsthis))) return throw_type_error(ctx, JS_E_NUMBER_EXPECTED, NULL); if(argc) { hres = to_int32(ctx, argv[0], &prec); if(FAILED(hres)) return hres; if(prec<1 || prec>21) return throw_range_error(ctx, JS_E_PRECISION_OUT_OF_RANGE, NULL); } val = number->value; if(!is_finite(val) || !prec) { hres = to_string(ctx, jsval_number(val), &str); if(FAILED(hres)) return hres; }else { if(val != 0) size = floor(log10(val>0 ? val : -val)) + 1; else size = 1; if(size > prec) str = number_to_exponential(val, prec-1); else str = number_to_fixed(val, prec-size); if(!str) return E_OUTOFMEMORY; } if(r) *r = jsval_string(str); else jsstr_release(str); return S_OK; }
int main(int,char*[]) { Triangulation t; Filter is_finite(t); Finite_triangulation ft(t, is_finite, is_finite); Point p ; while(std::cin >> p) { t.insert(p); } vertex_iterator vit, ve; // Associate indices to the vertices int index = 0; // boost::tie assigns the first and second element of the std::pair // returned by boost::vertices to the variables vit and ve for(boost::tie(vit,ve)=boost::vertices(ft); vit!=ve; ++vit ) { vertex_descriptor vd = *vit; vertex_id_map[vd]= index++; } // We use the default edge weight which is the squared length of the edge // This property map is defined in graph_traits_Triangulation_2.h // In the function call you can see a named parameter: vertex_index_map std::list<edge_descriptor> mst; boost::kruskal_minimum_spanning_tree(t, std::back_inserter(mst), vertex_index_map(vertex_index_pmap)); std::cout << "The edges of the Euclidean mimimum spanning tree:" << std::endl; for(std::list<edge_descriptor>::iterator it = mst.begin(); it != mst.end(); ++it) { edge_descriptor ed = *it; vertex_descriptor svd = boost::source(ed,t); vertex_descriptor tvd = boost::target(ed,t); Triangulation::Vertex_handle sv = svd; Triangulation::Vertex_handle tv = tvd; std::cout << "[ " << sv->point() << " | " << tv->point() << " ] " << std::endl; } return 0; }
double log_prior(uvec& gamma, const string &type, double a, double b, int n){ double res; if(type.compare("fixed") == 0){ // NOT IMPLEMENTED YET } else if (type.compare("betabinomial") == 0){ double x = gamma.n_elem + a; double y = n - gamma.n_elem + b; res = log(beta(x,y)) - log(beta(a,b)); // Stirling approximation: if(!is_finite(res)){ res = 0.5*log(2*PI)+(x-0.5)*log(x)+(y-0.5)*log(y)-(x+y-0.5)*log(x+y); } } else if (type.compare("MRF") == 0){ // NOT IMPLEMENTED YET } return res; }
/* ECMA-262 3rd Edition 9.5 */ HRESULT to_int32(script_ctx_t *ctx, jsval_t v, INT *ret) { double n; HRESULT hres; const double p32 = (double)0xffffffff + 1; hres = to_number(ctx, v, &n); if(FAILED(hres)) return hres; if(is_finite(n)) n = n > 0 ? fmod(n, p32) : -fmod(-n, p32); else n = 0; *ret = (UINT32)n; return S_OK; }
static HRESULT Number_toExponential(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { NumberInstance *number; DOUBLE val; INT prec = 0; jsstr_t *str; HRESULT hres; TRACE("\n"); if(!(number = number_this(jsthis))) return throw_type_error(ctx, JS_E_NUMBER_EXPECTED, NULL); if(argc) { hres = to_int32(ctx, argv[0], &prec); if(FAILED(hres)) return hres; if(prec<0 || prec>20) return throw_range_error(ctx, JS_E_FRACTION_DIGITS_OUT_OF_RANGE, NULL); } val = number->value; if(!is_finite(val)) { hres = to_string(ctx, jsval_number(val), &str); if(FAILED(hres)) return hres; }else { if(!prec) prec--; str = number_to_exponential(val, prec); if(!str) return E_OUTOFMEMORY; } if(r) *r = jsval_string(str); else jsstr_release(str); return S_OK; }
double SimpleInterpolation::interpolate(const double& v) { if(!is_finite(v)) return v; int n = x.size(); /* Approximate y(v), given (x,y)[i], i = 0,..,n-1 */ int i, j, ij; i = 0; j = n - 1; /* handle out-of-domain points */ if(v < x.front()) return ylow; if(v > x.back()) return yhigh; /* find the correct interval by bisection */ while(i < j - 1) { /* x[i] <= v <= x[j] */ ij = (i + j)/2; /* i+1 <= ij <= j-1 */ if(v < x[ij]) j = ij; else i = ij; /* still i < j */ } /* provably have i == j-1 */ /* interpolation */ if(v == x[j]) return y[j]; if(v == x[i]) return y[i]; /* impossible: if(x[j] == x[i]) return y[i]; */ if(!constant_interpolation) /* linear */ return y[i] + (y[j] - y[i]) * ((v - x[i])/(x[j] - x[i])); else /* 2 : constant */ return y[i] * f1 + y[j] * f2; }/* approx1() */
/** * D = pathLength(G); * The distance matrix contains lengths of shortest paths between all * pairs of nodes. An entry (u,v) represents the length of shortest path * from node u to node v. The average shortest path length is the * characteristic path length of the network. * Input: G, weighted directed/undirected connection matrix * Output: D, distance matrix * The input matrix must be a mapping from weight to distance. For * instance, in a weighted correlation network, higher correlations are * more naturally interpreted as shorter distances, and the input matrix * should consequently be some inverse of the connectivity matrix. * Lengths between disconnected nodes are set to Inf. * Lengths on the main diagonal are set to 0. * Algorithm: Dijkstra's algorithm. */ mat Connectome::pathLength(const mat &G) { uint n = G.n_rows,v=0; mat D = mat(n,n).fill(datum::inf), G1, t; D.diag().fill(0); uvec S, V, W, tt; for (uint u=0;u<n;++u) { S = linspace<uvec>(0,n-1,n); G1 = G; V = uvec(1).fill(u); while (true) { // instead of replacing indices by 0 like S(V)=0; // we declare all indices then remove the V indeces // from S. Notice that it is assured that tt should // be one element since indices don't repeat for (int i=0;i<V.n_elem;++i) { tt = find(S == V(i),1); if (!tt.is_empty()) S.shed_row(tt(0)); } G1.cols(V).fill(0); for (uint j = 0;j<V.n_elem;++j) { v = V(j); W = find(G1.row(v)>0); D(uvec(1).fill(u),W) = arma::min(D(uvec(1).fill(u),W), D(u,v)+G1(uvec(1).fill(v),W)); } t = D(uvec(1).fill(u),S); if (t.is_empty() || !is_finite(t.min())) break; V = find( D.row(u) == t.min()); } } return D; }
int main() { double zero = 0.0; double posnormal = 1.3; double negnormal = -1.0; double nan = zero/zero; double posinf = posnormal/zero; double neginf = negnormal/zero; if (!CGAL:: is_valid(zero)) return 1; if (!CGAL_NTS is_finite(zero)) return 1; if (!CGAL:: is_valid(posnormal)) return 1; if (!CGAL_NTS is_finite(posnormal)) return 1; if (!CGAL:: is_valid(negnormal)) return 1; if (!CGAL_NTS is_finite(negnormal)) return 1; if (CGAL:: is_valid(nan)) return 1; if (CGAL_NTS is_finite(nan)) return 1; if (!CGAL:: is_valid(posinf)) return 1; if (CGAL_NTS is_finite(posinf)) return 1; if (!CGAL:: is_valid(neginf)) return 1; if (CGAL_NTS is_finite(neginf)) return 1; test_is_integer(); test_split_num_den(); return 0; }
inline void glue_hist::apply(Mat<uword>& out, const mtGlue<uword,T1,T2,glue_hist>& in) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; const uword dim = in.aux_uword; const unwrap_check_mixed<T1> tmp1(in.A, out); const unwrap_check_mixed<T2> tmp2(in.B, out); const Mat<eT>& X = tmp1.M; const Mat<eT>& C = tmp2.M; arma_debug_check ( (C.is_vec() == false), "hist(): parameter 'centers' must be a vector" ); arma_debug_check ( (dim > 1), "hist(): parameter 'dim' must be 0 or 1" ); const uword X_n_rows = X.n_rows; const uword X_n_cols = X.n_cols; const uword X_n_elem = X.n_elem; const uword C_n_elem = C.n_elem; if( C_n_elem == 0 ) { out.reset(); return; } // for vectors we are currently ignoring the "dim" parameter uword out_n_rows = 0; uword out_n_cols = 0; if(X.is_vec()) { if(X.is_rowvec()) { out_n_rows = 1; out_n_cols = C_n_elem; } else if(X.is_colvec()) { out_n_rows = C_n_elem; out_n_cols = 1; } } else { if(dim == 0) { out_n_rows = C_n_elem; out_n_cols = X_n_cols; } else if(dim == 1) { out_n_rows = X_n_rows; out_n_cols = C_n_elem; } } out.zeros(out_n_rows, out_n_cols); const eT* C_mem = C.memptr(); const eT center_0 = C_mem[0]; if(X.is_vec()) { const eT* X_mem = X.memptr(); uword* out_mem = out.memptr(); for(uword i=0; i < X_n_elem; ++i) { const eT val = X_mem[i]; if(is_finite(val)) { eT opt_dist = (val >= center_0) ? (val - center_0) : (center_0 - val); uword opt_index = 0; for(uword j=1; j < C_n_elem; ++j) { const eT center = C_mem[j]; const eT dist = (val >= center) ? (val - center) : (center - val); if(dist < opt_dist) { opt_dist = dist; opt_index = j; } else { break; } } out_mem[opt_index]++; } else { // -inf if(val < eT(0)) { out_mem[0]++; } // +inf if(val > eT(0)) { out_mem[C_n_elem-1]++; } // ignore NaN } } } else { if(dim == 0) { for(uword col=0; col < X_n_cols; ++col) { const eT* X_coldata = X.colptr(col); uword* out_coldata = out.colptr(col); for(uword row=0; row < X_n_rows; ++row) { const eT val = X_coldata[row]; if(arma_isfinite(val)) { eT opt_dist = (center_0 >= val) ? (center_0 - val) : (val - center_0); uword opt_index = 0; for(uword j=1; j < C_n_elem; ++j) { const eT center = C_mem[j]; const eT dist = (center >= val) ? (center - val) : (val - center); if(dist < opt_dist) { opt_dist = dist; opt_index = j; } else { break; } } out_coldata[opt_index]++; } else { // -inf if(val < eT(0)) { out_coldata[0]++; } // +inf if(val > eT(0)) { out_coldata[C_n_elem-1]++; } // ignore NaN } } } } else if(dim == 1) { for(uword row=0; row < X_n_rows; ++row) { for(uword col=0; col < X_n_cols; ++col) { const eT val = X.at(row,col); if(arma_isfinite(val)) { eT opt_dist = (center_0 >= val) ? (center_0 - val) : (val - center_0); uword opt_index = 0; for(uword j=1; j < C_n_elem; ++j) { const eT center = C_mem[j]; const eT dist = (center >= val) ? (center - val) : (val - center); if(dist < opt_dist) { opt_dist = dist; opt_index = j; } else { break; } } out.at(row,opt_index)++; } else { // -inf if(val < eT(0)) { out.at(row,0)++; } // +inf if(val > eT(0)) { out.at(row,C_n_elem-1)++; } // ignore NaN } } } } } }
static void conv_host_fp_to_float() { if ( is_finite(1.0) ) {} } /* conv_host_fp_to_float */
Terrain::Terrain(const SystemBody *body) : m_body(body), m_seed(body->seed), m_rand(body->seed), m_heightScaling(0), m_minh(0) { // load the heightmap if (m_body->heightMapFilename) { RefCountedPtr<FileSystem::FileData> fdata = FileSystem::gameDataFiles.ReadFile(m_body->heightMapFilename); if (!fdata) { fprintf(stderr, "Error: could not open file '%s'\n", m_body->heightMapFilename); abort(); } ByteRange databuf = fdata->AsByteRange(); Sint16 minHMap = INT16_MAX, maxHMap = INT16_MIN; Uint16 minHMapScld = UINT16_MAX, maxHMapScld = 0; // XXX unify heightmap types switch (m_body->heightMapFractal) { case 0: { Uint16 v; bufread_or_die(&v, 2, 1, databuf); m_heightMapSizeX = v; bufread_or_die(&v, 2, 1, databuf); m_heightMapSizeY = v; const Uint32 heightmapPixelArea = (m_heightMapSizeX * m_heightMapSizeY); std::unique_ptr<Sint16[]> heightMap(new Sint16[heightmapPixelArea]); bufread_or_die(heightMap.get(), sizeof(Sint16), heightmapPixelArea, databuf); m_heightMap.reset(new double[heightmapPixelArea]); double *pHeightMap = m_heightMap.get(); for(Uint32 i=0; i<heightmapPixelArea; i++) { const Sint16 val = heightMap.get()[i]; minHMap = std::min(minHMap, val); maxHMap = std::max(maxHMap, val); // store then increment pointer (*pHeightMap) = val; ++pHeightMap; } assert(is_equal_general(*pHeightMap, m_heightMap[heightmapPixelArea])); //printf("minHMap = (%hd), maxHMap = (%hd)\n", minHMap, maxHMap); break; } case 1: { Uint16 v; // XXX x and y reversed from above *sigh* bufread_or_die(&v, 2, 1, databuf); m_heightMapSizeY = v; bufread_or_die(&v, 2, 1, databuf); m_heightMapSizeX = v; const Uint32 heightmapPixelArea = (m_heightMapSizeX * m_heightMapSizeY); // read height scaling and min height which are doubles double te; bufread_or_die(&te, 8, 1, databuf); m_heightScaling = te; bufread_or_die(&te, 8, 1, databuf); m_minh = te; std::unique_ptr<Uint16[]> heightMapScaled(new Uint16[heightmapPixelArea]); bufread_or_die(heightMapScaled.get(), sizeof(Uint16), heightmapPixelArea, databuf); m_heightMap.reset(new double[heightmapPixelArea]); double *pHeightMap = m_heightMap.get(); for(Uint32 i=0; i<heightmapPixelArea; i++) { const Uint16 val = heightMapScaled[i]; minHMapScld = std::min(minHMapScld, val); maxHMapScld = std::max(maxHMapScld, val); // store then increment pointer (*pHeightMap) = val; ++pHeightMap; } assert(is_equal_general(*pHeightMap, m_heightMap[heightmapPixelArea])); //printf("minHMapScld = (%hu), maxHMapScld = (%hu)\n", minHMapScld, maxHMapScld); break; } default: assert(0); } } switch (Pi::detail.textures) { case 0: textures = false; m_fracnum = 2;break; default: case 1: textures = true; m_fracnum = 0;break; } switch (Pi::detail.fracmult) { case 0: m_fracmult = 100;break; case 1: m_fracmult = 10;break; case 2: m_fracmult = 1;break; case 3: m_fracmult = 0.5;break; default: case 4: m_fracmult = 0.1;break; } m_sealevel = Clamp(m_body->m_volatileLiquid.ToDouble(), 0.0, 1.0); m_icyness = Clamp(m_body->m_volatileIces.ToDouble(), 0.0, 1.0); m_volcanic = Clamp(m_body->m_volcanicity.ToDouble(), 0.0, 1.0); // height scales with volcanicity as well m_surfaceEffects = 0; const double rad = m_body->GetRadius(); // calculate max height if ((m_body->heightMapFilename) && m_body->heightMapFractal > 1){ // if scaled heightmap m_maxHeightInMeters = 1.1*pow(2.0, 16.0)*m_heightScaling; // no min height required as it's added to radius in lua }else { m_maxHeightInMeters = std::max(100.0, (9000.0*rad*rad*(m_volcanic+0.5)) / (m_body->GetMass() * 6.64e-12)); if (!is_finite(m_maxHeightInMeters)) m_maxHeightInMeters = rad * 0.5; // ^^^^ max mountain height for earth-like planet (same mass, radius) // and then in sphere normalized j**z } m_maxHeight = std::min(1.0, m_maxHeightInMeters / rad); //printf("%s: max terrain height: %fm [%f]\n", m_body->name.c_str(), m_maxHeightInMeters, m_maxHeight); m_invMaxHeight = 1.0 / m_maxHeight; m_planetRadius = rad; m_planetEarthRadii = rad / EARTH_RADIUS; // Pick some colors, mainly reds and greens for (int i=0; i<int(COUNTOF(m_entropy)); i++) m_entropy[i] = m_rand.Double(); for (int i=0; i<int(COUNTOF(m_rockColor)); i++) { double r,g,b; r = m_rand.Double(0.3, 1.0); g = m_rand.Double(0.3, r); b = m_rand.Double(0.3, g); r = std::max(b, r * m_body->m_metallicity.ToFloat()); g = std::max(b, g * m_body->m_metallicity.ToFloat()); m_rockColor[i] = vector3d(r, g, b); } // Pick some darker colours mainly reds and greens for (int i=0; i<int(COUNTOF(m_entropy)); i++) m_entropy[i] = m_rand.Double(); for (int i=0; i<int(COUNTOF(m_darkrockColor)); i++) { double r,g,b; r = m_rand.Double(0.05, 0.3); g = m_rand.Double(0.05, r); b = m_rand.Double(0.05, g); r = std::max(b, r * m_body->m_metallicity.ToFloat()); g = std::max(b, g * m_body->m_metallicity.ToFloat()); m_darkrockColor[i] = vector3d(r, g, b); } // grey colours, in case you simply must have a grey colour on a world with high metallicity for (int i=0; i<int(COUNTOF(m_entropy)); i++) m_entropy[i] = m_rand.Double(); for (int i=0; i<int(COUNTOF(m_greyrockColor)); i++) { double g; g = m_rand.Double(0.3, 0.9); m_greyrockColor[i] = vector3d(g, g, g); } // Pick some plant colours, mainly greens // TODO take star class into account for (int i=0; i<int(COUNTOF(m_entropy)); i++) m_entropy[i] = m_rand.Double(); for (int i=0; i<int(COUNTOF(m_plantColor)); i++) { double r,g,b; g = m_rand.Double(0.3, 1.0); r = m_rand.Double(0.3, g); b = m_rand.Double(0.2, r); g = std::max(r, g * m_body->m_life.ToFloat()); b *= (1.0-m_body->m_life.ToFloat()); m_plantColor[i] = vector3d(r, g, b); } // Pick some darker plant colours mainly greens // TODO take star class into account for (int i=0; i<int(COUNTOF(m_entropy)); i++) m_entropy[i] = m_rand.Double(); for (int i=0; i<int(COUNTOF(m_darkplantColor)); i++) { double r,g,b; g = m_rand.Double(0.05, 0.3); r = m_rand.Double(0.00, g); b = m_rand.Double(0.00, r); g = std::max(r, g * m_body->m_life.ToFloat()); b *= (1.0-m_body->m_life.ToFloat()); m_darkplantColor[i] = vector3d(r, g, b); } // Pick some sand colours, mainly yellow // TODO let some planetary value scale this colour for (int i=0; i<int(COUNTOF(m_entropy)); i++) m_entropy[i] = m_rand.Double(); for (int i=0; i<int(COUNTOF(m_sandColor)); i++) { double r,g,b; r = m_rand.Double(0.6, 1.0); g = m_rand.Double(0.6, r); //b = m_rand.Double(0.0, g/2.0); b = 0; m_sandColor[i] = vector3d(r, g, b); } // Pick some darker sand colours mainly yellow // TODO let some planetary value scale this colour for (int i=0; i<int(COUNTOF(m_entropy)); i++) m_entropy[i] = m_rand.Double(); for (int i=0; i<int(COUNTOF(m_darksandColor)); i++) { double r,g,b; r = m_rand.Double(0.05, 0.6); g = m_rand.Double(0.00, r); //b = m_rand.Double(0.00, g/2.0); b = 0; m_darksandColor[i] = vector3d(r, g, b); } // Pick some dirt colours, mainly red/brown // TODO let some planetary value scale this colour for (int i=0; i<int(COUNTOF(m_entropy)); i++) m_entropy[i] = m_rand.Double(); for (int i=0; i<int(COUNTOF(m_dirtColor)); i++) { double r,g,b; r = m_rand.Double(0.3, 0.7); g = m_rand.Double(r-0.1, 0.75); b = m_rand.Double(0.0, r/2.0); m_dirtColor[i] = vector3d(r, g, b); } // Pick some darker dirt colours mainly red/brown // TODO let some planetary value scale this colour for (int i=0; i<int(COUNTOF(m_entropy)); i++) m_entropy[i] = m_rand.Double(); for (int i=0; i<int(COUNTOF(m_darkdirtColor)); i++) { double r,g,b; r = m_rand.Double(0.05, 0.3); g = m_rand.Double(r-0.05, 0.35); b = m_rand.Double(0.0, r/2.0); m_darkdirtColor[i] = vector3d(r, g, b); } // These are used for gas giant colours, they are more m_random and *should* really use volatileGasses - TODO for (int i=0; i<int(COUNTOF(m_entropy)); i++) m_entropy[i] = m_rand.Double(); for (int i=0; i<int(COUNTOF(m_gglightColor)); i++) { double r,g,b; r = m_rand.Double(0.0, 0.5); g = m_rand.Double(0.0, 0.5); b = m_rand.Double(0.0, 0.5); m_gglightColor[i] = vector3d(r, g, b); } //darker gas giant colours, more reds and greens for (int i=0; i<int(COUNTOF(m_entropy)); i++) m_entropy[i] = m_rand.Double(); for (int i=0; i<int(COUNTOF(m_ggdarkColor)); i++) { double r,g,b; r = m_rand.Double(0.0, 0.3); g = m_rand.Double(0.0, r); b = m_rand.Double(0.0, std::min(r, g)); m_ggdarkColor[i] = vector3d(r, g, b); } }
/* ECMA-262 5.1 Edition 15.12.3 (abstract operation Str) */ static HRESULT stringify(stringify_ctx_t *ctx, jsval_t val) { jsval_t value; HRESULT hres; if(is_object_instance(val) && get_object(val)) { jsdisp_t *obj; DISPID id; obj = iface_to_jsdisp((IUnknown*)get_object(val)); if(!obj) return S_FALSE; hres = jsdisp_get_id(obj, toJSONW, 0, &id); jsdisp_release(obj); if(hres == S_OK) FIXME("Use toJSON.\n"); } /* FIXME: Support replacer replacer. */ hres = maybe_to_primitive(ctx->ctx, val, &value); if(FAILED(hres)) return hres; switch(jsval_type(value)) { case JSV_NULL: if(!append_string(ctx, nullW)) hres = E_OUTOFMEMORY; break; case JSV_BOOL: if(!append_string(ctx, get_bool(value) ? trueW : falseW)) hres = E_OUTOFMEMORY; break; case JSV_STRING: { jsstr_t *str = get_string(value); const WCHAR *ptr = jsstr_flatten(str); if(ptr) hres = json_quote(ctx, ptr, jsstr_length(str)); else hres = E_OUTOFMEMORY; break; } case JSV_NUMBER: { double n = get_number(value); if(is_finite(n)) { const WCHAR *ptr; jsstr_t *str; /* FIXME: Optimize. There is no need for jsstr_t here. */ hres = double_to_string(n, &str); if(FAILED(hres)) break; ptr = jsstr_flatten(str); assert(ptr != NULL); hres = ptr && !append_string_len(ctx, ptr, jsstr_length(str)) ? E_OUTOFMEMORY : S_OK; jsstr_release(str); }else { if(!append_string(ctx, nullW)) hres = E_OUTOFMEMORY; } break; } case JSV_OBJECT: { jsdisp_t *obj; obj = iface_to_jsdisp((IUnknown*)get_object(value)); if(!obj) { hres = S_FALSE; break; } if(!is_callable(obj)) hres = is_class(obj, JSCLASS_ARRAY) ? stringify_array(ctx, obj) : stringify_object(ctx, obj); else hres = S_FALSE; jsdisp_release(obj); break; } case JSV_UNDEFINED: hres = S_FALSE; break; case JSV_VARIANT: FIXME("VARIANT\n"); hres = E_NOTIMPL; break; } jsval_release(value); return hres; }
expr * user_sort_factory::get_fresh_value(sort * s) { if (is_finite(s)) return nullptr; return simple_factory<unsigned>::get_fresh_value(s); }
void user_sort_factory::register_value(expr * n) { SASSERT(!is_finite(m_manager.get_sort(n))); simple_factory<unsigned>::register_value(n); }