/***********************************************************************//** * @brief Load effective area from performance table * * @param[in] filename Performance table file name. * * @exception GCTAExceptionHandler::file_open_error * File could not be opened for read access. * * This method loads the effective area information from an ASCII * performance table. ***************************************************************************/ void GCTAAeffPerfTable::load(const std::string& filename) { // Clear arrays m_logE.clear(); m_aeff.clear(); // Allocate line buffer const int n = 1000; char line[n]; // Open performance table readonly FILE* fptr = std::fopen(filename.c_str(), "r"); if (fptr == NULL) { throw GCTAException::file_open_error(G_LOAD, filename); } // Read lines while (std::fgets(line, n, fptr) != NULL) { // Split line in elements. Strip empty elements from vector. std::vector<std::string> elements = split(line, " "); for (int i = elements.size()-1; i >= 0; i--) { if (strip_whitespace(elements[i]).length() == 0) { elements.erase(elements.begin()+i); } } // Skip header if (elements[0].find("log(E)") != std::string::npos) { continue; } // Break loop if end of data table has been reached if (elements[0].find("----------") != std::string::npos) { break; } // Push elements in node array and vector m_logE.append(todouble(elements[0])); m_aeff.push_back(todouble(elements[1])*10000.0); } // endwhile: looped over lines // Close file std::fclose(fptr); // Store filename m_filename = filename; // Return return; }
/* See STRATEGY section below */ static int to_value_number(void* ctx, const char* val, size_t len) { lua_State* L = (lua_State*)ctx; lua_pushnumber(L, todouble(L, val, len)); (lua_tocfunction(L, -2))(L); return 1; }
int fpe_iszero(const fpe_t op) { fpe_t t; double d; int i; unsigned long long tr = 0; unsigned int differentbits=0; for(i=0;i<12;i++) t->v[i] = op->v[i]; coeffred_round_par(t->v); //Constant-time comparison double zero = 0.; unsigned long long *zp = (unsigned long long *)&zero;; unsigned long long *tp; for(i=0;i<12;i++) { d = todouble(t->v[i]); tp = (unsigned long long *)&d; tr |= (*tp ^ *zp); } for(i=0;i<8;i++) differentbits |= i[(unsigned char*)&tr]; return 1 & ((differentbits - 1) >> 8); }
std::vector<double> CEPHandler::todoubleList(const std::vector<std::string>& s) { std::vector<double> result; for (std::vector<std::string>::const_iterator i = s.begin(); i != s.end(); ++i) { result.push_back(todouble(*i)); } return result; }
void loadTest(Values &testData) { ifstream fin; string data = "test.txt"; fin.open(ROOT_PATH + data); char data_char[256]; if (!fin) { cout << "load test data faild"; exit(1); } while (fin.getline(data_char, sizeof(data_char), '\n')) { vector<string> temp; vector<double> tempII; char demlit[] = " :"; temp.push_back(data_char); temp = split(temp[0], demlit); for (int i = 0; i < temp.size(); i++) { if (i % 2 == 0) { tempII.push_back(todouble(temp[i])); } } tempII.erase(tempII.begin()); testData.push_back(tempII); } cout << testData[0].size() << endl; }
static value_t fl_time_string(value_t *args, uint32_t nargs) { argcount("time.string", nargs, 1); double t = todouble(args[0], "time.string"); char buf[64]; timestring(t, buf, sizeof(buf)); return string_from_cstr(buf); }
/***********************************************************************//** * @brief Get double precision value * * @param[in] row Table row. * @param[in] col Table column. * * Returns value of specified row and column as double precision. ***************************************************************************/ double GCsv::real(const int& row, const int& col) const { // Convert element into double double value = todouble((*this)(row, col)); // Return value return value; }
static int js_parser_number(void *ctx, const char* buffer, size_t buffer_len) { lua_State *L=(lua_State*)ctx; lua_getfield(L, lua_upvalueindex(2), "value"); if ( ! lua_isnil(L, -1) ) { lua_pushvalue(L, lua_upvalueindex(2)); lua_pushnumber(L, todouble(L, buffer, buffer_len)); lua_pushliteral(L, "number"); lua_call(L, 3, 0); } else { lua_pop(L, 1); } return 1; }
void loadTrain(Values &workset, Set &i_low, Set &i_up) { ifstream fin; string data = "train.txt"; fin.open(ROOT_PATH + data); char data_char[256]; if (!fin) { cout << "training data error opening via" << endl; cout << ROOT_PATH << endl; exit(1); } int count = 0; while (fin.getline(data_char, sizeof(data_char), '\n')) { vector<string> temp; vector<double> tempII; char demlit[] = " :"; temp.push_back(data_char); temp = split(temp[0], demlit); for (int i = 0; i < temp.size(); i++) { if ((i % 2) == 0) { tempII.push_back(todouble(temp[i])); } } encounter.push_back(tempII[0]);//push into encounter index if (tempII[0] == -1) { tempII[0] = count; //index in trainData i_low.push_back(tempII); } else { tempII[0] = count; //index in trainData i_up.push_back(tempII); } tempII.erase(tempII.begin());//erase 1/-1 value workset.push_back(tempII); count++; } }
void run_tests(void) { volatile long double vld; long double ld; volatile double vd; double d; volatile float vf; float f; int x; test("sign bits", fpequal(-0.0, -0.0) && !fpequal(0.0, -0.0)); vd = NAN; test("NaN equality", fpequal(NAN, NAN) && NAN != NAN && vd != vd); feclearexcept(ALL_STD_EXCEPT); test("NaN comparison returns false", !(vd <= vd)); /* * XXX disabled; gcc/amd64 botches this IEEE 754 requirement by * emitting ucomisd instead of comisd. */ skiptest("FENV_ACCESS: NaN comparison raises invalid exception", fetestexcept(ALL_STD_EXCEPT) == FE_INVALID); vd = 0.0; run_zero_opt_test(vd, vd); vd = INFINITY; run_inf_opt_test(vd); feclearexcept(ALL_STD_EXCEPT); vd = INFINITY; x = (int)vd; /* XXX disabled (works with -O0); gcc doesn't support FENV_ACCESS */ skiptest("FENV_ACCESS: Inf->int conversion raises invalid exception", fetestexcept(ALL_STD_EXCEPT) == FE_INVALID); /* Raising an inexact exception here is an IEEE-854 requirement. */ feclearexcept(ALL_STD_EXCEPT); vd = 0.75; x = (int)vd; test("0.75->int conversion rounds toward 0, raises inexact exception", x == 0 && fetestexcept(ALL_STD_EXCEPT) == FE_INEXACT); feclearexcept(ALL_STD_EXCEPT); vd = -42.0; x = (int)vd; test("-42.0->int conversion is exact, raises no exception", x == -42 && fetestexcept(ALL_STD_EXCEPT) == 0); feclearexcept(ALL_STD_EXCEPT); x = (int)INFINITY; /* XXX disabled; gcc doesn't support FENV_ACCESS */ skiptest("FENV_ACCESS: const Inf->int conversion raises invalid", fetestexcept(ALL_STD_EXCEPT) == FE_INVALID); feclearexcept(ALL_STD_EXCEPT); x = (int)0.5; /* XXX disabled; gcc doesn't support FENV_ACCESS */ skiptest("FENV_ACCESS: const double->int conversion raises inexact", x == 0 && fetestexcept(ALL_STD_EXCEPT) == FE_INEXACT); test("compile-time constants don't have too much precision", one_f == 1.0L && one_d == 1.0L && one_ld == 1.0L); test("const minimum rounding precision", 1.0F + FLT_EPSILON != 1.0F && 1.0 + DBL_EPSILON != 1.0 && 1.0L + LDBL_EPSILON != 1.0L); /* It isn't the compiler's fault if this fails on FreeBSD/i386. */ vf = FLT_EPSILON; vd = DBL_EPSILON; vld = LDBL_EPSILON; test("runtime minimum rounding precision", 1.0F + vf != 1.0F && 1.0 + vd != 1.0 && 1.0L + vld != 1.0L); test("explicit float to float conversion discards extra precision", (float)(1.0F + FLT_EPSILON * 0.5F) == 1.0F && (float)(1.0F + vf * 0.5F) == 1.0F); test("explicit double to float conversion discards extra precision", (float)(1.0 + FLT_EPSILON * 0.5) == 1.0F && (float)(1.0 + vf * 0.5) == 1.0F); test("explicit ldouble to float conversion discards extra precision", (float)(1.0L + FLT_EPSILON * 0.5L) == 1.0F && (float)(1.0L + vf * 0.5L) == 1.0F); test("explicit double to double conversion discards extra precision", (double)(1.0 + DBL_EPSILON * 0.5) == 1.0 && (double)(1.0 + vd * 0.5) == 1.0); test("explicit ldouble to double conversion discards extra precision", (double)(1.0L + DBL_EPSILON * 0.5L) == 1.0 && (double)(1.0L + vd * 0.5L) == 1.0); /* * FLT_EVAL_METHOD > 1 implies that float expressions are always * evaluated in double precision or higher, but some compilers get * this wrong when registers spill to memory. The following expression * forces a spill when there are at most 8 FP registers. */ test("implicit promption to double or higher precision is consistent", #if FLT_EVAL_METHOD == 1 || FLT_EVAL_METHOD == 2 || defined(__i386__) TWICE(TWICE(TWICE(TWICE(TWICE( TWICE(TWICE(TWICE(TWICE(1.0F + vf * 0.5F))))))))) == (1.0 + FLT_EPSILON * 0.5) * 512.0 #else 1 #endif ); f = 1.0 + FLT_EPSILON * 0.5; d = 1.0L + DBL_EPSILON * 0.5L; test("const assignment discards extra precision", f == 1.0F && d == 1.0); f = 1.0 + vf * 0.5; d = 1.0L + vd * 0.5L; test("variable assignment discards explicit extra precision", f == 1.0F && d == 1.0); f = 1.0F + vf * 0.5F; d = 1.0 + vd * 0.5; test("variable assignment discards implicit extra precision", f == 1.0F && d == 1.0); test("return discards extra precision", tofloat(1.0 + vf * 0.5) == 1.0F && todouble(1.0L + vd * 0.5L) == 1.0); fesetround(FE_UPWARD); /* XXX disabled (works with -frounding-math) */ skiptest("FENV_ACCESS: constant arithmetic respects rounding mode", 1.0F + FLT_MIN == 1.0F + FLT_EPSILON && 1.0 + DBL_MIN == 1.0 + DBL_EPSILON && 1.0L + LDBL_MIN == 1.0L + LDBL_EPSILON); fesetround(FE_TONEAREST); ld = vld * 0.5; test("associativity is respected", 1.0L + ld + (LDBL_EPSILON * 0.5) == 1.0L && 1.0L + (LDBL_EPSILON * 0.5) + ld == 1.0L && ld + 1.0 + (LDBL_EPSILON * 0.5) == 1.0L && ld + (LDBL_EPSILON * 0.5) + 1.0 == 1.0L + LDBL_EPSILON); }
/*! Cast to double. \return ticks as double */ operator double() { return todouble(); }
// Print the element to stdout: void fpe_print(FILE * outfile, const fpe_t op) { int i; for(i=0;i<11;i++) fprintf(outfile, "%10lf, ", todouble(op->v[i])); fprintf(outfile, "%10lf", todouble(op->v[11])); }
bool CEPHandler::ReadVehicleFile(const std::vector<std::string>& DataPath, const std::string& emissionClass, Helpers* Helper, double& vehicleMass, double& vehicleLoading, double& vehicleMassRot, double& crossArea, double& cWValue, double& f0, double& f1, double& f2, double& f3, double& f4, double& axleRatio, double& auxPower, double& ratedPower, double& engineIdlingSpeed, double& engineRatedSpeed, double& effectiveWheelDiameter, std::vector<double>& transmissionGearRatios, std::string& vehicleMassType, std::string& vehicleFuelType, double& pNormV0, double& pNormP0, double& pNormV1, double& pNormP1, std::vector<std::vector<double> >& matrixSpeedInertiaTable, std::vector<std::vector<double> >& normedDragTable) { vehicleMass = 0; vehicleLoading = 0; vehicleMassRot = 0; crossArea = 0; cWValue = 0; f0 = 0; f1 = 0; f2 = 0; f3 = 0; f4 = 0; axleRatio = 0; ratedPower = 0; auxPower = 0; engineIdlingSpeed = 0; engineRatedSpeed = 0; effectiveWheelDiameter = 0; vehicleMassType = ""; vehicleFuelType = ""; pNormV0 = 0; pNormP0 = 0; pNormV1 = 0; pNormP1 = 0; transmissionGearRatios = std::vector<double>(); matrixSpeedInertiaTable = std::vector<std::vector<double> >(); normedDragTable = std::vector<std::vector<double> >(); std::string line; std::string cell; int dataCount = 0; //Open file std::ifstream vehicleReader; for (std::vector<std::string>::const_iterator i = DataPath.begin(); i != DataPath.end(); i++) { vehicleReader.open(((*i) + emissionClass + ".PHEMLight.veh").c_str()); if (vehicleReader.good()) { break; } } if (!vehicleReader.good()) { Helper->setErrMsg("File does not exist! (" + emissionClass + ".PHEMLight.veh)"); return false; } // skip header ReadLine(vehicleReader); while ((line = ReadLine(vehicleReader)) != "" && dataCount <= 49) { if (line.substr(0, 1) == Helper->getCommentPrefix()) { continue; } else { dataCount++; } cell = split(line, ',')[0]; // reading Mass if (dataCount == 1) { vehicleMass = todouble(cell); } // reading vehicle loading if (dataCount == 2) { vehicleLoading = todouble(cell); } // reading cWValue if (dataCount == 3) { cWValue = todouble(cell); } // reading crossectional area if (dataCount == 4) { crossArea = todouble(cell); } // reading vehicle mass rotational if (dataCount == 7) { vehicleMassRot = todouble(cell); } // reading rated power if (dataCount == 9) { auxPower = todouble(cell); } // reading rated power if (dataCount == 10) { ratedPower = todouble(cell); } // reading engine rated speed if (dataCount == 11) { engineRatedSpeed = todouble(cell); } // reading engine idling speed if (dataCount == 12) { engineIdlingSpeed = todouble(cell); } // reading f0 if (dataCount == 14) { f0 = todouble(cell); } // reading f1 if (dataCount == 15) { f1 = todouble(cell); } // reading f2 if (dataCount == 16) { f2 = todouble(cell); } // reading f3 if (dataCount == 17) { f3 = todouble(cell); } // reading f4 if (dataCount == 18) { f4 = todouble(cell); } // reading axleRatio if (dataCount == 21) { axleRatio = todouble(cell); } // reading effective wheel diameter if (dataCount == 22) { effectiveWheelDiameter = todouble(cell); } if (dataCount >= 23 && dataCount <= 40) { transmissionGearRatios.push_back(todouble(cell)); } // reading vehicleMassType if (dataCount == 45) { vehicleMassType = cell; } // reading vehicleFuelType if (dataCount == 46) { vehicleFuelType = cell; } // reading pNormV0 if (dataCount == 47) { pNormV0 = todouble(cell); } // reading pNormP0 if (dataCount == 48) { pNormP0 = todouble(cell); } // reading pNormV1 if (dataCount == 49) { pNormV1 = todouble(cell); } // reading pNormP1 if (dataCount == 50) { pNormP1 = todouble(cell); } } while ((line = ReadLine(vehicleReader)) != "" && line.substr(0, 1) != Helper->getCommentPrefix()) { if (line.substr(0, 1) == Helper->getCommentPrefix()) { continue; } matrixSpeedInertiaTable.push_back(todoubleList(split(line, ','))); } while ((line = ReadLine(vehicleReader)) != "") { if (line.substr(0, 1) == Helper->getCommentPrefix()) { continue; } normedDragTable.push_back(todoubleList(split(line, ','))); } return true; }