Skip to content

Commit

Permalink
Normalize zero and NaN
Browse files Browse the repository at this point in the history
Give up on universal representation of Infinity and NaN.

ref #317
  • Loading branch information
frostburn committed May 13, 2024
1 parent a237cf5 commit 3861e78
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 81 deletions.
29 changes: 29 additions & 0 deletions src/__tests__/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {describe, it, expect} from 'vitest';
import {toSonicWeaveInterchange} from '../cli';

describe('Interchange format', () => {
it('uses plain monzos up to 23-limit', () => {
const result = toSonicWeaveInterchange('23/16 "test"');
expect(result).toContain('[-4 0 0 0 0 0 0 0 1> "test"');
});

it('has representation for infinity', () => {
const result = toSonicWeaveInterchange('Infinity');
expect(result).toContain('Infinity');
});

it('has representation for NaN', () => {
const result = toSonicWeaveInterchange('NaN');
expect(result).toContain('NaN');
});

it('has representation for Infinity Hz', () => {
const result = toSonicWeaveInterchange('Infinity * 1 Hz');
expect(result).toContain('Infinity * 1Hz');
});

it('has representation for NaN Hz (normalizes)', () => {
const result = toSonicWeaveInterchange('NaN * 1 Hz');
expect(result).toContain('NaN');
});
});
6 changes: 5 additions & 1 deletion src/__tests__/interval.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ describe('Interchange format', () => {
expect(interval.toString()).toBe('[0.>@rc');
});

// Real zero skipped
it('has an expression for real zero', () => {
const interval = new Interval(TimeReal.fromValue(0), 'linear');
interval.node = interval.asMonzoLiteral(true);
expect(interval.toString()).toBe('[1 0.>@0.rc');
});

it('has an expression for real -2', () => {
const interval = new Interval(TimeReal.fromValue(-2), 'linear');
Expand Down
1 change: 1 addition & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {REPLServer, ReplOptions} from 'repl';
import type {Context} from 'node:vm';
import {parse as parenCounter} from './parser/paren-counter';
import {literalToString} from './expression';
import {TimeReal} from './monzo';
const {version} = require('../package.json');

/**
Expand Down
17 changes: 13 additions & 4 deletions src/interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,7 @@ export class Interval {
* @param interchange Boolean flag to format everything explicitly.
* @returns A virtual monzo literal.
*/
asMonzoLiteral(interchange = false): MonzoLiteral {
asMonzoLiteral(interchange = false): MonzoLiteral | undefined {
let node: MonzoLiteral;
if (
interchange &&
Expand All @@ -1089,15 +1089,19 @@ export class Interval {
clone.numberOfComponents = NUM_INTERCHANGE_COMPONENTS;
node = clone.asMonzoLiteral();
} else {
node = this.value.asMonzoLiteral();
const maybeNode = this.value.asMonzoLiteral();
if (maybeNode === undefined) {
return undefined;
}
node = maybeNode;
}
if (
interchange &&
(node.basis.length ||
node.components.length > NUM_INTERCHANGE_COMPONENTS ||
this.steps)
) {
node = this.value.asInterchangeLiteral();
node = this.value.asInterchangeLiteral()!;
}
if (this.steps) {
if (!node.basis.length && node.components.length) {
Expand All @@ -1124,7 +1128,12 @@ export class Interval {
if (this.isPureSteps()) {
result = `${this.steps}°`;
} else {
result = literalToString(this.asMonzoLiteral());
const node = this.asMonzoLiteral();
if (node) {
result = literalToString(node);
} else {
result = this.value.toString();
}
}
if (this.domain === 'linear') {
return `linear(${result})`;
Expand Down
Loading

0 comments on commit 3861e78

Please sign in to comment.