Skip to content

Commit 6c25f50

Browse files
committed
Fix floating point errors for float settings
1 parent a63d0a1 commit 6c25f50

File tree

5 files changed

+16
-15
lines changed

5 files changed

+16
-15
lines changed

common/src/main/kotlin/com/lambda/graphics/animation/Animation.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ class Animation(initialValue: Double, val update: (Double) -> Double) {
5757

5858
fun AnimationTicker.exp(min: () -> Double, max: () -> Double, speed: () -> Double, flag: () -> Boolean) =
5959
Animation(min()) {
60-
val min = min()
61-
val max = max()
62-
val target = if (flag()) max else min
60+
val minVal = min()
61+
val maxVal = max()
62+
val target = if (flag()) maxVal else minVal
6363

64-
if (abs(target - it) < CLAMP * abs(max - min)) target
64+
if (abs(target - it) < CLAMP * abs(maxVal - minVal)) target
6565
else lerp(speed(), it, target)
6666
}.apply(::register)
6767

common/src/main/kotlin/com/lambda/gui/impl/clickgui/module/settings/NumberSlider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class NumberSlider <V> (
4242

4343
slider.onSlide {
4444
settingDelegate = settingDelegate.typeConvert(
45-
lerp(it, min, max).roundToStep(setting.step.toDouble())
45+
lerp(it, min, max).roundToStep(setting.step).toDouble()
4646
)
4747
}
4848
}

common/src/main/kotlin/com/lambda/gui/impl/clickgui/module/settings/SettingSlider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ abstract class SettingSlider <V : Any, T: AbstractSetting<V>>(
6161
}
6262

6363
textField {
64-
var lastValue = ""
64+
var lastValue: String
6565

6666
onUpdate {
6767
lastValue = text

common/src/main/kotlin/com/lambda/module/modules/client/GuiSettings.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ object GuiSettings : Module(
4848
val shadeBackground by setting("Shade Background", true, visibility = { page == Page.Colors })
4949
val colorWidth by setting("Shade Width", 200.0, 10.0..1000.0, 10.0, visibility = { page == Page.Colors })
5050
val colorHeight by setting("Shade Height", 200.0, 10.0..1000.0, 10.0, visibility = { page == Page.Colors })
51-
val colorSpeed by setting("Color Speed", 1.0, 0.1..10.0, 0.1, visibility = { page == Page.Colors })
51+
val colorSpeed by setting("Color Speed", 1.0, 0.1..5.0, 0.1, visibility = { page == Page.Colors })
5252

5353
val mainColor: Color get() = if (shade) Color.WHITE else primaryColor
5454

common/src/main/kotlin/com/lambda/util/math/MathUtils.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@ object MathUtils {
4040
fun Double.ceilToInt() = ceil(this).toInt()
4141

4242
fun <T : Number> T.roundToStep(step: T): T {
43-
val stepD = step.toDouble()
44-
if (stepD == 0.0) return this
45-
46-
var value = round(toDouble() / stepD) * stepD
47-
value = value.roundToPlaces(stepD.decimals)
48-
if (abs(value) == 0.0) value = 0.0
49-
50-
return typeConvert(value)
43+
val valueBD = BigDecimal(toString())
44+
val stepBD = BigDecimal(step.toString())
45+
if (stepBD.compareTo(BigDecimal.ZERO) == 0) return this
46+
val scaled = valueBD.divide(stepBD, stepBD.scale(), RoundingMode.HALF_UP)
47+
.setScale(0, RoundingMode.HALF_UP)
48+
.multiply(stepBD)
49+
.setScale(stepBD.scale(), RoundingMode.HALF_UP)
50+
51+
return typeConvert(scaled.toDouble())
5152
}
5253

5354
private fun Double.roundToPlaces(places: Int) =

0 commit comments

Comments
 (0)