Add comments about floats

This commit is contained in:
James Cole 2022-10-16 14:44:11 +02:00
parent 39654249e4
commit e472c3520d
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
12 changed files with 36 additions and 32 deletions

View File

@ -127,11 +127,12 @@ class AccountController extends Controller
];
$currentStart = clone $start;
$range = app('steam')->balanceInRange($account, $start, clone $end);
$previous = round((float) array_values($range)[0], 12);
// 2022-10-11 this method no longer converts to float.
$previous = array_values($range)[0];
while ($currentStart <= $end) {
$format = $currentStart->format('Y-m-d');
$label = $currentStart->toAtomString();
$balance = array_key_exists($format, $range) ? round((float) $range[$format], 12) : $previous;
$balance = array_key_exists($format, $range) ? $range[$format] : $previous;
$previous = $balance;
$currentStart->addDay();
$currentSet['entries'][$label] = $balance;

View File

@ -94,7 +94,7 @@ class AccountController extends Controller
'id' => (string) $expense['id'],
'name' => $expense['name'],
'difference' => $expense['sum'],
'difference_float' => (float) $expense['sum'],
'difference_float' => (float) $expense['sum'], // float but on purpose.
'currency_id' => (string) $expense['currency_id'],
'currency_code' => $expense['currency_code'],
];
@ -126,7 +126,7 @@ class AccountController extends Controller
'id' => (string) $expense['id'],
'name' => $expense['name'],
'difference' => $expense['sum'],
'difference_float' => (float) $expense['sum'],
'difference_float' => (float) $expense['sum'], // float but on purpose.
'currency_id' => (string) $expense['currency_id'],
'currency_code' => $expense['currency_code'],
];

View File

@ -101,7 +101,7 @@ class BillController extends Controller
'currency_code' => $journal['currency_code'],
];
$response[$key]['difference'] = bcadd($response[$key]['difference'], $journal['amount']);
$response[$key]['difference_float'] = (float) $response[$key]['difference'];
$response[$key]['difference_float'] = (float) $response[$key]['difference']; // float but on purpose.
}
if (0 !== $foreignCurrencyId) {
$response[$foreignKey] = $response[$foreignKey] ?? [
@ -111,7 +111,7 @@ class BillController extends Controller
'currency_code' => $journal['foreign_currency_code'],
];
$response[$foreignKey]['difference'] = bcadd($response[$foreignKey]['difference'], $journal['foreign_amount']);
$response[$foreignKey]['difference_float'] = (float) $response[$foreignKey]['difference'];
$response[$foreignKey]['difference_float'] = (float) $response[$foreignKey]['difference']; // float but on purpose.
}
}
@ -154,7 +154,7 @@ class BillController extends Controller
'currency_code' => $journal['currency_code'],
];
$response[$currencyId]['difference'] = bcadd($response[$currencyId]['difference'], $journal['amount']);
$response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference'];
$response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference']; // float but on purpose.
}
if (0 !== $foreignCurrencyId) {
$response[$foreignCurrencyId] = $response[$foreignCurrencyId] ?? [
@ -164,7 +164,7 @@ class BillController extends Controller
'currency_code' => $journal['foreign_currency_code'],
];
$response[$foreignCurrencyId]['difference'] = bcadd($response[$foreignCurrencyId]['difference'], $journal['foreign_amount']);
$response[$foreignCurrencyId]['difference_float'] = (float) $response[$foreignCurrencyId]['difference'];
$response[$foreignCurrencyId]['difference_float'] = (float) $response[$foreignCurrencyId]['difference']; // float but on purpose.
}
}

View File

@ -91,7 +91,7 @@ class BudgetController extends Controller
'id' => (string) $budget->id,
'name' => $budget->name,
'difference' => $expense['sum'],
'difference_float' => (float) $expense['sum'],
'difference_float' => (float) $expense['sum'], // float but on purpose.
'currency_id' => (string) $expense['currency_id'],
'currency_code' => $expense['currency_code'],
];
@ -120,7 +120,7 @@ class BudgetController extends Controller
foreach ($expenses as $expense) {
$result[] = [
'difference' => $expense['sum'],
'difference_float' => (float) $expense['sum'],
'difference_float' => (float) $expense['sum'], // float but on purpose.
'currency_id' => (string) $expense['currency_id'],
'currency_code' => $expense['currency_code'],
];

View File

@ -92,7 +92,7 @@ class CategoryController extends Controller
'id' => (string) $category->id,
'name' => $category->name,
'difference' => $expense['sum'],
'difference_float' => (float) $expense['sum'],
'difference_float' => (float) $expense['sum'], // float but on purpose.
'currency_id' => (string) $expense['currency_id'],
'currency_code' => $expense['currency_code'],
];
@ -121,7 +121,7 @@ class CategoryController extends Controller
foreach ($expenses as $expense) {
$result[] = [
'difference' => $expense['sum'],
'difference_float' => (float) $expense['sum'],
'difference_float' => (float) $expense['sum'], // float but on purpose.
'currency_id' => (string) $expense['currency_id'],
'currency_code' => $expense['currency_code'],
];

View File

@ -66,7 +66,7 @@ class PeriodController extends Controller
'currency_code' => $journal['currency_code'],
];
$response[$currencyId]['difference'] = bcadd($response[$currencyId]['difference'], $journal['amount']);
$response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference'];
$response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference']; // float but on purpose.
}
if (0 !== $foreignCurrencyId) {
$response[$foreignCurrencyId] = $response[$foreignCurrencyId] ?? [
@ -76,7 +76,7 @@ class PeriodController extends Controller
'currency_code' => $journal['foreign_currency_code'],
];
$response[$foreignCurrencyId]['difference'] = bcadd($response[$foreignCurrencyId]['difference'], $journal['foreign_amount']);
$response[$foreignCurrencyId]['difference_float'] = (float) $response[$foreignCurrencyId]['difference'];
$response[$foreignCurrencyId]['difference_float'] = (float) $response[$foreignCurrencyId]['difference']; // float but on purpose.
}
}

View File

@ -90,7 +90,7 @@ class TagController extends Controller
'currency_code' => $journal['currency_code'],
];
$response[$currencyId]['difference'] = bcadd($response[$currencyId]['difference'], $journal['amount']);
$response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference'];
$response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference']; // float but on purpose.
}
if (0 !== $foreignCurrencyId) {
$response[$foreignCurrencyId] = $response[$foreignCurrencyId] ?? [
@ -100,7 +100,7 @@ class TagController extends Controller
'currency_code' => $journal['foreign_currency_code'],
];
$response[$foreignCurrencyId]['difference'] = bcadd($response[$foreignCurrencyId]['difference'], $journal['foreign_amount']);
$response[$foreignCurrencyId]['difference_float'] = (float) $response[$foreignCurrencyId]['difference'];
$response[$foreignCurrencyId]['difference_float'] = (float) $response[$foreignCurrencyId]['difference']; // float but on purpose.
}
}
@ -157,7 +157,7 @@ class TagController extends Controller
'currency_code' => $journal['currency_code'],
];
$response[$key]['difference'] = bcadd($response[$key]['difference'], $journal['amount']);
$response[$key]['difference_float'] = (float) $response[$key]['difference'];
$response[$key]['difference_float'] = (float) $response[$key]['difference']; // float but on purpose.
}
// on foreign ID
@ -169,7 +169,7 @@ class TagController extends Controller
'currency_code' => $journal['foreign_currency_code'],
];
$response[$foreignKey]['difference'] = bcadd($response[$foreignKey]['difference'], $journal['foreign_amount']);
$response[$foreignKey]['difference_float'] = (float) $response[$foreignKey]['difference'];
$response[$foreignKey]['difference_float'] = (float) $response[$foreignKey]['difference']; // float but on purpose.
}
}
}

View File

@ -93,7 +93,7 @@ class AccountController extends Controller
'id' => (string) $entry['id'],
'name' => $entry['name'],
'difference' => $entry['sum'],
'difference_float' => (float) $entry['sum'],
'difference_float' => (float) $entry['sum'], // float but on purpose.
'currency_id' => (string) $entry['currency_id'],
'currency_code' => $entry['currency_code'],
];
@ -125,7 +125,7 @@ class AccountController extends Controller
'id' => (string) $entry['id'],
'name' => $entry['name'],
'difference' => $entry['sum'],
'difference_float' => (float) $entry['sum'],
'difference_float' => (float) $entry['sum'], // float but on purpose.
'currency_id' => (string) $entry['currency_id'],
'currency_code' => $entry['currency_code'],
];

View File

@ -92,7 +92,7 @@ class CategoryController extends Controller
'id' => (string) $category->id,
'name' => $category->name,
'difference' => $expense['sum'],
'difference_float' => (float) $expense['sum'],
'difference_float' => (float) $expense['sum'], // float but on purpose.
'currency_id' => (string) $expense['currency_id'],
'currency_code' => $expense['currency_code'],
];
@ -121,7 +121,7 @@ class CategoryController extends Controller
foreach ($expenses as $expense) {
$result[] = [
'difference' => $expense['sum'],
'difference_float' => (float) $expense['sum'],
'difference_float' => (float) $expense['sum'], // float but on purpose.
'currency_id' => (string) $expense['currency_id'],
'currency_code' => $expense['currency_code'],
];

View File

@ -66,7 +66,7 @@ class PeriodController extends Controller
'currency_code' => $journal['currency_code'],
];
$response[$currencyId]['difference'] = bcadd($response[$currencyId]['difference'], app('steam')->positive($journal['amount']));
$response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference'];
$response[$currencyId]['difference_float'] = (float) $response[$currencyId]['difference']; // float but on purpose.
}
if (0 !== $foreignCurrencyId) {
$response[$foreignCurrencyId] = $response[$foreignCurrencyId] ?? [
@ -78,7 +78,7 @@ class PeriodController extends Controller
$response[$foreignCurrencyId]['difference'] = bcadd(
$response[$foreignCurrencyId]['difference'], app('steam')->positive($journal['foreign_amount'])
);
$response[$foreignCurrencyId]['difference_float'] = (float) $response[$foreignCurrencyId]['difference'];
$response[$foreignCurrencyId]['difference_float'] = (float) $response[$foreignCurrencyId]['difference']; // float but on purpose.
}
}

View File

@ -102,11 +102,14 @@ class AccountController extends Controller
];
$currentStart = clone $start;
$range = app('steam')->balanceInRange($account, $start, clone $end);
$previous = round((float) array_values($range)[0], 12);
// 2022-10-11: this method no longer converts to floats
$previous = array_values($range)[0];
while ($currentStart <= $end) {
$format = $currentStart->format('Y-m-d');
$label = $currentStart->toAtomString();
$balance = array_key_exists($format, $range) ? round((float) $range[$format], 12) : $previous;
$balance = array_key_exists($format, $range) ? $range[$format] : $previous;
$previous = $balance;
$currentStart->addDay();
$currentSet['entries'][$label] = $balance;

View File

@ -76,7 +76,7 @@ class NetWorth implements NetWorthInterface
$netWorth = [];
$result = [];
//Log::debug(sprintf('Now in getNetWorthByCurrency(%s)', $date->format('Y-m-d')));
Log::debug(sprintf('Now in getNetWorthByCurrency(%s)', $date->format('Y-m-d')));
// get default currency
$default = app('amount')->getDefaultCurrencyByUser($this->user);
@ -87,16 +87,16 @@ class NetWorth implements NetWorthInterface
// get the preferred currency for this account
/** @var Account $account */
foreach ($accounts as $account) {
//Log::debug(sprintf('Now at account #%d: "%s"', $account->id, $account->name));
Log::debug(sprintf('Now at account #%d: "%s"', $account->id, $account->name));
$currencyId = (int) $this->accountRepository->getMetaValue($account, 'currency_id');
$currencyId = 0 === $currencyId ? $default->id : $currencyId;
//Log::debug(sprintf('Currency ID is #%d', $currencyId));
Log::debug(sprintf('Currency ID is #%d', $currencyId));
// balance in array:
$balance = $balances[$account->id] ?? '0';
//Log::debug(sprintf('Balance is %s', $balance));
Log::debug(sprintf('Balance for %s is %s', $date->format('Y-m-d'), $balance));
// always subtract virtual balance.
$virtualBalance = (string) $account->virtual_balance;
@ -104,14 +104,14 @@ class NetWorth implements NetWorthInterface
$balance = bcsub($balance, $virtualBalance);
}
//Log::debug(sprintf('Balance corrected to %s because of virtual balance (%s)', $balance, $virtualBalance));
Log::debug(sprintf('Balance corrected to %s because of virtual balance (%s)', $balance, $virtualBalance));
if (!array_key_exists($currencyId, $netWorth)) {
$netWorth[$currencyId] = '0';
}
$netWorth[$currencyId] = bcadd($balance, $netWorth[$currencyId]);
//Log::debug(sprintf('Total net worth for currency #%d is %s', $currencyId, $netWorth[$currencyId]));
Log::debug(sprintf('Total net worth for currency #%d is %s', $currencyId, $netWorth[$currencyId]));
}
ksort($netWorth);