Skip to content

Commit

Permalink
float -> binary
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Theisen committed Aug 31, 2019
1 parent 0e9a9f8 commit 07a720a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
12 changes: 9 additions & 3 deletions StaxLang.Interpreter/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,7 @@ private IEnumerable<ExecutionState> RunSteps(Block block) {
}
break;
case 'B':
if (IsInt(Peek())) {
DoOverlappingBatch(block);
}
if (IsInt(Peek())) DoOverlappingBatch(block);
else if (IsArray(Peek())) {
block.AddDesc("uncons; remove first element from array and push both");
RunMacro("c1tsh");
Expand All @@ -486,6 +484,14 @@ private IEnumerable<ExecutionState> RunSteps(Block block) {
block.AddDesc("properize fraction; push integer floor and remainder of fraction separately");
RunMacro("c@s1%");
}
else if (Peek() is double) {
block.AddDesc("get binary representation of floating point number");
byte[] bytes = BitConverter.GetBytes(Pop());
ulong u = BitConverter.ToUInt64(bytes, 0);
var list = new List<object>(64);
for (int i = 63; i >= 0; i--) list.Add((BigInteger)(u >> i & 1));
Push(list);
}
else if (IsBlock(Peek())) {
Block b = Pop();
for (int i = 0; i < 3; i++) {
Expand Down
11 changes: 11 additions & 0 deletions StaxLang.JS/stax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,17 @@ export class Runtime {
if (isInt(this.peek())) this.doOverlappingBatch();
else if (isArray(this.peek())) this.runMacro("c1tsh"); // uncons
else if (this.peek() instanceof Rational) this.runMacro("c@s1%"); // properize
else if (typeof this.peek() === "number") {
let buf = new ArrayBuffer(8), view = new DataView(buf);
view.setFloat64(0, this.pop() as number);
let result: StaxInt[] = [];
for (let i = 0; i < 8; i++) {
for (let j = 7; j >= 0; j--) {
result.push(view.getUint8(i) >> j & 1 ? one : zero);
}
}
this.push(result);
}
else if (this.peek() instanceof Block) {
let b = this.pop() as Block;
for (let i = 0; i < 3; i++) {
Expand Down
1 change: 1 addition & 0 deletions docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ op |Types |Name |Pseudo-code |Des
`v` |num |dec |a - 1 |Decrement by 1.
`^` |num |inc |a + 1 |Increment by 1.
`B` |frac |properize |floor(a), a%1 |Properize fraction. Push integer floor, and proper remainder separately.
`B` |float |float-binary | |Get binary IEEE-754 representation as 64-element array of bits.
`D` |num |frac-part |a%1 |Get fractional non-integer part of rational or float.
`e` |frac |ceil |ceiling(a) |Integer ceiling of fraction.
`e` |float |ceil |ceiling(a) |Integer ceiling of float.
Expand Down
17 changes: 16 additions & 1 deletion testspecs/numerics.staxtest
Original file line number Diff line number Diff line change
Expand Up @@ -1244,4 +1244,19 @@ me|N
1
0
stax
me:N
me:N
name:FloatBinary
in
0.0
1e-100
1e-1
1e0
1e100
out
0000000000000000000000000000000000000000000000000000000000000000
0010101100101011111111110010111011100100100011100000010100110000
0011111110111001100110011001100110011001100110011001100110011010
0011111111110000000000000000000000000000000000000000000000000000
0101010010110010010010011010110100100101100101001100001101111101
stax
meB$

0 comments on commit 07a720a

Please sign in to comment.