From c7877dbd189c76dc357ef6755b4ecd50aecd1eea Mon Sep 17 00:00:00 2001 From: Alexander Piskun <13381981+bigcat88@users.noreply.github.com> Date: Mon, 7 Jul 2025 01:40:27 +0300 Subject: [PATCH] fix(float-precision): correct float widget rounding (#4291) Signed-off-by: Alexander Piskun --- src/composables/widgets/useFloatWidget.ts | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/composables/widgets/useFloatWidget.ts b/src/composables/widgets/useFloatWidget.ts index b0768893b..bdf059742 100644 --- a/src/composables/widgets/useFloatWidget.ts +++ b/src/composables/widgets/useFloatWidget.ts @@ -10,14 +10,19 @@ import { type ComfyWidgetConstructorV2 } from '@/scripts/widgets' import { useSettingStore } from '@/stores/settingStore' function onFloatValueChange(this: INumericWidget, v: number) { - this.value = this.options.round - ? _.clamp( - Math.round((v + Number.EPSILON) / this.options.round) * - this.options.round, - this.options.min ?? -Infinity, - this.options.max ?? Infinity - ) - : v + const round = this.options.round + if (round) { + const precision = + this.options.precision ?? Math.max(0, -Math.floor(Math.log10(round))) + const rounded = Math.round(v / round) * round + this.value = _.clamp( + Number(rounded.toFixed(precision)), + this.options.min ?? -Infinity, + this.options.max ?? Infinity + ) + } else { + this.value = v + } } export const _for_testing = { @@ -62,7 +67,7 @@ export const useFloatWidget = () => { max: inputSpec.max ?? 2048, round: enableRounding && precision && !inputSpec.round - ? (1_000_000 * Math.pow(0.1, precision)) / 1_000_000 + ? Math.pow(10, -precision) : (inputSpec.round as number), /** @deprecated Use step2 instead. The 10x value is a legacy implementation. */ step: step * 10.0,