Compare commits

...

2 Commits

Author SHA1 Message Date
446c8d9a6e minor additions 2026-04-01 02:51:00 +03:00
c140567e0c made last changes KMP compliant 2026-04-01 02:51:00 +03:00
4 changed files with 101 additions and 88 deletions

View File

@ -1,28 +1,13 @@
/*
Рассчитывает глубину провала по времени падения камня и прихода звука.
@param T измеренное полное время (с)
@param m масса камня (кг)
@param d диаметр камня (м) (предполагается сферическая форма)
@param rho плотность воздуха (кг/м³), по умолчанию 1.2
@param c скорость звука (м/с), по умолчанию 340.0
@param g ускорение свободного падения (м/с²), по умолчанию 9.81
@param Cd коэффициент лобового сопротивления, по умолчанию 0.5
@param epsilon относительная точность (м), по умолчанию 1e-3
@param maxIter максимальное число итераций, по умолчанию 20
@return глубина h (м), или null если расчёт не сошёлся
*/
fun calculateDepth( fun calculateDepth(
T: Real, T: Real,
m: Real, m: Real,
d: Real, d: Real,
rho: Real = 1.2, rho: Real = 1.2,
c: Real = 340.0, c: Real = 340.0,
g: Real = 9.81, g: Real = 9.81,
Cd: Real = 0.5, Cd: Real = 0.5,
epsilon: Real = 1e-3, eps: Real = 1e-3,
maxIter: Int = 20 maxIter: Int = 100
): Real? { ): Real? {
// Площадь миделя // Площадь миделя
val r = d / 2.0 val r = d / 2.0
@ -36,69 +21,56 @@ fun calculateDepth(
// Функция времени падения с высоты h // Функция времени падения с высоты h
fun tFall(h: Real): Real { fun tFall(h: Real): Real {
// Для численной стабильности при больших h используем логарифмическую форму
val arg = exp(g * h / (vTerm * vTerm)) val arg = exp(g * h / (vTerm * vTerm))
// arcosh(x) = ln(x + sqrt(x^2 - 1)) // arcosh(x) = ln(x + sqrt(x^2 - 1))
val arcosh = ln(arg + sqrt(arg * arg - 1.0)) val arcosh = ln(arg + sqrt(arg * arg - 1.0))
return vTerm / g * arcosh return vTerm / g * arcosh
} }
// Производная времени падения по h // Полное расчётное время
fun dtFall_dh(h: Real): Real {
val expArg = exp(2.0 * g * h / (vTerm * vTerm))
return 1.0 / (vTerm * sqrt(expArg - 1.0))
}
// Полное расчётное время T_calc(h) = tFall(h) + h/c
fun Tcalc(h: Real): Real = tFall(h) + h / c fun Tcalc(h: Real): Real = tFall(h) + h / c
// Производная T_calc по h // Находим интервал, содержащий корень
fun dTcalc_dh(h: Real): Real = dtFall_dh(h) + 1.0 / c // Нижняя граница: глубина не может быть отрицательной
var lo = 0.0
// Начальное приближение (без сопротивления) // Верхняя граница: сначала попробуем оценку по свободному падению (без звука)
val term = 1.0 + g * T / c var hi = 0.5 * g * T * T // максимальная глубина, если бы не было сопротивления и звука
val sqrtTerm = sqrt(1.0 + 2.0 * g * T / c) // Уточним hi, чтобы Tcalc(hi) было заведомо больше T
var h = (c * c / g) * (term - sqrtTerm) while (Tcalc(hi) < T && hi < 1e4) {
hi *= 2.0
// Проверка на валидность начального приближения
if (h.isNaN() || h <= 0.0) {
// Если формула дала некорректный результат, используем оценку по свободному падению
h = 0.5 * g * T * T // грубая оценка, всё равно будет уточняться
if (h.isNaN() || h <= 0.0) return null
} }
// Проверка, что hi достаточно велико
if (Tcalc(hi) < T) return null // слишком большая глубина, не укладываемся в разумное
// Итерации Ньютона // Бисекция
var iter = 0 var iter = 0
while (iter < maxIter) { var h = (lo + hi) / 2.0
while (iter < maxIter && (hi - lo) > eps) {
val f = Tcalc(h) - T val f = Tcalc(h) - T
val df = dTcalc_dh(h) if (abs(f) < eps) break
if (f > 0) {
// Если производная близка к нулю, выходим hi = h
if (abs(df) < 1e-12) return null } else {
lo = h
val hNew = h - f / df
// Проверка сходимости
if (abs(hNew - h) < epsilon) {
return hNew
} }
h = (lo + hi) / 2.0
h = hNew
iter++ iter++
println("iter: $iter: $h")
} }
// Не сошлось за maxIter return h
return null
} }
// Пример использования // Пример: T=12 секунд
val T = 6.0 // секунды val T = 26.0
val m = 1.0 // кг val m = 1.0 // кг
val d = 0.1 // м (10 см) val d = 0.1 // м
val depth = calculateDepth(T, m, d) val depth = calculateDepth(T, m, d)
if (depth != null) { if (depth != null) {
println("Глубина: %.2f м"(depth)) println("Глубина: %.2f м"(depth))
} else { // Для проверки выведем теоретическое время при найденной глубине
println("Расчёт не сошёлся") // (можно добавить функцию для самопроверки)
} } else {
println("Расчёт не сошёлся")
}

View File

@ -436,6 +436,12 @@ class Compiler(
} }
} }
private fun rememberModuleReferencePos(name: String, pos: Pos) {
if (!moduleReferencePosByName.containsKey(name)) {
moduleReferencePosByName[name] = pos
}
}
private fun predeclareClassMembers(target: MutableSet<String>, overrides: MutableMap<String, Boolean>) { private fun predeclareClassMembers(target: MutableSet<String>, overrides: MutableMap<String, Boolean>) {
val saved = cc.savePos() val saved = cc.savePos()
var depth = 0 var depth = 0
@ -948,7 +954,7 @@ class Compiler(
} }
} }
captureLocalRef(name, slotLoc, pos)?.let { ref -> captureLocalRef(name, slotLoc, pos)?.let { ref ->
moduleReferencePosByName.putIfAbsent(name, pos) rememberModuleReferencePos(name, pos)
resolutionSink?.reference(name, pos) resolutionSink?.reference(name, pos)
return ref return ref
} }
@ -1042,7 +1048,7 @@ class Compiler(
moduleEntry.isDelegated moduleEntry.isDelegated
) )
captureLocalRef(name, moduleLoc, pos)?.let { ref -> captureLocalRef(name, moduleLoc, pos)?.let { ref ->
moduleReferencePosByName.putIfAbsent(name, pos) rememberModuleReferencePos(name, pos)
resolutionSink?.reference(name, pos) resolutionSink?.reference(name, pos)
return ref return ref
} }
@ -1069,7 +1075,7 @@ class Compiler(
strictSlotRefs strictSlotRefs
) )
} }
moduleReferencePosByName.putIfAbsent(name, pos) rememberModuleReferencePos(name, pos)
resolutionSink?.reference(name, pos) resolutionSink?.reference(name, pos)
return ref return ref
} }
@ -1100,14 +1106,14 @@ class Compiler(
) )
} }
} }
registerImportBinding(name, resolved.binding, pos) registerImportBinding(name, resolved.binding, pos)
val slot = lookupSlotLocation(name) val slot = lookupSlotLocation(name)
if (slot != null) { if (slot != null) {
captureLocalRef(name, slot, pos)?.let { ref -> captureLocalRef(name, slot, pos)?.let { ref ->
moduleReferencePosByName.putIfAbsent(name, pos) rememberModuleReferencePos(name, pos)
resolutionSink?.reference(name, pos) resolutionSink?.reference(name, pos)
return ref return ref
} }
val ref = if (!useScopeSlots && capturePlanStack.isEmpty() && slot.depth > 0) { val ref = if (!useScopeSlots && capturePlanStack.isEmpty() && slot.depth > 0) {
LocalSlotRef( LocalSlotRef(
name, name,
@ -1131,7 +1137,7 @@ class Compiler(
strictSlotRefs strictSlotRefs
) )
} }
moduleReferencePosByName.putIfAbsent(name, pos) rememberModuleReferencePos(name, pos)
resolutionSink?.reference(name, pos) resolutionSink?.reference(name, pos)
return ref return ref
} }

View File

@ -7664,7 +7664,9 @@ class BytecodeCompiler(
private fun noteScopeSlotRef(slot: Int, pos: Pos) { private fun noteScopeSlotRef(slot: Int, pos: Pos) {
if (slot >= scopeSlotCount) return if (slot >= scopeSlotCount) return
val key = scopeKeyByIndex.getOrNull(slot) ?: return val key = scopeKeyByIndex.getOrNull(slot) ?: return
scopeSlotRefPosByKey.putIfAbsent(key, pos) if (!scopeSlotRefPosByKey.containsKey(key)) {
scopeSlotRefPosByKey[key] = pos
}
} }
private fun resolveSlot(ref: LocalSlotRef): Int? { private fun resolveSlot(ref: LocalSlotRef): Int? {
@ -7675,11 +7677,15 @@ class BytecodeCompiler(
val localIndex = localSlotIndexByKey[key] val localIndex = localSlotIndexByKey[key]
if (localIndex != null) return scopeSlotCount + localIndex if (localIndex != null) return scopeSlotCount + localIndex
scopeSlotMap[key]?.let { scopeSlotMap[key]?.let {
scopeSlotRefPosByKey.putIfAbsent(key, ref.pos()) if (!scopeSlotRefPosByKey.containsKey(key)) {
scopeSlotRefPosByKey[key] = ref.pos()
}
return it return it
} }
scopeSlotIndexByName[ref.name]?.let { scopeSlotIndexByName[ref.name]?.let {
scopeSlotRefPosByKey.putIfAbsent(key, ref.pos()) if (!scopeSlotRefPosByKey.containsKey(key)) {
scopeSlotRefPosByKey[key] = ref.pos()
}
return it return it
} }
} }
@ -7693,7 +7699,9 @@ class BytecodeCompiler(
} }
val resolved = scopeSlotMap[scopeKey] val resolved = scopeSlotMap[scopeKey]
if (resolved != null) { if (resolved != null) {
scopeSlotRefPosByKey.putIfAbsent(scopeKey, ref.pos()) if (!scopeSlotRefPosByKey.containsKey(scopeKey)) {
scopeSlotRefPosByKey[scopeKey] = ref.pos()
}
} }
return resolved return resolved
} }
@ -7708,7 +7716,9 @@ class BytecodeCompiler(
val scopeKey = ScopeSlotKey(refScopeId(ref), refSlot(ref)) val scopeKey = ScopeSlotKey(refScopeId(ref), refSlot(ref))
val resolved = scopeSlotMap[scopeKey] val resolved = scopeSlotMap[scopeKey]
if (resolved != null) { if (resolved != null) {
scopeSlotRefPosByKey.putIfAbsent(scopeKey, ref.pos()) if (!scopeSlotRefPosByKey.containsKey(scopeKey)) {
scopeSlotRefPosByKey[scopeKey] = ref.pos()
}
} }
return resolved return resolved
} }

View File

@ -284,6 +284,31 @@ class DecimalModuleTest {
) )
} }
@Test
fun testDecimalTruncateToTwoFractionDigitsViaGlobalRound() = runTest {
val scope = Script.newScope()
scope.eval(
"""
import lyng.decimal
fun trunc2(x: Decimal): Decimal {
val scaled = x * 100.d
val whole = if (scaled >= 0.d) {
floor(scaled) as Decimal
} else {
ceil(scaled) as Decimal
}
whole / 100.d
}
assertEquals("12.34", trunc2("12.349".d).toStringExpanded())
assertEquals("12.34", trunc2("12.340".d).toStringExpanded())
assertEquals("-12.34", trunc2("-12.349".d).toStringExpanded())
assertEquals("-12.34", trunc2("-12.340".d).toStringExpanded())
""".trimIndent()
)
}
@Test @Test
fun testDecimalMathHelpersFallbackThroughRealTemporarily() = runTest { fun testDecimalMathHelpersFallbackThroughRealTemporarily() = runTest {
val scope = Script.newScope() val scope = Script.newScope()