tree pow (tree t1, tree t2) { if (is_double (t1) && is_double (t2)) return as_tree (pow (as_double (t1), as_double (t2))); if (t1 == "0" || t1 == "1") return t1; if (t2 == "0") return "1"; if (t2 == "1") return t1; return tree (POW, t1, t2); }
tree mul (tree t1, tree t2) { if (is_double (t1) && is_double (t2)) return as_tree (as_double (t1) * as_double (t2)); if (t1 == "0" || t2 == "0") return "0"; if (t1 == "1") return t2; if (t2 == "1") return t1; return tree (TIMES, t1, t2); }
int Vec2_Look(var self, var input, int pos) { Vec2Data* v = cast(self, Vec2); var x = $(Real, 0); var y = $(Real, 0); pos = scan_from(input, pos, "(%f, %f)", x, y); v->x = as_double(x); v->y = as_double(y); return pos; }
/*! Place self inside parents-client area. * Proportions are pos.{x|y} / base.{x|y}. * If base.{x|y}==0 then position in {X|Y} direction will be preserved. */ void Widget::Place ( const Pos &pos, const Pos &base) { Rect client_area = GetParent()->GetClientRect( this); Pos p = GetPosition(); double x = p.x; double y = p.y; if( base.x > 0 ) { x = as_double( client_area.GetW() * pos.x ) / as_double( base.x ); } if( base.y > 0 ) { y = as_double( client_area.GetH() * pos.y ) / as_double( base.y ); } SetPosition( client_area.GetPos() + Pos( std::floor(x), std::floor(y) )); }
time_value& time_value::operator -= (double interval) { if (as_double() >= interval) { *this = time_value(as_double() - interval); } else { *this = time_value(); } return *this; }
static void test_want_double(Constraint *constraint, const char *function, intptr_t actual, const char *test_file, int test_line, TestReporter *reporter) { (*reporter->assert_true)( reporter, test_file, test_line, (*constraint->compare)(constraint, actual), "Wanted [%d], but got [%d] in function [%s] parameter [%s]", as_double(constraint->expected), as_double(actual), function, constraint->parameter); unbox_double(actual); }
static void test_do_not_want_double(Constraint *constraint, const char *function, intptr_t actual, const char *test_file, int test_line, TestReporter *reporter) { (*reporter->assert_true)( reporter, test_file, test_line, (*constraint->compare)(constraint, actual), "Did not want [%f], but got [%f] in function [%s] parameter [%s]", as_double(constraint->expected_value), as_double(actual), function, constraint->parameter_name); (void)unbox_double(actual); }
tree evaluate_lesseq (tree t) { if (N(t)!=2) return evaluate_error ("bad less or equal"); tree t1= evaluate (t[0]); tree t2= evaluate (t[1]); if (is_compound (t1) || is_compound (t2)) return evaluate_error ("bad less or equal"); string s1= t1->label; string s2= t2->label; if (is_double (s1) && (is_double (s2))) return as_string_bool (as_double (s1) <= as_double (s2)); if (is_length (s1) && is_length (s2)) return as_string_bool (as_length (s1) <= as_length (s2)); return evaluate_error ("bad less or equal"); }
void TranscriptionToolBar::OnEndOn(wxCommandEvent & WXUNUSED(event)) { //If IO is busy, abort immediately if (gAudioIO->IsBusy()){ SetButton(false,mButtons[TTB_EndOn]); return; } mVk->AdjustThreshold(GetSensitivity()); AudacityProject *p = GetActiveProject(); TrackList *tl = p->GetTracks(); TrackListOfKindIterator iter(Track::Wave, tl); Track *t = iter.First(); //Make a track if(t) { auto wt = static_cast<const WaveTrack*>(t); sampleCount start, len; GetSamples(wt, &start, &len); //Adjust length to end if selection is null if(len == 0) { len = start; start = 0; } auto newEnd = mVk->OnBackward(*wt, start + len, len); double newpos = newEnd.as_double() / wt->GetRate(); p->SetSel1(newpos); p->RedrawProject(); SetButton(false, mButtons[TTB_EndOn]); } }
void TranscriptionToolBar::OnStartOff(wxCommandEvent & WXUNUSED(event)) { //If IO is busy, abort immediately if (gAudioIO->IsBusy()){ SetButton(false,mButtons[TTB_StartOff]); return; } mVk->AdjustThreshold(GetSensitivity()); AudacityProject *p = GetActiveProject(); SetButton(false, mButtons[TTB_StartOff]); auto t = *p->GetTracks()->Any< const WaveTrack >().begin(); if(t) { auto wt = static_cast<const WaveTrack*>(t); sampleCount start, len; GetSamples(wt, &start, &len); //Adjust length to end if selection is null //if(len == 0) //len = wt->GetSequence()->GetNumSamples()-start; auto newstart = mVk->OffForward(*wt, start, len); double newpos = newstart.as_double() / wt->GetRate(); auto &selectedRegion = p->GetViewInfo().selectedRegion; selectedRegion.setT0( newpos ); p->RedrawProject(); SetButton(false, mButtons[TTB_StartOn]); } }
void assert_that_double_(const char *file, int line, const char *expression, double actual, Constraint* constraint) { BoxedDouble* boxed_actual; if (NULL != constraint && is_not_comparing(constraint)) { (*get_test_reporter()->assert_true)( get_test_reporter(), file, line, false, "\tGot constraint of type [%s], but they are not allowed for assertions, only in mock expectations.", constraint->name); constraint->destroy(constraint); return; } boxed_actual = (BoxedDouble*)box_double(actual); (*get_test_reporter()->assert_true)(get_test_reporter(), file, line, (*constraint->compare)(constraint, (intptr_t)boxed_actual), "Expected [%s] to [%s] [%s] within [%d] significant figures\n" "\t\tactual value:\t%08f\n" "\t\texpected value:\t%08f", expression, constraint->name, constraint->expected_value_name, get_significant_figures(), actual, as_double(constraint->expected_value)); free(boxed_actual); constraint->destroy(constraint); }
std::string Value::as_string() const { //if the data is stored as a string if(m_storedAs == valueType_string) { try { return any_cast<std::string>(m_value); } catch(std::bad_cast&) { throw Error_BadDataType(); } } //build the string depending on how the value is stored switch(m_storedAs) { case valueType_uint8: return Utils::toStr(static_cast<uint16>(as_uint8())); case valueType_uint16: return Utils::toStr(as_uint16()); case valueType_uint32: return Utils::toStr(as_uint32()); case valueType_int16: return Utils::toStr(as_int16()); case valueType_int32: return Utils::toStr(as_int32()); case valueType_float: return Utils::toStr(as_float()); case valueType_double: return Utils::toStr(as_double()); case valueType_bool: return Utils::toStr(as_bool()); default: throw Error_BadDataType(); } }
bool Value::as_bool() const { try { switch(m_storedAs) { case valueType_bool: return any_cast<bool>(m_value); case valueType_int16: case valueType_uint32: case valueType_uint16: case valueType_uint8: case valueType_int32: return as_uint32() != 0; case valueType_float: case valueType_double: return as_double() != 0.0; case valueType_string: default: return as_uint32() != 0; } } catch(...) { throw Error_BadDataType(); } }
static inline bool readVizAttribute( GraphAttributes &GA, edge e, const pugi::xml_node tag) { const long attrs = GA.attributes(); if(string(tag.name()) == "viz:color") { if(attrs & GraphAttributes::edgeStyle) { return readColor(GA.strokeColor(e), tag); } } else if(string(tag.name()) == "viz:thickness") { auto thickAttr = tag.attribute("value"); if(!thickAttr) { GraphIO::logger.lout() << "Missing \"value\" on thickness tag." << std::endl; return false; } if(attrs & GraphAttributes::edgeDoubleWeight) { GA.doubleWeight(e) = thickAttr.as_double(); } else if(attrs & GraphAttributes::edgeIntWeight) { GA.intWeight(e) = thickAttr.as_int(); } } else if(string(tag.name()) == "viz:shape") { // Values: solid, dotted, dashed, double. Not supported in OGDF. } else { GraphIO::logger.lout() << "Incorrect tag \"" << tag.name() << "\"." << std::endl; return false; } return true; }
void TranscriptionToolBar::OnEndOff(wxCommandEvent & WXUNUSED(event)) { //If IO is busy, abort immediately if (gAudioIO->IsBusy()){ SetButton(false,mButtons[TTB_EndOff]); return; } mVk->AdjustThreshold(GetSensitivity()); AudacityProject *p = GetActiveProject(); auto t = *p->GetTracks()->Any< const WaveTrack >().begin(); if(t) { auto wt = static_cast<const WaveTrack*>(t); sampleCount start, len; GetSamples(wt, &start, &len); //Adjust length to end if selection is null if(len == 0) { len = start; start = 0; } auto newEnd = mVk->OffBackward(*wt, start + len, len); double newpos = newEnd.as_double() / wt->GetRate(); p->SetSel1(newpos); p->RedrawProject(); SetButton(false, mButtons[TTB_EndOff]); } }
var Real_Eq(var self, var other) { RealData* ro = cast(self, Real); if (type_implements(type_of(other), AsDouble)) { return (var)(intptr_t)(ro->value == as_double(other)); } else { return False; } }
X X::from_json(JSON::Value const& v) { X result; auto& o = as_object(as_array(v)[0]); result.var1 = as_string(o["var1"]); result.var2 = as_double(o["var2"]); return result; }
tab_rep::tab_rep (int pos2, tree t): pos (pos2) { if (N(t) <= 1) { kind= tab_all; weight= 1.0; } else if (t[1] == "first") { kind= tab_first; weight= 0; } else if (t[1] == "last") { kind= tab_last; weight= 0; } else { kind= tab_all; weight= as_double (t[1]); } }
void exec( ) throw( general_error ) { double aep = 1; // annual output, get from performance model double aoe = 0; // annual operating costs double fcr = 0; // fixed charge rate, before tax revenues required double icc = 0; // initial investment, or capital cost double voc = 0; // variable operating cost double foc = 0; // fixed operating cost aep = as_double("annual_energy"); // kWh foc = as_double("fixed_operating_cost"); // $ voc = as_double("variable_operating_cost"); // $/kWh fcr = as_double("fixed_charge_rate"); // unitless fraction icc = as_double("capital_cost"); // $ double lcoe = (fcr*icc + foc) / aep + voc; //$/kWh assign("lcoe_fcr", var_data((ssc_number_t)lcoe)); }
int catajson::as_int() const { double temp = as_double(); int ret = static_cast<int>(temp); if (ret != temp) { debugmsg("JSON warning at %s: value was requested as int, provided as double", path_msg.c_str()); } return ret; }
void TranscriptionToolBar::OnSelectSound(wxCommandEvent & WXUNUSED(event)) { //If IO is busy, abort immediately if (gAudioIO->IsBusy()){ SetButton(false,mButtons[TTB_SelectSound]); return; } mVk->AdjustThreshold(GetSensitivity()); AudacityProject *p = GetActiveProject(); TrackList *tl = p->GetTracks(); TrackListOfKindIterator iter(Track::Wave, tl); Track *t = iter.First(); //Make a track if(t) { auto wt = static_cast<const WaveTrack*>(t); sampleCount start, len; GetSamples(wt, &start, &len); //Adjust length to end if selection is null //if(len == 0) //len = wt->GetSequence()->GetNumSamples()-start; double rate = wt->GetRate(); auto newstart = mVk->OffBackward(*wt, start, start); auto newend = mVk->OffForward(*wt, start + len, (int)(tl->GetEndTime() * rate)); //reset the selection bounds. p->SetSel0(newstart.as_double() / rate); p->SetSel1(newend.as_double() / rate); p->RedrawProject(); } SetButton(false,mButtons[TTB_SelectSound]); }
player accelerate (player pl, tree kind) { if (kind == "reverse") return reverse_player (pl); if (kind == "fade-in") return fade_in_player (pl); if (kind == "fade-out") return fade_out_player (pl); if (kind == "faded") return faded_player (pl); if (kind == "bump") return bump_player (pl); if (is_atomic (kind) && starts (kind->label, "reverse-")) return reverse_player (accelerate (pl, kind->label (8, N(kind->label)))); if (is_tuple (kind, "fixed", 1) && is_atomic (kind[1])) return fixed_player (pl, as_double (kind[1])); return pl; }
void ConstantSet::read_from_netcdf(NcFile &nc, std::string const &vname) { NcVar *ncvar = giss::get_var_safe(nc, vname.c_str(), true); if (!ncvar) { fprintf(stderr, "ConstantSet::read_from_netcdf() cannot find variable %s\n", vname.c_str()); throw std::exception(); } int n = ncvar->num_atts(); // Read through the attributes, getting units and names separately std::map<std::string, std::string> units; std::map<std::string, double> consts; std::map<std::string, std::string> descriptions; for (int i=0; i<n; ++i) { auto att = giss::get_att(ncvar, i); std::string att_name(att->name()); if (giss::ends_with(att_name, "_description")) { descriptions.insert(std::make_pair( att_name.substr(0, att_name.size() - std::strlen("_description")), std::string(att->as_string(0)))); } else if (giss::ends_with(att_name, "_units")) { units.insert(std::make_pair( att_name.substr(0, att_name.size() - std::strlen("_units")), std::string(att->as_string(0)))); } else { consts.insert(std::make_pair( att_name, att->as_double(0))); } } // Now go through them again, matching up constants and units for (auto ii = consts.begin(); ii != consts.end(); ++ii) { std::string const &name = ii->first; double const val = ii->second; auto ui = units.find(name); if (ui == units.end()) { fprintf(stderr, "Could not find _units attribute for %s\n", name.c_str()); } auto di = descriptions.find(name); if (di == descriptions.end()) { fprintf(stderr, "Could not find _description attribute for %s\n", name.c_str()); } std::string const &u = units.find(name)->second; std::string const &d = descriptions.find(name)->second; set(name, val, u, d); } }
/*! Place self inside parents-client area. * Proportions are rect.{x|y|w|h} / base.{x|y|x|y}. * If base.{x|y}==0 then position and size in {X|Y} direction will be preserved. */ void Widget::Place ( const Rect &rect, const Pos &base) { Rect client_area = GetParent()->GetClientRect( this); Rect my_rect = GetBoundingRect() + GetPosition(); double x = my_rect.GetX(); double y = my_rect.GetY(); double w = my_rect.GetW(); double h = my_rect.GetH(); if ( base.x > 0) { x = as_double( client_area.GetW() * rect.GetX() ) / as_double( base.x ); } if ( base.y > 0) { y = as_double( client_area.GetH() * rect.GetY() ) / as_double( base.y ); } if ( base.x > 0) { w = as_double( client_area.GetW() * rect.GetW() ) / as_double( base.x ); } if ( base.y > 0) { h = as_double( client_area.GetH() * rect.GetH() ) / as_double( base.y ); } SetPosition( client_area.GetPos() + Pos( std::floor(x), std::floor(y) )); Resize( std::ceil(w), std::ceil(h) ); }
void TranscriptionToolBar::OnSelectSound(wxCommandEvent & WXUNUSED(event)) { //If IO is busy, abort immediately if (gAudioIO->IsBusy()){ SetButton(false,mButtons[TTB_SelectSound]); return; } mVk->AdjustThreshold(GetSensitivity()); AudacityProject *p = GetActiveProject(); TrackList *tl = p->GetTracks(); if(auto wt = *tl->Any<const WaveTrack>().begin()) { sampleCount start, len; GetSamples(wt, &start, &len); //Adjust length to end if selection is null //if(len == 0) //len = wt->GetSequence()->GetNumSamples()-start; double rate = wt->GetRate(); auto newstart = mVk->OffBackward(*wt, start, start); auto newend = mVk->OffForward(*wt, start + len, (int)(tl->GetEndTime() * rate)); //reset the selection bounds. auto &selectedRegion = p->GetViewInfo().selectedRegion; selectedRegion.setTimes( newstart.as_double() / rate, newend.as_double() / rate ); p->RedrawProject(); } SetButton(false,mButtons[TTB_SelectSound]); }
AnyScalar& AnyScalar::operator+=(const AnyScalar& rhs) { AnyScalar ret; switch (AnyScalar::merge(t, rhs.t)) { case TENSOR_DOUBLE: ret = as_double()+rhs.as_double(); break; case TENSOR_SX: ret = as_SX()+rhs.as_SX(); break; case TENSOR_MX: ret = as_MX()+rhs.as_MX(); break; default: tensor_assert(false); } return this->operator=(ret); }
bool Value::isSameValue(const Value& other) const { switch(m_storedAs) { case valueType_float: return as_float() == other.as_float(); case valueType_double: return as_double() == other.as_double(); case valueType_uint16: return as_uint16() == other.as_uint16(); case valueType_uint32: return as_uint32() == other.as_uint32(); case valueType_int16: return as_int16() == other.as_int16(); case valueType_int32: return as_int32() == other.as_int32(); case valueType_uint8: return as_uint8() == other.as_uint8(); case valueType_bool: return as_bool() == other.as_bool(); case valueType_string: return as_string() == other.as_string(); default: return false; } }
bool EffectReverse::ProcessOneClip(int count, WaveTrack *track, sampleCount start, sampleCount len, sampleCount originalStart, sampleCount originalEnd) { bool rc = true; // keep track of two blocks whose data we will swap auto first = start; auto blockSize = track->GetMaxBlockSize(); float tmp; Floats buffer1{ blockSize }; Floats buffer2{ blockSize }; auto originalLen = originalEnd - originalStart; while (len > 1) { auto block = limitSampleBufferSize( track->GetBestBlockSize(first), len / 2 ); auto second = first + (len - block); track->Get((samplePtr)buffer1.get(), floatSample, first, block); track->Get((samplePtr)buffer2.get(), floatSample, second, block); for (decltype(block) i = 0; i < block; i++) { tmp = buffer1[i]; buffer1[i] = buffer2[block-i-1]; buffer2[block-i-1] = tmp; } track->Set((samplePtr)buffer1.get(), floatSample, first, block); track->Set((samplePtr)buffer2.get(), floatSample, second, block); len -= 2 * block; first += block; if( TrackProgress(count, 2 * ( first - originalStart ).as_double() / originalLen.as_double() ) ) { rc = false; break; } } return rc; }
inline double get_double (string var) { tree t= env [var]; if (is_compound (t)) return 0.0; return as_double (t->label); }
inline double as_double (tree t) { if (is_atomic (t)) return as_double (t->label); else return 0.0; }