/** * Madara function that determines if an argument is false **/ madara::knowledge::KnowledgeRecord is_false (madara::knowledge::FunctionArguments & args, madara::knowledge::Variables & variables) { // if we've been provided with an argument, check if it is true if (args.size () == 1) return madara::knowledge::KnowledgeRecord::Integer ( args[0].to_double () < 0.5 && args[0].to_double () >= 0); // if the user didn't provide an argument, return false else return madara::knowledge::KnowledgeRecord::Integer (0); }
/** * Madara function that returns a random number. No argument means to generate * a random number up to the limits of the stdlib.h implementation's limits. An * integer argument means to generate a random up to the specified arg limit. **/ madara::knowledge::KnowledgeRecord rand_int (madara::knowledge::FunctionArguments & args, madara::knowledge::Variables & variables) { // if the args list is greater than zero, is an integer, and is not 0 if (args.size () > 0 && args[0].type () == madara::knowledge::KnowledgeRecord::INTEGER && args[0].to_integer () != 0) { // then call rand () with a modulus up to that integer argument return madara::knowledge::KnowledgeRecord::Integer ( rand () % (int)args[0].to_integer ()); } // otherwise, just return rand () else return madara::knowledge::KnowledgeRecord::Integer (rand ()); }
Record to_legible_hertz(engine::FunctionArguments & args, engine::Variables & /*vars*/) { Record result; if (args.size() == 1) { std::stringstream buffer; std::locale loc("C"); buffer.imbue(loc); const int ghz_mark = 1000000000; const int mhz_mark = 1000000; const int khz_mark = 1000; double hertz = args[0].to_double(); double freq = hertz / ghz_mark; if (freq >= 1) { buffer << std::setprecision(2) << std::fixed; buffer << freq; buffer << " ghz"; result = Record(buffer.str().c_str()); } else { freq = hertz / mhz_mark; if (freq >= 1) { buffer << std::setprecision(2) << std::fixed; buffer << freq; buffer << " mhz"; result = Record(buffer.str().c_str()); } else { freq = hertz / khz_mark; if (freq >= 1) { buffer << std::setprecision(2) << std::fixed; buffer << freq; buffer << " khz"; result = Record(buffer.str().c_str()); } else { freq = hertz; buffer << std::setprecision(2) << std::fixed; buffer << freq; buffer << " hz"; result = Record(buffer.str().c_str()); } } } } return result; }