public function deviation(array $a) { $max = abs(math::max($a)); $min = abs(math::min($a)); $avg = abs(math::average($a)); return $avg - ($max + $min) / 2; }
function run() { $cases = Lunit::getCases(); $casedata = array(); // Enumerate the cases foreach ($cases as $case) { // Setup report structure $casereport = array(); // Reflect the class to find methods and metadata $r = new ReflectionClass($case); $ml = $r->getMethods(); $skip = false; $meta = LunitUtil::parseDoc($r->getDocComment()); if (!isset($meta['description'])) { $meta['description'] = $case; } $meta['casename'] = $case; if (isset($meta['extensions'])) { $extn = explode(' ', $meta['extensions']); foreach ($extn as $ext) { if (!extension_loaded($ext)) { $skip = true; $skipmsg = "Need extension: " . $ext; } } } $casereport['meta'] = $meta; // Callback if set if ($this->statuscb) { $this->statuscb->onCaseBegin($case, $meta); } if ($this->dblog) { $this->dblog->onCaseBegin($case, $meta); } try { if (!$skip) { $tc = new $case($this); } foreach ($ml as $method) { $methodname = $method->getName(); if ($method->isPublic() && substr($methodname, 0, 1) != '_') { $methodreport = array(); $tmeta = LunitUtil::parseDoc($method->getDocComment()); if (!isset($tmeta['description'])) { $tmeta['description'] = $methodname; } if (!isset($tmeta['repeat'])) { $tmeta['repeat'] = 1; } // Save meta to method report $methodreport['meta'] = $tmeta; // Times to repeat the test $repeat = intval($tmeta['repeat']); // Callback if set, then create timer if ($this->statuscb) { $this->statuscb->onTestBegin($methodname, $tmeta); } if ($this->dblog) { $this->dblog->onTestBegin($methodname, $meta); } $methodreport['skipped'] = false; $tavg = null; $tmax = null; $tmin = null; if (!$skip) { $tm = new Timer(); try { $telapsed = array(); $ttotal = 0; for ($n = 0; $n < $repeat; $n++) { $tm->start(); $tc->{$methodname}(); $tm->stop(); $telapsed[] = $tm->getElapsed() * 1000; $ttotal += $tm->getElapsed() * 1000; } $ttot = math::sum($telapsed); $tavg = math::average($telapsed); $tmin = math::min($telapsed); $tmax = math::max($telapsed); $tdev = math::deviation($telapsed); $methodreport['passed'] = true; $methodreport['message'] = null; if ($repeat > 1) { // console::write('%6.1fms <%6.1fms> %6.1fms ', $tmin, $tavg, $tmax); } else { // console::write('%6.1fms ', $tmax); } if ($this->statuscb) { $this->statuscb->onTestEnd(true, null); } if ($this->dblog) { $this->dblog->onTestEnd(true, null); } } catch (LunitAssertionFailure $f) { $tm->stop(); $methodreport['passed'] = false; $methodreport['message'] = $f->getMessage(); if ($this->statuscb) { $this->statuscb->onTestEnd(false, $f->getMessage()); } if ($this->dblog) { $this->dblog->onTestEnd(false, $f->getMessage()); } } catch (LunitAssertionSkip $f) { $tm->stop(); $methodreport['passed'] = false; $methodreport['skipped'] = true; $methodreport['message'] = 'Skipped'; if ($this->statuscb) { $this->statuscb->onTestEnd(null, $f->getMessage()); } if ($this->dblog) { $this->dblog->onTestEnd(null, $f->getMessage()); } } catch (Exception $e) { $tm->stop(); $methodreport['passed'] = false; $methodreport['message'] = $e->getMessage(); if ($this->statuscb) { $this->statuscb->onTestEnd(false, $e->getMessage()); } if ($this->dblog) { $this->dblog->onTestEnd(false, $f->getMessage()); } } } else { $methodreport['passed'] = false; $methodreport['skipped'] = true; $methodreport['message'] = $skipmsg; $this->statuscb->onTestEnd(null, $skipmsg); if ($this->dblog) { $this->dblog->onTestEnd(null, $skipmsg); } } $methodreport['elapsed'][] = $tm->getElapsed(); $methodreport['average'] = $tavg; $methodreport['minmax'] = array($tmin, $tmax); // Save report $casereport['tests'][$methodname] = $methodreport; } } } catch (Exception $e) { console::writeLn("Skipped due to exception: %s", $e->getMessage()); } $casedata[$case] = $casereport; // Callback if set if ($this->statuscb) { $this->statuscb->onCaseEnd(); } if ($this->dblog) { $this->dblog->onCaseEnd($casereport); } } $this->results = $casedata; }
/** * Scale * * @param int $scale */ public static function scale($scale) { self::$scale = $scale; bcscale($scale); }
function calculate_planet_position($radius, $covered) { $outline = 2 * $radius * M_PI; $planet_x = $planet_y = $this->system_map_size / 2; if ($covered < 1) { $planet_y -= $radius; } else { if ($covered > $outline) { $covered = 0; } $angle = 360 * $covered / $outline; /* $quadrant = ceil($angle / 90); $angle -= ($quadrant - 1) * 90; */ if ($angle <= 90) { $quadrant = 1; } elseif ($angle > 90 && $angle <= 180) { $quadrant = 2; $angle -= 90; } elseif ($angle > 180 && $angle <= 270) { $quadrant = 3; $angle -= 180; } elseif ($angle > 270 && $angle <= 360) { $quadrant = 4; $angle -= 270; } else { return message(GENERAL, 'Bad angle ' . $angle . '° of planet distance ' . $radius); } $tri_a = sin($angle * M_PI / 180) * $radius; $tri_b = math::pythagoras_hc($radius, $tri_a); switch ($quadrant) { case 1: $planet_x += $tri_a; $planet_y -= $tri_b; break; case 2: $planet_x += $tri_b; $planet_y += $tri_a; break; case 3: $planet_x -= $tri_a; $planet_y += $tri_b; break; case 4: $planet_x -= $tri_b; $planet_y -= $tri_a; break; } } return array($planet_x, $planet_y); }
/** * Find only the max per page by default * @Developer brandon * @Date Apr 20, 2010 */ public function find_all($page = NULL) { return parent::find_all(Kohana::config('pagination.default.items_per_page'), math::page($page)); }
/** * @en Factorial calculator * @ru Вычисление факториала числа * * $result = math::factorial(3); # int(6) * * @param int $number * * @return int */ public static function factorial($number) { return $number ? $number * math::factorial($number - 1) : 1; }
/** * Validate required one field * * @param mixed $value * @param string $error_name * @param array $options */ public function validate_required_one_field(&$value, $error_name, $options) { // if we have type errors we skip required validation if ($this->has_errors($error_name)) { return; } // check if its required field if (isset($options['options']['required']) && ($options['options']['required'] === true || $options['options']['required'] . '' === '1')) { if ($options['options']['php_type'] == 'integer' || $options['options']['php_type'] == 'float') { if (empty($value)) { $this->error('danger', object_content_messages::required_field, $error_name); } } else { if ($options['options']['php_type'] == 'bcnumeric') { // accounting numbers if (math::compare($value, '0') == 0) { $this->error('danger', object_content_messages::required_field, $error_name); } } else { if (!empty($options['options']['multiple_column'])) { if (empty($value)) { $this->error('danger', object_content_messages::required_field, $error_name); } } else { if ($value . '' == '') { $this->error('danger', object_content_messages::required_field, $error_name); } } } } } // validator if (!empty($options['options']['validator_method']) && !empty($value)) { $neighbouring_values_key = $options['options']['values_key']; array_pop($neighbouring_values_key); $temp = object_validator_base::method($options['options']['validator_method'], $value, $options['options']['validator_params'] ?? [], $options['options'], array_key_get($this->values, $neighbouring_values_key)); if (!$temp['success']) { foreach ($temp['error'] as $v10) { $this->error('danger', $v10, $error_name); } } else { if (!empty($temp['data'])) { $value = $temp['data']; } } } }
/** * Find only the max per page by default * @Developer brandon * @Date Apr 20, 2010 */ public function find_all($page = NULL) { if (array_key_exists('store_id', $this->table_columns)) { $this->where('store_id', Kohana::config('store.id')); } return parent::find_all(Kohana::config('pagination.default.items_per_page'), math::page($page)); }
98158613184731646854608754802513285673669494264747 27227865422124526123045507906391762175841116643793 39850974894692494486368052068371819815910113761040 63490299853191179995399246673356287563758444418220 53154165762550556600954767900019986378970992788078 64797215974157442597089748525751049202572404302198 11405262167097524195464078289767857222196498196134 29687534751215212055058906496964672558760320501154 91133643125025140132825568220788949708373324718483 57376931880125809438719502505545465368409685529830 71223388766326899429481386092863529999034542088544 33995917053899725007399139847689566709529191189337 84790942271541382242627748059894973082898889736963 87206620190357534346248030013849556514356211899613 21043428350991221189032210186899219729536503580454 45921258692976503811486552863113407178171167533442 74150896154954863910644978643565912769523517994679 75951901206636834598184405124056408120655559297841 67644459127085238805625953993846409363249489129468 98499869120140979681802667136467929596221650382127 73907675497041137141887536315127577976650808741634 54192521026103408661173368701953261361195486615212 79435343347066625586018352353537660649186444536550 70517728982618553879543271828187149743971001556862 52973321283417716122503456539069424763416261020226 50641658018819365611278644990768554434421999162162'; // Преобразовать список чисел в массив $array = explode("\r\n", $string); // Вывести сумму чисел массива echo math::sum($array); // 3392787000171382151515600556927976920245731910265213
<?php class math { function __call($name, $args) { switch ($name) { case 'add': return array_sum($args); case 'subtract': $val = array_shift($args); foreach ($args as $v) { $val /= $v; } return $val; } } } $m = new math(); echo $m->add(1, 2); // will print 3 echo $m->subtract(8, 2); // will print 4
<?php class math { function __call($name, $args) { switch ($name) { case 'add': return array_sum($args); case 'divide': $val = array_shift($args); foreach ($args as $v) { $val /= $v; } return $val; } } } $m = new math(); echo $m->add(1, 2); // will print 3 echo $m->divide(8, 2); // will print 4