Skip to content

EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust

Expressions: timeToFrames keeps whole durations whole under float error

bflatasticcc138fcb9036
diff --git a/crates/expr/src/prelude.js b/crates/expr/src/prelude.js
index e28f2cd6c..a1dff678d 100644
--- a/crates/expr/src/prelude.js
+++ b/crates/expr/src/prelude.js
@@ -262,7 +262,8 @@ function timeToFrames(t, fps, isDuration) {
if (t === undefined) t = time + thisComp.displayStartTime;
if (fps === undefined) fps = 1 / __S.fd;
var frames = t * fps;
- if (isDuration && frames !== 0) return frames < 0 ? Math.floor(frames) : Math.ceil(frames);
+ // The 1e-6 keeps a whole number of frames that floating point overshoots (31/24 - 5/24) whole.
+ if (isDuration && frames !== 0) return frames < 0 ? Math.floor(frames + 1e-6) : Math.ceil(frames - 1e-6);
return Math.floor(frames + 1e-6);
}
diff --git a/crates/expr/src/tests.rs b/crates/expr/src/tests.rs
index fd1cebbb4..8a4db485b 100644
--- a/crates/expr/src/tests.rs
+++ b/crates/expr/src/tests.rs
@@ -1350,8 +1350,8 @@ fn time_to_frames_preserves_coercion_and_small_durations() {
("timeToFrames(null, 24, true)", 0.0),
("timeToFrames(0.05, null, true)", 0.0),
("timeToFrames(0.05, 0, true)", 0.0),
- ("timeToFrames(1e-10, 24, true)", 1.0),
- ("timeToFrames(-1e-10, 24, true)", -1.0),
+ ("timeToFrames(31 / 24 - 5 / 24, 24, true)", 26.0),
+ ("timeToFrames(5 / 24 - 31 / 24, 24, true)", -26.0),
("timeToFrames(-1e-10, 24, false)", 0.0),
("1 / timeToFrames(-0, 24, true) === Infinity ? 1 : 0", 1.0),
("isNaN(timeToFrames(NaN, 24, true)) ? 1 : 0", 1.0),