Skip to content

Commit

Permalink
Admit inf in the universal monzo basis
Browse files Browse the repository at this point in the history
  • Loading branch information
frostburn committed May 13, 2024
1 parent 254bd7a commit 2dce6f8
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/__tests__/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ describe('Interchange format', () => {
expect(result).toContain('nan');
});

it('has representation for infinity Hz', () => {
const result = toSonicWeaveInterchange('inf * 1 Hz');
expect(result).toContain('inf * 1Hz');
it('has representation for negative infinity Hz', () => {
const result = toSonicWeaveInterchange('-inf * 1 Hz');
expect(result).toContain('[1. 1 1>@Hz.-1.inf');
});

it('has representation for nan Hz (normalizes)', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export type WartBasisElement = BasisFraction | '';

export type ValBasisElement = WartBasisElement | 's' | 'Hz' | 'hz';

export type BasisElement = ValBasisElement | 'rc' | 'r¢' | '1°';
export type BasisElement = ValBasisElement | 'rc' | 'r¢' | 'inf' | '1°';

export type IntegerLiteral = {
type: 'IntegerLiteral';
Expand Down
2 changes: 1 addition & 1 deletion src/grammars/base.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ SecondToken = @'s' !IdentifierPart

ValBasisElement = Fraction / SecondToken / HertzToken / LowHertzToken

BasisElement = ValBasisElement / RealCentToken / 'r¢' / '1°' / ''
BasisElement = ValBasisElement / RealCentToken / 'r¢' / 'inf' / '1°' / ''

ValBasis = (ValBasisElement / '')|.., '.'|

Expand Down
7 changes: 6 additions & 1 deletion src/monzo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ export class TimeReal {
* @returns Monzo literal.
*/
asMonzoLiteral(interchange = false): MonzoLiteral | undefined {
if (!isFinite(this.value)) {
if (isNaN(this.value)) {
return undefined;
}
const components: VectorComponent[] = [];
Expand Down Expand Up @@ -801,6 +801,11 @@ export class TimeReal {
});
return {type: 'MonzoLiteral', components, ups: 0, lifts: 0, basis};
}
if (!isFinite(this.value)) {
basis.push('inf');
components.push({sign: '', left: 1, right: '', exponent: null});
return {type: 'MonzoLiteral', components, ups: 0, lifts: 0, basis};
}
basis.push('rc');
const {sign, whole, fractional, exponent} = numberToDecimalLiteral(
this.totalCents(true),
Expand Down
6 changes: 6 additions & 0 deletions src/parser/__tests__/expression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2397,4 +2397,10 @@ describe('Poor grammar / Fun with "<"', () => {
expect(interval.isAbsolute()).toBe(false);
expect(interval.valueOf()).toBeCloseTo(Math.PI);
});

it('parses universal negative infinite frequency', () => {
const interval = evaluate('[1. 1 1>@Hz.-1.inf') as Interval;
expect(interval.isAbsolute()).toBe(true);
expect(interval.valueOf()).toBe(-Infinity);
});
});
3 changes: 3 additions & 0 deletions src/warts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const TWO_MONZO = new TimeMonzo(ZERO, [ONE]);
const SECOND_MONZO = new TimeMonzo(ONE, []);
const HERTZ_MONZO = new TimeMonzo(NEGATIVE_ONE, []);
const REAL_CENT_MONZO = new TimeReal(0, 1.0005777895065548);
const INF_MONZO = new TimeReal(0, Infinity);

export const STEP_ELEMENT = Symbol();

Expand Down Expand Up @@ -57,6 +58,8 @@ export function parseSubgroup(basis: BasisElement[], targetSize?: number) {
} else if (element === 'rc' || element === 'r¢') {
subgroup.push(REAL_CENT_MONZO);
checkSpan();
} else if (element === 'inf') {
subgroup.push(INF_MONZO);
} else if (element === '1°') {
subgroup.push(STEP_ELEMENT);
checkSpan();
Expand Down

0 comments on commit 2dce6f8

Please sign in to comment.