(by Codex) Widen daily interest calculations before rate division
This commit is contained in:
1 parent
70082ef825
commit
d57b6b5a96
6 files changed
+104
-25
No files matched your search
@@ -1161,3 +1161,7 @@ endif()
|
||||
add_executable(desktop_feedback_tests tests/desktop_feedback_tests.cpp)
|
||||
target_link_libraries(desktop_feedback_tests PRIVATE bmhui)
|
||||
bmh_add_data_test(NAME desktop_feedback_tests COMMAND desktop_feedback_tests "${BMH_DATA}")
|
||||
|
||||
add_executable(daily_interest_unit_tests tests/daily_interest_unit_tests.cpp)
|
||||
target_link_libraries(daily_interest_unit_tests PRIVATE bmhcore)
|
||||
add_test(NAME daily_interest_unit_tests COMMAND daily_interest_unit_tests)
|
||||
@@ -68,3 +68,11 @@ for8k and37weeks for18k. That was original data, not a mistaken timer. Expanded
|
||||
limits/time policy is an intentional improvement. The UI now validates quotes
|
||||
against the actual edition and this shared policy rather than hardcoded Diamond
|
||||
prices and sizes. Existing eight capacity/construction bytes and saves are retained.
|
||||
## Daily interest at large native balances
|
||||
|
||||
OpenBMH deviates from the original: daily debt and deposit interest uses 64-bit
|
||||
intermediate arithmetic before the original integer divisions. The balance-plan
|
||||
preview uses the same calculation. Original rates, division order and random
|
||||
consumption remain unchanged. Account balances and accumulated statistics retain
|
||||
the native 32-bit save representation; this change does not add unlimited account
|
||||
balances or change the original high-cash deduction policy.
|
||||
@@ -56,19 +56,9 @@ uint32_t facility_level(const GameState& g, int m, int k) {
|
||||
// (balance 1 %, fixed deposit 2 % a year), positive an expense (overdraft 12 %, beyond the
|
||||
// credit line 18 %). The divisor is -36500.
|
||||
int32_t daily_interest_preview(const GameState& g, int m) {
|
||||
constexpr int32_t kAnnual = -0x8e94;
|
||||
const int32_t balance = rec32(g, m, 0x314), deposit = rec32(g, m, 0x318), credit = rec32(g, m, 0x31c);
|
||||
int32_t result = 0;
|
||||
if (balance < 0) {
|
||||
const int32_t with_credit = wrap(int64_t(balance) + credit);
|
||||
if (with_credit < 0) result = wrap(int64_t(with_credit) * 0x12) / kAnnual;
|
||||
int32_t below = wrap(-int64_t(credit));
|
||||
if (below < balance) below = balance;
|
||||
result = wrap(int64_t(result) + wrap(int64_t(below) * 0xc) / kAnnual);
|
||||
} else {
|
||||
result = balance / kAnnual;
|
||||
}
|
||||
return wrap(int64_t(result) + wrap(int64_t(deposit / 10000) * 20000) / kAnnual);
|
||||
const auto interest = daily_interest_amounts(balance, deposit, credit);
|
||||
return interest.borrowing + interest.cash_and_deposit;
|
||||
}
|
||||
|
||||
// SharesAndLoans(manager, flags, months): flags 1 the dividends of the manager's funds and
|
||||
|
||||
+27
-13
@@ -8,6 +8,7 @@
|
||||
#include "game/schedule.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <bit>
|
||||
|
||||
namespace bmh {
|
||||
|
||||
@@ -24,6 +25,23 @@ void wr32(std::vector<uint8_t>& v, size_t o, int32_t x) {
|
||||
|
||||
} // namespace
|
||||
|
||||
DailyInterestAmounts daily_interest_amounts(int32_t cash, int32_t deposit, int32_t credit) {
|
||||
// OpenBMH deviates from the original: widen products before annual-rate
|
||||
// division so valid native account amounts do not overflow intermediate sums.
|
||||
// Preserve truncation at each original division, including deposit / 10000.
|
||||
DailyInterestAmounts result;
|
||||
if (cash < 0) {
|
||||
const int64_t with_credit = int64_t(cash) + credit;
|
||||
int64_t borrowing = with_credit < 0 ? with_credit * 18 / -36500 : 0;
|
||||
borrowing += std::max(int64_t(cash), -int64_t(credit)) * 12 / -36500;
|
||||
result.borrowing = int32_t(borrowing);
|
||||
} else {
|
||||
result.cash_and_deposit = cash / -36500;
|
||||
}
|
||||
result.cash_and_deposit += int32_t(int64_t(deposit / 10000) * 20000 / -36500);
|
||||
return result;
|
||||
}
|
||||
|
||||
void daily_interest(GameState& g, WatcomRandom& rand) {
|
||||
// (DailyInterest before the day step: the training camp visits of today are over)
|
||||
for (size_t m = 0; m < g.manager_teams.size(); m++) g.manager_records.at(m * kManagerRecord + 0x21f) &= 0x7f;
|
||||
@@ -32,24 +50,20 @@ void daily_interest(GameState& g, WatcomRandom& rand) {
|
||||
const int32_t balance = rd32(g.manager_records, rec + 0x314);
|
||||
const int32_t deposit = rd32(g.manager_records, rec + 0x318);
|
||||
const int32_t credit = rd32(g.manager_records, rec + 0x31c);
|
||||
int32_t amount = 0; // positive = cost
|
||||
const auto interest = daily_interest_amounts(balance, deposit, credit);
|
||||
if (balance < 0) {
|
||||
if (balance + credit < 0) amount = ((balance + credit) * 18) / -36500;
|
||||
int32_t used = -credit < balance ? balance : -credit;
|
||||
amount += (used * 12) / -36500;
|
||||
wr32(g.manager_records, rec + 0x89c, rd32(g.manager_records, rec + 0x89c) + amount);
|
||||
book_money(g, k, amount, true);
|
||||
amount = 0;
|
||||
} else {
|
||||
amount = balance / -36500;
|
||||
const auto statistic = uint32_t(rd32(g.manager_records, rec + 0x89c)) + uint32_t(interest.borrowing);
|
||||
wr32(g.manager_records, rec + 0x89c, std::bit_cast<int32_t>(statistic));
|
||||
book_money(g, k, interest.borrowing, true);
|
||||
}
|
||||
amount += ((deposit / 10000) * 20000) / -36500;
|
||||
wr32(g.manager_records, rec + 0x870, rd32(g.manager_records, rec + 0x870) - amount);
|
||||
book_money(g, k, amount, true);
|
||||
const auto statistic = uint32_t(rd32(g.manager_records, rec + 0x870)) - uint32_t(interest.cash_and_deposit);
|
||||
wr32(g.manager_records, rec + 0x870, std::bit_cast<int32_t>(statistic));
|
||||
book_money(g, k, interest.cash_and_deposit, true);
|
||||
if (rd32(g.manager_records, rec + 0x314) > 100000000) { // read again after the bookings
|
||||
int32_t extra = int32_t(rand.range(10000, 20000)) * 1000;
|
||||
// BookMoney2 with the statistics flag set: counts as an expense (+0x8a0)
|
||||
wr32(g.manager_records, rec + 0x8a0, rd32(g.manager_records, rec + 0x8a0) + extra);
|
||||
const auto expense = uint32_t(rd32(g.manager_records, rec + 0x8a0)) + uint32_t(extra);
|
||||
wr32(g.manager_records, rec + 0x8a0, std::bit_cast<int32_t>(expense));
|
||||
book_money(g, k, extra, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,12 @@ namespace bmh {
|
||||
// cleared for every manager.
|
||||
void daily_interest(GameState& g, WatcomRandom& rand);
|
||||
|
||||
struct DailyInterestAmounts {
|
||||
int32_t borrowing = 0;
|
||||
int32_t cash_and_deposit = 0; // Negative amounts are interest income.
|
||||
};
|
||||
DailyInterestAmounts daily_interest_amounts(int32_t cash, int32_t deposit, int32_t credit);
|
||||
|
||||
} // namespace bmh
|
||||
|
||||
namespace bmh {
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "game/week.hpp"
|
||||
#include <bit>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace bmh;
|
||||
namespace {
|
||||
void check(bool ok,const char* message){if(!ok)throw std::runtime_error(message);}
|
||||
void write(GameState& state,size_t at,int32_t value) {
|
||||
for(int byte=0;byte<4;++byte)state.manager_records.at(at+byte)=uint8_t(uint32_t(value)>>(byte*8));
|
||||
}
|
||||
int32_t read(const GameState& state,size_t at) {
|
||||
uint32_t value=0;
|
||||
for(int byte=0;byte<4;++byte)value|=uint32_t(state.manager_records.at(at+byte))<<(byte*8);
|
||||
return std::bit_cast<int32_t>(value);
|
||||
}
|
||||
}
|
||||
int main()try {
|
||||
check(daily_interest_amounts(0,1500000000,0).cash_and_deposit==-82191,
|
||||
"large deposits reverse interest because of an intermediate product overflow");
|
||||
check(daily_interest_amounts(-2000000000,0,1000000000).borrowing==821917,
|
||||
"large debt loses its annual-rate charges before division");
|
||||
check(daily_interest_amounts(-1800000000,0,1800000000).borrowing==591780,
|
||||
"credit-line interest overflows before division");
|
||||
check(daily_interest_amounts(0,std::numeric_limits<int32_t>::max(),0).cash_and_deposit==-117670,
|
||||
"maximum native deposit interest is not computed with a wide product");
|
||||
check(daily_interest_amounts(std::numeric_limits<int32_t>::min(),0,std::numeric_limits<int32_t>::max()).borrowing==706022,
|
||||
"native debt/credit boundary changed the division order");
|
||||
for(int32_t cash:{-1000000,-1,0,1,1000000})
|
||||
for(int32_t deposit:{0,9999,10000,1000000000})
|
||||
for(int32_t credit:{0,100000,1000000}) {
|
||||
int32_t expected=cash>=0?cash/-36500:0;
|
||||
if(cash<0) {
|
||||
if(cash+credit<0)expected=(cash+credit)*18/-36500;
|
||||
expected+=(cash>-credit?cash:-credit)*12/-36500;
|
||||
}
|
||||
expected+=((deposit/10000)*20000)/-36500;
|
||||
const auto interest=daily_interest_amounts(cash,deposit,credit);
|
||||
check(interest.borrowing+interest.cash_and_deposit==expected,
|
||||
"ordinary balances changed their original truncation or rate split");
|
||||
}
|
||||
GameState state;state.level=1;state.manager_teams={0};state.manager_records.resize(5*0xb0c);
|
||||
write(state,0x318,1500000000);
|
||||
WatcomRandom random(713);const auto seed=random.seed();const auto calls=random.calls();
|
||||
daily_interest(state,random);
|
||||
check(read(state,0x314)==82191&&read(state,0x870)==82191&&read(state,0x318)==1500000000,
|
||||
"daily booking and income statistics disagree with large-deposit interest");
|
||||
check(random.seed()==seed&&random.calls()==calls,"interest arithmetic consumed extra randomness");
|
||||
write(state,0x314,-2000000000);write(state,0x318,0);write(state,0x31c,1000000000);
|
||||
daily_interest(state,random);
|
||||
check(read(state,0x314)==-2000821917&&read(state,0x89c)==821917,
|
||||
"large-debt booking and expense statistics disagree with the wide calculation");
|
||||
std::cout<<"Wide daily interest, native boundary amounts, ordinary-rate parity and actual booking passed\n";
|
||||
return 0;
|
||||
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}
|
||||
Reference in new issue
Block a user