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 3209f52 commit d38a166
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
9 changes: 9 additions & 0 deletions __tests__/algorithm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,15 @@ describe('next_state', () => {
)
}).toThrow('invalid memory state')

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

// g<0
expect(() => {
f.next_state(init, 1, -1 /** invalid grade */)
Expand Down
13 changes: 8 additions & 5 deletions src/fsrs/algorithm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,21 +289,24 @@ export class FSRSAlgorithm {
difficulty: 0,
stability: 0,
}
if (g < 0 || g > 4 || t < 0) {
throw new Error('invalid memory state')
}
if (d === 0 && s === 0) {
return {
difficulty: this.init_difficulty(clamp(g, 1, 4)),
stability: this.init_stability(clamp(g, 1, 4)),
difficulty: this.init_difficulty(g),
stability: this.init_stability(g),
}
}
if (d < 1 || s < 0.01 || g < 0 || g > 4) {
throw new Error('invalid memory state')
}
if (g === 0) {
return {
difficulty: d,
stability: s,
}
}
if (d < 1 || s < 0.01) {
throw new Error('invalid memory state')
}
const r = this.forgetting_curve(t, s)
const s_after_success = this.next_recall_stability(d, s, r, g)
const s_after_fail = this.next_forget_stability(d, s, r)
Expand Down

0 comments on commit d38a166

Please sign in to comment.