nomisiv.com / blog / how-to-make-money TOC

How to make (a not bad) money (type in code)

Picture this: You want to open a small local mom-and-pop international bank. You want customers that have accounts, and these accounts have money in different currencies. They can send each other money, and they can convert it to different currencies between accounts. Typical bank stuff, right? Instead of buying an off-the-shelf banking system you want to build it yourself, cus you’re feeling particularily entrepreneurial today.

You start building the system, but almost immediately, you get stuck. How do you represent money the “right” way?

Baby’s first money type

You start off with a unsigned 32-bit integer, because it’s your favorite datatype when it comes to numbers, and money is just numbers, you think, and move on with your day.

type Money = u32;

You build the rest of the system thinking “this wasn’t that hard”, but you have a small inkling of a thought in the back of your head that you might have missed something.

Then you get your first customer, and they love your international bank, and they want to transfer all their money to your bank from their old bank. Luckily they only have one account, but the amount of that account is 6.534,50 SEK. They actually did have an integer amount on their old bank account the other day, but that changed when they purchased a chewing gum some days later that cost 1,50 SEK.

Your system crashes on an .unwrap(), because your code was not able to parse their transfer amount into a u32.

Rookie mistake…

…you think, as you refactor your code to use a 32-bit floating point number instead. Surely, this is solved now.

type Money = float;

You find your next customer. While they are exited about your value proposition, they would like to start of small with two test transfers of 0.10 SEK and 0.20 SEK, just to make sure everything works correctly, before sending over all of their money. You watch the transactions land in your bank, and not one minute later, you receive a call from them:

Why do I have exactly 0.30000000000000004 SEK on my account?

You’re confused, and you don’t believe your eyes. You saw the transactions yourself, they clearly were 0.10 and 0.20. How did this happen?

You consult your favorite webpage, StackOverflow1, which tells you that this is a common issue with floating point numbers, due to lack of precision. Something about about limited presicion and binary-base exponentiation.

As a quick fix you try to change the type to a 64-bit float instead, but you quickly realize that the same issue remains, but now in even smaller amounts.

Almost unlimited precision

A picture of Darth Sidious shooting purple flashes out from his fingers, accompanied by the synesthetic “Unlimited Power”

You decide to think this through for a little bit. An int cannot represent cents, and a float doesn’t math properly. What if we just created a new type that just wraps a float, but truncates off the precision-error by only using the two first decimals? Then we have as much decimals as we care about.

type Money = float;
    
    fn truncate(m: Money) -> Money {
      todo!()
    }

Confident in the new version of the banking platform, you eye up a real whale of a customer, and to your suprise they are interested. They tell you that their grandmas passed away recently. Yes, both of them. The grandma on their mother’s side left them 500.000.000.000.000,00 SEK (five hundred trillion swedish crowns), and the grandma on their father’s side, not to be outdone by the other, left them 500.000.000.000.000,01 (five hundred trillion swedish crowns and one öre)

After transferring both of their inheritences to your bank, they send you a flaming email:

Hello Dear <your name>,

You have STOLEN 1 öre from me, and I will not accept this. Look at my account balance!!!!!

Worst regards

Their account balance indeed says 1.000.000.000.000.000,00, and not 1.000.000.000.000.000,01. You would also be upset if you lost one öre if you had this much money, so you completely understand where they are coming from.

Once again, it seems that the floating point presicion is coming back to bite you.

BigMoney

With frustration and resignation, you turn to your third most favorite (and thus least favorite) datatype for numbers: the string. The benefit of the string is that it can be arbitrarily long, but the drawback is that you have to reimplement all the mathematical operations to operate on one digit at a time. You realize that you can optimize this by instead of a string using a linked-list or a dynamically sized array of digits, then you don’t have to convert the digits between chars and ints.

type Money = Vec<u8>;
    
    fn add(a: Money, b: Money) -> Money {
      a.
    }

  1. Here’s one such article, but I’m sure there are more of them: https://stackoverflow.com/questions/588004/is-floating-point-math-broken. Fun fact, there is even a website for this number: https://0.30000000000000004.com/↩︎