Skip to content

Instantly share code, notes, and snippets.

View Yaffle's full-sized avatar

Viktor Yaffle

View GitHub Profile
@Yaffle
Yaffle / gist:6027002
Created July 18, 2013 06:00
BigInteger modInverse
var ZERO = new BigInteger("0");
var ONE = new BigInteger("1");
function modInverse(a, b) {
// http://en.wikipedia.org/wiki/Modular_multiplicative_inverse#Extended_Euclidean_algorithm
// http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Iterative_method_2
var m = b.abs();
var aSign = a.compareTo(ZERO);
var x = ZERO;
var lastx = ONE;
var tmp = null;
@Yaffle
Yaffle / websql.php
Last active April 30, 2020 18:52
Web SQL Database API for PHP
<?php
/* usage:
$db = new Database("mysql:host=" . Config::$dbHost . ";dbname=" . Config::$dbName . ";", Config::$dbName, Config::$dbUser, Config::$dbPass);
$db->changeVersion("", "1", function ($tx) {
$tx->executeSql("CREATE TABLE IF NOT EXISTS `examples` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`example` mediumtext,
PRIMARY KEY (`id`)
setTimeout(function () {
var x = new XMLHttpRequest();
var url = "http://www.enhanceie.com/test/stream.aspx?" + Math.random();
x.open("GET", url, true);
x.responseType = "ms-stream";
var reader = new MSStreamReader();
reader.onload = function (e) {
var s = reader.result;
console.log(s);
var cJSON = {
parse: function (text) {
var lastVisitedIndex = -1;
var results = JSON.parse(text);
var f = function (index) {
var x = results[index];
if (index <= lastVisitedIndex) {
return x;
}
lastVisitedIndex = index;
@Yaffle
Yaffle / TextEncoderTextDecoder.js
Last active February 21, 2024 18:27
TextEncoder/TextDecoder polyfills for utf-8
// TextEncoder/TextDecoder polyfills for utf-8 - an implementation of TextEncoder/TextDecoder APIs
// Written in 2013 by Viktor Mukhachev <vic99999@yandex.ru>
// To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
// You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
// Some important notes about the polyfill below:
// Native TextEncoder/TextDecoder implementation is overwritten
// String.prototype.codePointAt polyfill not included, as well as String.fromCodePoint
// TextEncoder.prototype.encode returns a regular array instead of Uint8Array
// No options (fatal of the TextDecoder constructor and stream of the TextDecoder.prototype.decode method) are supported.
<!DOCTYPE html>
<style>
.f {
width: 105px;
}
.f > div {
overflow-y: scroll;
border-left: 1px solid blue;
width: 24%;
@Yaffle
Yaffle / Math.nextUp.js
Last active February 2, 2017 22:49
Math.nextAfter, Math.nextDown, Math.nextUp, Math.ulp in javascript
(function (global) {
"use strict";
// Math.nextUp
// Note:
// Math.nextDown = function (x) { return -Math.nextUp(-x); };
// Math.nextAfter = function (x, y) { return y < x ? -Math.nextUp(-x) : (y > x ? Math.nextUp(x) : (x !== x ? x : y)); };
// Math.ulp = function (x) { return x < 0 ? Math.nextUp(x) - x : x - (-Math.nextUp(-x)); };
var EPSILON = Math.pow(2, -52);
// https://bugzilla.mozilla.org/show_bug.cgi?id=697151
(function () {
var s = [];
var url = "https://cors-test.appspot.com/test";
setTimeout(function () {
s.push('? - setTimeout');
}, 0);
function getStep(min, max, intervals) {
var range = max - min;
var a = range / (intervals - 1);
var b = range / (intervals - 2);
// a <= nice < b
// -2 -1 0 1 2 3 4
// 10^-1; 10^0 / 2; 10^0; 10^1 / 2; 10^1; 10^2 / 2; 10^3; ...
function f(i) {
@Yaffle
Yaffle / gist:3743508
Last active October 10, 2015 19:59
fix for Number.prototype.toFixed
(function () {
"use strict";
var base = 1e7;
var size = 6;
var data = [];
var i = -1;
while (++i < size) {
data[i] = 0;
}