/** * @param string $s * @return string|null bcmath number * @throws \Exception */ public static function parse($s) { //bcscale(2); $res = self::parsePrivate($s); if ($res === null) { return null; } return BigNumber::stripTrailingZeroes($res); }
public static function BitwiseAnd($left_operand, $right_operand) { $val1 = BigNumber::Dec2Bin($left_operand); $val2 = BigNumber::Dec2Bin($right_operand); $len1 = strlen($val1); $len2 = strlen($val2); $maxlen = max($len1, $len2); for ($i = $len1; $i < $maxlen; $i++) { $val1 = "0" . $val1; } for ($i = $len2; $i < $maxlen; $i++) { $val2 = "0" . $val2; } $result = ""; for ($i = 0; $i < $maxlen; $i++) { $char = "0"; if ($val1[$i] == "1" && $val2[$i] == "1") { $char = "1"; } $result = $result . $char; } return BigNumber::Bin2Dec($result); }
public function testAddTableWithDecimals() { $e = null; try { $this->_conn->selectValues("SELECT * FROM big_numbers"); } catch (Exception $e) { } $this->assertInstanceOf('Horde_Db_Exception', $e); $m = new GiveMeBigNumbers(); $m->up(); $result = BigNumber::create(array('bank_balance' => '1586.43', 'big_bank_balance' => "1000234000567.95", 'world_population' => '6000000000', 'my_house_population' => '3', 'value_of_e' => "2.7182818284590452353602875")); $this->assertInstanceOf('BigNumber', $result); $b = BigNumber::find('first'); $this->assertNotNull($b->bank_balance); $this->assertNotNull($b->big_bank_balance); $this->assertNotNull($b->world_population); $this->assertNotNull($b->my_house_population); $this->assertNotNull($b->value_of_e); $m->down(); $e = null; try { $this->_conn->selectValues("SELECT * FROM big_numbers"); } catch (Exception $e) { } $this->assertInstanceOf('Horde_Db_Exception', $e); }
/** * Raise e to the power of this. * @param number $precision the precision we want to return this to (default = DEFAULT_PRECISION). * @return BigNumber e raised to the power of *this. */ public function Exp($precision = self::BIGNUMBER_DEFAULT_PRECISION) { // shortcut if ($this->IsZero()) { // reset this to 1 $this->_Copy(BigNumberConstants::One()); // done return $this->PerformPostOperations($precision); } // get the integer part of the number. $integer = (new BigNumber($this))->Integer(); // now get the decimal part of the number $fraction = (new BigNumber($this))->Frac(); // reset 'this' to 1 $this->_Copy(BigNumberConstants::One()); // the two sides of the equation // the whole number. if (!$integer->IsZero()) { // get the value of e $e = BigNumberConstants::e(); // truncate the precision so we do not do too many multiplications. // add a bit of room for more accurate precision. $e->Trunc(BigNumberConstants::PrecisionPadding($precision)); // then raise it. $this->_Copy($e->Pow($integer, BigNumberConstants::PrecisionPadding($precision))); } if (!$fraction->IsZero()) { // x^1 x^2 x^3 // 1 + --- + --- + --- ... // 1! 2! 3! $fact = BigNumberConstants::One(); $base = new BigNumber($fraction); $power = new BigNumber($base); $result = BigNumberConstants::One(); $paddedPrecision = BigNumberConstants::PrecisionPadding($precision); for ($i = 1; $i < self::BIGNUMBER_MAX_EXP_ITERATIONS; ++$i) { // calculate the number up to the precision we are after. $calulatedNumber = (new BigNumber($power))->Div($fact, $paddedPrecision); if ($calulatedNumber->IsZero()) { break; } // add it to our number. $result->Add($calulatedNumber); // x * x * x ... $power->Mul($base, $paddedPrecision); // 1 * 2 * 3 ... $fact->Mul((int) ($i + 1), $paddedPrecision); } // the decimal part of the number. $fraction = $result; // multiply the decimal number with the fraction. $this->Mul($fraction, BigNumberConstants::PrecisionPadding($precision)); } // clean up and return. return $this->PerformPostOperations($precision); }
public function ReadForumReplies($topic, $start = 0, $count = 10) { $this->UpdateTopicWatch($topic); $result = $this->CoreSQL("SELECT t1.*, t2.Name AS AuthorName, \nt2.CharID AS AuthorCharID, t2.Signature AS Signature, t2.Title AS \nAuthorTitle, t2.CorporationTicker AS AuthorCorpTicker, \nt2.PortalRoles AS AuthorPortalRoles, t3.Name as \nEditedByName FROM forum_replies AS t1 INNER JOIN users AS t2 ON t1.AuthorID = t2.id LEFT JOIN users AS t3 ON t1.EditedBy = t3.id WHERE t1.TopicID = " . $topic . " AND t1.IsDeleted = FALSE ORDER BY t1.DateCreated ASC LIMIT " . $start . "," . $count); if (mysql_num_rows($result) == 0) { return array(); } $replies = array(); while ($row = mysql_fetch_assoc($result)) { $reply = new Reply(); $reply->ID = $row["id"]; $reply->TopicID = $row["TopicID"]; $reply->AuthorID = $row["AuthorID"]; $reply->AuthorName = $this->SQLUnEscape($row["AuthorName"]); $reply->AuthorCharID = $row["AuthorCharID"]; $reply->AuthorSignature = $this->SQLUnEscape($row["Signature"]); $reply->AuthorTitle = $this->SQLUnEscape($row["AuthorTitle"]); $reply->AuthorCorpTicker = $this->SQLUnEscape($row["AuthorCorpTicker"]); $reply->IsHonorary = BigNumber::Compare(BigNumber::BitwiseAnd($row["AuthorPortalRoles"], User::MDYN_HonoraryMember), "0") != 0; if ($reply->IsHonorary && $reply->AuthorTitle == "") { $reply->AuthorTitle = "Honorary Member"; } $reply->Text = $this->SQLUnEscape($row["Text"]); $reply->DateCreated = $row["DateCreated"]; $reply->DateEdited = $row["DateEdited"]; $reply->EditedByID = $row["EditedBy"]; $reply->EditedByName = $this->SQLUnEscape($row["EditedByName"]); $reply->DateDeleted = $row["DateDeleted"]; $reply->IsDeleted = $row["IsDeleted"]; $reply->ShowSignature = $row["ShowSignature"]; $reply->ShowEdited = $row["ShowEdited"]; $replies[] = $reply; } mysql_free_result($result); return $replies; }
$roles = BigNumber::Add($roles, User::MDYN_ForumModerator); } if (@$_POST["manager" . $user->ID] == "on") { $roles = BigNumber::Add($roles, User::MDYN_Manager); } if (@$_POST["admin" . $user->ID] == "on") { $roles = BigNumber::Add($roles, User::MDYN_Administrator); } if (@$_POST["dev" . $user->ID] == "on") { $roles = BigNumber::Add($roles, User::MDYN_Developer); } if (@$_POST["honorary" . $user->ID] == "on") { $roles = BigNumber::Add($roles, User::MDYN_HonoraryMember); } if (@$_POST["allyleader" . $user->ID] == "on") { $roles = BigNumber::Add($roles, User::MDYN_AllyLeader); } $user->PortalRoles = $roles; } $cms->UpdateAllUserRoles($users); $cms->Log("Edited user roles."); } $cms->Goto("admin.php"); } elseif ($action == "guests") { $users = $cms->GetAllUsers(false, true); usort($users, "objcmp"); $guests = array(); $allies = array(); foreach ($users as $user) { if ($user->IsGuest) { $guests[] = $user;
public function HasPortalRole($role) { if (empty($role)) { return true; } return BigNumber::Compare(BigNumber::BitwiseAnd($this->PortalRoles, $role), "0") != 0; }
/** * Tests the possibility of a "negative" string zero, i.e. "-0.000" * * The sign of -0 is still a negative sign. This is ultimately calculated * by bccomp(), according to which, when -0.000 is compared to 0.000, it * will return a -1, meaning -0.000 is less than 0.000, but -0 compared to * 0 will return a 0, meaning the two are equal. This is odd, but it is the * expected behavior. */ public function testNegativeZero() { $bn = new BigNumber('-0.0000005', 3); $this->assertSame('-0.000', $bn->getValue()); $this->assertEquals(-1, $bn->signum()); $this->assertTrue($bn->isNegative()); }