Skip to content

Commit c7877db

Browse files
authored
fix(float-precision): correct float widget rounding (#4291)
Signed-off-by: Alexander Piskun <[email protected]>
1 parent 4cbcded commit c7877db

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/composables/widgets/useFloatWidget.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@ import { type ComfyWidgetConstructorV2 } from '@/scripts/widgets'
1010
import { useSettingStore } from '@/stores/settingStore'
1111

1212
function onFloatValueChange(this: INumericWidget, v: number) {
13-
this.value = this.options.round
14-
? _.clamp(
15-
Math.round((v + Number.EPSILON) / this.options.round) *
16-
this.options.round,
17-
this.options.min ?? -Infinity,
18-
this.options.max ?? Infinity
19-
)
20-
: v
13+
const round = this.options.round
14+
if (round) {
15+
const precision =
16+
this.options.precision ?? Math.max(0, -Math.floor(Math.log10(round)))
17+
const rounded = Math.round(v / round) * round
18+
this.value = _.clamp(
19+
Number(rounded.toFixed(precision)),
20+
this.options.min ?? -Infinity,
21+
this.options.max ?? Infinity
22+
)
23+
} else {
24+
this.value = v
25+
}
2126
}
2227

2328
export const _for_testing = {
@@ -62,7 +67,7 @@ export const useFloatWidget = () => {
6267
max: inputSpec.max ?? 2048,
6368
round:
6469
enableRounding && precision && !inputSpec.round
65-
? (1_000_000 * Math.pow(0.1, precision)) / 1_000_000
70+
? Math.pow(10, -precision)
6671
: (inputSpec.round as number),
6772
/** @deprecated Use step2 instead. The 10x value is a legacy implementation. */
6873
step: step * 10.0,

0 commit comments

Comments
 (0)