From 4f71319b757e98a0e4c91984acfc60b9453943af Mon Sep 17 00:00:00 2001 From: "Jonathan \"Duke\" Leto" Date: Tue, 26 Mar 2019 21:07:00 -0700 Subject: [PATCH] Do calculations in satoshis to avoid FP error --- contrib/hush_supply | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/contrib/hush_supply b/contrib/hush_supply index f85180814..a807cf3dd 100755 --- a/contrib/hush_supply +++ b/contrib/hush_supply @@ -2,25 +2,28 @@ use warnings; use strict; -my $supply = 0.0; -my $block = 0; -my $amount = 12.5; +my $supply = 0.0; +my $block = 0; +my $satoshis = 100_000_000; +my $amount = int(12.5*$satoshis); my $halvings = 0; -while ($supply <= 21_000_000) { -#while ($halvings <= 64 && $amount >= 1e-8) { +# Use this to calculate when supply hits a certain value +#while ($supply <= 21_000_000*$satoshis) { +# Use this to calculat when block rewards end +while ($halvings <= 64 && $amount >= 1) { $block++; if ($block < 5) { - $amount = 40_000; + $amount = 40_000 * $satoshis; } else { # Halving every 840000 blocks if ($block % 840_000 == 0) { $amount /= 2; $halvings++; } - $amount = 12.5 / (2**$halvings); + $amount = int(12.5*$satoshis) / (2**$halvings); } $supply += $amount; # block, current supply, block reward amount, number of halvings - print "$block,$supply,$amount,$halvings\n"; + printf "%s,%s,%s,%s\n", $block,$supply / $satoshis, $amount / $satoshis, $halvings; }