Skip to content

Commit

Permalink
Update the boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
ishiko732 committed Jan 6, 2025
1 parent 2a02143 commit 3209f52
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
50 changes: 50 additions & 0 deletions __tests__/algorithm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,53 @@ describe('change Params', () => {
}).toThrow('Requested retention rate should be in the range (0,1]')
})
})

describe('next_state', () => {
it('next_state not NaN', () => {
const f = fsrs()
const next_state = f.next_state(
{ stability: 0, difficulty: 0 },
1,
1 /** Again */
)

expect(Number.isNaN(next_state.stability)).toBe(false)
expect(next_state).toEqual(f.next_state(null, 1, 1 /** Again */))
expect(next_state).toEqual(
f.next_state({ difficulty: 0, stability: 0 }, 1, 1 /** Again */)
)
})

it('invalid memory state', () => {
const f = fsrs()

const init = f.next_state(null, 0, 3 /** Good */)
// d<1
expect(() => {
f.next_state(
{ stability: init.stability, difficulty: 0 },
1,
1 /** Again */
)
}).toThrow('invalid memory state')

// s<0.01
expect(() => {
f.next_state(
{ stability: 0, difficulty: init.stability },
1,
1 /** Again */
)
}).toThrow('invalid memory state')

// g<0
expect(() => {
f.next_state(init, 1, -1 /** invalid grade */)
}).toThrow('invalid memory state')

// g>4
expect(() => {
f.next_state(init, 1, 5 /** invalid grade */)
}).toThrow('invalid memory state')
})
})
12 changes: 9 additions & 3 deletions src/fsrs/algorithm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,20 @@ export class FSRSAlgorithm {
* @returns The next state of memory with updated difficulty and stability.
*/
next_state(memory_state: FSRSState | null, t: number, g: number): FSRSState {
if (!memory_state) {
const { difficulty: d, stability: s } = memory_state ?? {
difficulty: 0,
stability: 0,
}
if (d === 0 && s === 0) {
return {
difficulty: this.init_difficulty(clamp(g, 1, 4)),
stability: this.init_stability(clamp(g, 1, 4)),
}
}
const { difficulty: d, stability: s } = memory_state
if (g < 1 || g > 4) {
if (d < 1 || s < 0.01 || g < 0 || g > 4) {
throw new Error('invalid memory state')
}
if (g === 0) {
return {
difficulty: d,
stability: s,
Expand Down

0 comments on commit 3209f52

Please sign in to comment.