Public
Edited
Feb 14, 2023
Fork of Blog Post
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
/** Converts a string of digits in the base, possibly containing a decimal point, to a JS number */
function digitsToNumber(numStr, base = 2) {
const numberOfDecimalDigits = (numStr.split(".")[1] || "").length;
const digitsWithoutDecimalPoint = numStr.replace(".", "");
return (
parseInt(digitsWithoutDecimalPoint, base) / Math.pow(base, numberOfDecimalDigits)
);
}
Insert cell
Insert cell
digitsToNumber("10.1", 2)
Insert cell
Insert cell
digitsToNumber("10.1", 2).toString(2)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
vlembed(makeTreeVegaSpec(binaryTreeNodes))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
aBinaryTree = {
const rootNode = new BinaryTreeNode(null, "0");

const zero = new BinaryTreeNode(rootNode, "0");
const oneHalf = new BinaryTreeNode(rootNode, "1"); // == 0.1

const alsoZero = new BinaryTreeNode(zero, "0"); // == 0.00
const alsoOneHalf = new BinaryTreeNode(oneHalf, "0"); // == 0.10

const oneQuarter = new BinaryTreeNode(zero, "1"); // == 0.01
const threeQuarters = new BinaryTreeNode(oneHalf, "1"); // == 0.11

return rootNode;
}
Insert cell
Insert cell
binaryTreeNodes = aBinaryTree.traverseTree()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Class used only to create (or find) the number 0.
// To create other rational numbers with an infinite trailing '0's, use Rational.construct.
class Zero extends Rational {
static construct(digitsNode, index = 0, name = "Zero") {
const newReal = super.construct(digitsNode, index, name, [], ["0"]);
return newReal;
}
}
Insert cell
class One extends Rational {
static construct(digitsNode, index = 0, name = "One") {
const newReal = super.construct(digitsNode, index, name, [], ["1"]);
return newReal;
}
}
Insert cell
Insert cell
function simpleExampleNumberGenerator() {
// The following construction of the rootNode and numbers Zero and One is
// copied to each of the generator functions, with small variations.

// Enumeration of all numbers.
const numbers = [];
let index = 0;

// Generate root of tree, 0.
const rootNode = new BinaryTreeNode(null, "0");

// Generate numbers for 0 and 1, the infinite strings of all '0's and all '1's.
const zero = Zero.construct(rootNode.addDigit("0"), index++, "Zero"); // 0.00̅
const one = One.construct(rootNode.addDigit("1"), index++, "One"); // 0.11̅

// Append to numbers array.
numbers.push(zero);
numbers.push(one);

// Add one more digit to zero and one.
zero.addNextDigit(); // == "0.000̅"
one.addNextDigit(); // == "0.111̅"

// Add '1' digit to zero.node.parent, and create a number for 0.01...
const node01 = zero.node.parent.addDigit("1");
const zeroone = Real.construct(
node01,
index++,
'A number starting with "0.01"'
);
numbers.push(zeroone);

// Add '0' digit to one.node.parent, and create a number for 0.10...
const node10 = one.node.parent.addDigit("0");
const onezero = Real.construct(
node10,
index++,
'A number starting with "0.10"'
);
numbers.push(onezero);

// Return list of nodes by traversing the binary tree.
return { numbers, tree: rootNode.traverseTree() };
}
Insert cell
Insert cell
vlembed(makeTreeVegaSpec(simpleExampleNumbers.tree))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
showEnumerationTable(simpleExampleNumbers)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
showEnumerationTable(ratiosOfOneDenominator)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more