Forum Home
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Popular

    \[fun\] 500FTC BOUNTY - NOW CLAIMED! Not so simple, simple math...

    Support
    6
    22
    5368
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • U
      UKMark last edited by

      I have been pulling my hair out for the past day on how to solve (what should be simple math)…I am currently creating a service that will charge a set 1% fee…I want to display the users current balance and their maximum spendable balance that takes into account this fee…Seems easy enough…lets see.

      Example (ignoring any potential TX fee for now)
      Fee = 0.01
      Balance = 100
      100 * 0.01 = 1
      100 - 1 = 99 (max spendable balance?) Wrong!

      User then sends their displayed max spendable balance but the fee is charged on the amount entered, so…

      99 * 0.01 = 0.99
      99 + 0.99 = 99.99
      100 - 99.99 = 0.01 (this amount will be their remaining balance, so the maximum spendable balance displayed was actually wrong)

      I have tried adding this difference back onto the displayed maximum balance and while close is still not correct.

      So how would I display their [i]exact[/i] max spendable balance, a figure that when sent will satisfy every one of the following cases: eg, Pay 1% fee, pay tx fee, send amount entered, and reduce balance to [i]exactly[/i] 0?

      There must be a way, but can I for the life of me figure out how…

      I will pay a 500FTC bounty for the first person that can solve this problem (if at all solvable) and post me a working example of the math involved below.

      1 Reply Last reply Reply Quote 1
      • M
        mnstrcck last edited by

        iawgoM basically has it.

        But you will need to round up/down to, say the second decimal point [most people are used to 99.99 not 99.999]. You can also simplify the math by using a fee that’s something like 1.0001% - it’ll fudge the numbers to make it easier to round up/down etc.

        So:

        100 * .010001 = 1.0001
        Available Balance 98.9998 - Rounded up is 99

        98.9998 * 1.010001 = 99.989897
        Rounded up it’s 100

        Let’s try another amount:

        46.37 * .010001 = 0.46374637 - Rounded up is 0.46
        Available 45.91
        45.91 * 1.010001 = 46.3691459 - Rounded up is 46.37

        One other thing, no one will ever notice the .0001% because you’re rounding up/down to a decimal place before it, so it always evens out. It’l like the leave a penny/take a penny jar at the convenience store [or pence in the UK I guess].

        1 Reply Last reply Reply Quote 0
        • kris_davison
          kris_davison last edited by

          Wait a min this really shouldn’t be that hard. I think the issue is you are trying to get one percent of 100% the first time then you expect one percent of the 99% to equal that first one percent but it does not as you have demonstrated.

          Starting balance (a)= 100
          Fee Percent (b) = 1%
          Fee amount ©= (a) divided by 100 x by (b) = 1
          Spendable balance (d) = (a) divided by 100 x by (100-(b)) = 99
          Now when u are trying to find the fee from the spendable balance.
          Fee amount2 (e) = (d) divided by (100 - (b)) = 1

          This is because the spendable amount should be seen as 99% of the full amount and to find 1% you simply divide by 99.

          1 Reply Last reply Reply Quote 0
          • S
            spynappels last edited by

            It’s actually probably a little easier still.

            The percentage fee is of the spend amount, so starting amount is 100% of spend amount plus fee percentage.

            If fee is 1%, starting amount is 101% of spend amount.

            Therefore spend amount = start amount / 1.01

            Adjust the divisor depending on the fee percentage, for 5% spend amount = start amount / 1.05

            1 Reply Last reply Reply Quote 0
            • kris_davison
              kris_davison last edited by

              Again im not sure this is right as 100 starting amount with a 1% fee in you calculation would end up something like what iawgoM had said.

              100 / 101 * 1 = 0.9900990099009901 ( and not 1 as it should be)

              1 Reply Last reply Reply Quote 0
              • S
                spynappels last edited by

                [quote name=“kris_davison” post=“30530” timestamp=“1381088702”]
                Again im not sure this is right as 100 starting amount with a 1% fee in you calculation would end up something like what iawgoM had said.

                100 / 101 * 1 = 0.9900990099009901 ( and not 1 as it should be)
                [/quote]

                I’m sorry, it should not be. The 1% is not of the starting amount but of the spend amount.

                if the starting amount is 100FTC, the spendable amount is 99.00990099FTC (to 8 dec pl) and the fee is 1% of this, which when added together will add up to 100FTC (near as dammit)

                So iawgo was right, I just got there in a slightly simpler way.

                1 Reply Last reply Reply Quote 0
                • M
                  mnstrcck last edited by

                  [quote name=“iawgoM” post=“30533” timestamp=“1381089435”]
                  [quote author=spynappels link=topic=3929.msg30531#msg30531 date=1381089153]
                  iawgo
                  [/quote]

                  Mogwai, if that’s easier for you mate ;D
                  [/quote]

                  It’s a hard one ;D I messed it up too. But I imagine so does everyone with mine.

                  1 Reply Last reply Reply Quote 0
                  • R
                    Ruthie last edited by

                    [quote name=“mnstrcck” post=“30534” timestamp=“1381089739”]
                    [quote author=iawgoM link=topic=3929.msg30533#msg30533 date=1381089435]
                    [quote author=spynappels link=topic=3929.msg30531#msg30531 date=1381089153]
                    iawgo
                    [/quote]

                    Mogwai, if that’s easier for you mate ;D
                    [/quote]

                    It’s a hard one ;D I messed it up too. But I imagine so does everyone with mine.
                    [/quote]

                    Off topic… I knew Mog’s and I can’t forget yours Montster, thanks to your little clue on another thread… - btw thanks for that :o

                    1 Reply Last reply Reply Quote 0
                    • M
                      mnstrcck last edited by

                      [quote name=“Ruthie” post=“30535” timestamp=“1381090135”]
                      Off topic… I knew Mog’s and I can’t forget yours Montster, thanks to your little clue on another thread… - btw thanks for that :o
                      [/quote]

                      :-\

                      It’s my long ago single-use DJ name, when dropping vowels became cool. I used it for one event. But, well, outside of that I’m rather quite average. Imagine me saying that in David Mitchell’s dry tone.

                      1 Reply Last reply Reply Quote 0
                      • R
                        Ruthie last edited by

                        [quote name=“mnstrcck” post=“30536” timestamp=“1381090384”]
                        [quote author=Ruthie link=topic=3929.msg30535#msg30535 date=1381090135]
                        Off topic… I knew Mog’s and I can’t forget yours Montster, thanks to your little clue on another thread… - btw thanks for that :o
                        [/quote]

                        :-\

                        It’s my long ago single-use DJ name, when dropping vowels became cool. I used it for one event. But, well, outside of that I’m rather quite average. Imagine me saying that in David Mitchell’s dry tone.
                        [/quote]

                        Great topic change…(ignoring the bits about average :o) now just remembering the laughs that David Mitchell gives me on Radio 4 and Peep Show :)

                        1 Reply Last reply Reply Quote 0
                        • M
                          mnstrcck last edited by

                          [quote name=“Ruthie” post=“30538” timestamp=“1381090581”]
                          Great topic change…(ignoring the bits about average :o) now just remembering the laughs that David Mitchell gives me on Radio 4 and Peep Show :)
                          [/quote]

                          Averages = Math = Original Topic. ; ;)

                          1 Reply Last reply Reply Quote 0
                          • R
                            Ruthie last edited by

                            [quote name=“mnstrcck” post=“30539” timestamp=“1381090833”]
                            [quote author=Ruthie link=topic=3929.msg30538#msg30538 date=1381090581]
                            Great topic change…(ignoring the bits about average :o) now just remembering the laughs that David Mitchell gives me on Radio 4 and Peep Show :)
                            [/quote]

                            Averages = Math = Original Topic. ; ;)
                            [/quote]

                            I’ll go with that :)

                            1 Reply Last reply Reply Quote 0
                            • kris_davison
                              kris_davison last edited by

                              [quote name=“spynappels” post=“30531” timestamp=“1381089153”]
                              [quote author=kris_davison link=topic=3929.msg30530#msg30530 date=1381088702]
                              Again im not sure this is right as 100 starting amount with a 1% fee in you calculation would end up something like what iawgoM had said.

                              100 / 101 * 1 = 0.9900990099009901 ( and not 1 as it should be)
                              [/quote]

                              I’m sorry, it should not be. The 1% is not of the starting amount but of the spend amount.

                              if the starting amount is 100FTC, the spendable amount is 99.00990099FTC (to 8 dec pl) and the fee is 1% of this, which when added together will add up to 100FTC (near as dammit)

                              So iawgo was right, I just got there in a slightly simpler way.
                              [/quote]

                              Hmm good point but this now depends on if we are looking for 1% of the full amount deposited
                              as the fee or are we looking for 1% of the spend amount as the fee. Given that we only start with the full amount.

                              If you simply divide by 100 plus the fee
                              then the higher the fee the more out the answer becomes

                              e.g
                              20% Fee
                              100 / 100% * 20 = 20
                              100 - 20 = 80
                              or
                              100 / 1.20 = 83.33333333333333
                              meaning the fee is only 16.6666 recurring of the total amount

                              The only way to get the correct answer is to fix the mistake in the original problem.
                              (I have added % signs to the simple int values that represent percentages)

                              Example (ignoring any potential TX fee for now)
                              Fee = 1 [color=red][b] //just use 1 here instead of a decimal 0.01 (as it means 1% anyway)[/b][/color]
                              Balance = 100
                              100 * [color=red](Fee / 100%)[/color] = 1
                              100 - 1 = 99 (max spendable balance?) [b][color=red](This is Actually Correct!)[/color][/b]

                              User then sends their displayed max spendable balance but the fee is charged on the amount entered, so…

                              >>> 99 * 0.01 = 0.99 [b][color=red](No dont do this)[/color][/b]
                              99 / (100% - Fee%) = 1 ()
                              99 + 1 = 100
                              100 - 100 = 0.00 (All is well again in the world :-) )

                              1 Reply Last reply Reply Quote 0
                              • U
                                UKMark last edited by

                                [quote name=“iawgoM” post=“30516” timestamp=“1381079477”]
                                Your problem here, because of which it’s impossible to make it “perfect” is limitation in number of decimal places. FTC client allows you only to send transactions with maximum of 8 decimal places.
                                Remember that movie when some guy thought of how banks kind of ignore small amounts beyond two decimal places? And he wrote a software that transfer all those leftovers to his own account? :) For example if someone’s balance with interests was 100.001 US$ in account you would always see 100.00. My BTC-e USD balance currently shows 0.00001009USD lol…

                                In your case, maximum amount to spend if you have 100FTC and 1% fee would be: 99.00990099009900(990099…) - Excel and Windows calculators don’t even accept anything beyond 14
                                decimals (brackets).
                                Fee for this would be: 0.99009900990099. Total = 99.00990099009900 + 0.99009900990099 = 99.99999999999999

                                So, you will have to find a way to calm down your OCD… :D

                                As I always copied the results from people in school, I’ve developed special skill - heavy improvisation to get this result, and my procedures probably made Newton and others scream in their graves ::) Teachers would always be like “dude, dafuq? what, this…how did you, why, how dare you, GTFO!!!” :D

                                But here it goes:
                                a (total amount) = 100.00 FTC
                                b (fee) = 0.01
                                c = 100*0.01 = 1
                                d = a - c = 100-1=99
                                e = d * 0.0001 = 99 * 0.0001 = 0.0099 (oh yes, adding 4 more decimal places)
                                f = e * 0.0001 = 0.0099 * 0.0001 = 0.00000099 (and 4 more - to get 8 decimal places total)
                                SPENDABLE=d+e+f = 99+0.0099+0.00000099 = 99.00990099
                                Fee for sendable amount = 99.00990099 * 0.01 = 0.9900990099 (notice the 10 decimal places here)
                                Spendable+fee = 99.9999999999 - and this cuts off after 8th decimal place?

                                Let’s try with other number:
                                a (total amount) = 15.00 FTC
                                b (fee) = 0.01
                                c = 15*0.01 = 0.15
                                d = a - c = 15-0.15=14.85
                                e = d * 0.0001 = 14.85 * 0.0001 = 0.001485 (oh yes, adding 4 more decimal places)
                                f = e * 0.0001 = 0.001485 * 0.0001 = 0.00000014(85) (this one goes over 8 decimal places)
                                SPENDABLE=d+e+f = 14.85+0.001485+0.00000014 = 14.85148514
                                Fee for sendable amount = 14.85148514 * 0.01 = 0.1485148514 (notice the 10 decimal places here)
                                Spendable+fee = 14.99999999(14) - and this cuts off after 8th decimal place?

                                This all means that people will still have some funds in their account…but they are beyond 8 decimal places…somewhere in limbo - but this way you will still show them what actually IS the maximum spendable amount.

                                Gosh, I’m so wrong with this, right? :D
                                [/quote]
                                [quote author=spynappels link=topic=3929.msg30529#msg30529 date=1381088316]
                                It’s actually probably a little easier still.

                                The percentage fee is of the spend amount, so starting amount is 100% of spend amount plus fee percentage.

                                If fee is 1%, starting amount is 101% of spend amount.

                                Therefore spend amount = start amount / 1.01

                                Adjust the divisor depending on the fee percentage, for 5% spend amount = start amount / 1.05
                                [/quote]

                                Just like to say thanks for everyone’s input. 8)
                                Ok I am going to accept, 2 answers, iawgoMs and spynappels, as they both solve my stated problem while retaining the accuracy that I require!
                                I will send iawgoM 400FTC for being the first and spynappels 100FTC for simplifying iawgoMs answer greatly!

                                1 Reply Last reply Reply Quote 0
                                • S
                                  spynappels last edited by

                                  Thanks dude!

                                  1 Reply Last reply Reply Quote 0
                                  • Q
                                    Quer last edited by

                                    Hi,

                                    a = 100 FTC
                                    b = 1%

                                    –> c = 1 FTC (fee amount)
                                    –> d = 99 FTC (spendable amount)

                                    Recalculation of the fee amount © from the perspective of the spendable amount (d)
                                    = (d/(100-b)*100)/100*b = c

                                    Works for every fee and amount.

                                    1 Reply Last reply Reply Quote 0
                                    • kris_davison
                                      kris_davison last edited by

                                      [quote name=“Quer” post=“30586” timestamp=“1381152025”]
                                      Hi,

                                      a = 100 FTC
                                      b = 1%

                                      –> c = 1 FTC (fee amount)
                                      –> d = 99 FTC (spendable amount)

                                      Recalculation of the fee amount © from the perspective of the spendable amount (d)
                                      = (d/(100-b)*100)/100*b = c

                                      Works for every fee and amount.
                                      [/quote]

                                      Yes thats what I was trying to explain. But you can but try as they say!

                                      1 Reply Last reply Reply Quote 0
                                      • S
                                        spynappels last edited by

                                        [quote name=“kris_davison” post=“30602” timestamp=“1381160656”]
                                        [quote author=Quer link=topic=3929.msg30586#msg30586 date=1381152025]
                                        Hi,

                                        a = 100 FTC
                                        b = 1%

                                        –> c = 1 FTC (fee amount)
                                        –> d = 99 FTC (spendable amount)

                                        Recalculation of the fee amount © from the perspective of the spendable amount (d)
                                        = (d/(100-b)*100)/100*b = c

                                        Works for every fee and amount.
                                        [/quote]

                                        Yes thats what I was trying to explain. But you can but try as they say!
                                        [/quote]

                                        Your maths was impeccable, but UK Mark was trying to calculate (d) given (a) and (b), given that (b) was always going to be a percentage of (d).

                                        iawgoM’s calculations calculated (d) to a sufficiently precise level.

                                        You guys were right but focused on the fee amount rather than on the spendable amount.

                                        1 Reply Last reply Reply Quote 0
                                        • kris_davison
                                          kris_davison last edited by

                                          True sufficiently accurate rather than accurate.
                                          There was really no need to have rely on rounding at all.

                                          1 Reply Last reply Reply Quote 0
                                          • kris_davison
                                            kris_davison last edited by

                                            Sorry I’m happy you found a solution that fits that’s all that matters I guess. Just me being funny about maths the one area where a solution really can only be correct or not.

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post