 Okay, hi everyone. How are you doing? My name is Stephanie and I'm one of the organizers of Iris Desk Out, but today I happen to be a speaker. So I'm going to talk today about money at a subject that is near dear to my heart, specifically in relation to floating point errors. So, okay, I don't know. So the scope of my talk today would be based on consumer-facing apps that will use only simple addition or subtraction, so anything like a budget app or maybe a payments app where you don't have to do too many crazy multiplications and divisions to your figures. And for the reference point today I'll be using Singapore Dollars. So, has anyone heard of Ariane 5? Has anyone heard of Ariane 5? Okay, one percent. Okay, so, Ariane 5 was a rocket that, well, crashed and they're really bad because of a floating point error that the scientists made. So it was launched in 1996 and it crashed 40 seconds after its takeoff. So it was on its first voyage after one decade of development and it cost over $7 billion to produce. And it turned out because of its failure was due to a software error in its initial reference system where there was a conversion of a 64-bit floating point number relating to its horizontal velocity with respect to the platform that was converted into a 16-bit signed integer. So the number that was originally supposed to be was larger than the 2,767 which was larger than the largest installable integer, I mean, value in a 16-bit signed integer. So it's very off. So, and disaster ensued. Okay, the next question I have for you is, does anybody know what salami slicing means? Okay, nobody knows. Okay, it's not like going to call storage to get someone to slice salami for you, but it refers to the act of removing or reducing something in small amounts of a large period of time. So it's less noticeable to changes. So that is sometimes what happens when we ignore floating point errors. You know, I don't run up around down and we keep doing the errors, tend to build up over time. Okay, so how does this relate to money? So money basically is, this value here is in base 10. So if you had to display, if you had to see the location of this, this would be, sorry, 9.2 to the power 10, 9.2 times 10 to the power zero, so 9.2. So in money, like in Singapore dollars, the cent is this, like the smallest unit of it. And usually you're like 100 cents makes like $1. So sometimes we divide cents by hundreds in order to get the value displayed here. So at times like, you know, when you enter like a floating point type into Swift without determining what type it is, it will automatically infer the type is a double if we don't specify what type it is. And that can be kind of a problem when displaying currency, especially if the currency has an amount that, like if you look at a dollar, and there's below a dollar. So anything below, in between zero and one will have trouble being expressed. And you will lose certain precision when it comes to maybe like a lot of figures that you start to see the growing errors. So, okay, IE 754 is a technical centre of floating point computation. So it comprises the negative sign to the power of whether the digit, I mean the number is positive or negative. So if it's positive, the sign would be the power of zero. And if it's negative, the sign would be the power of one, the means is that, and the exponent, so to the power of whatever it is. So floating point type numbers will store numbers based on this notation. And therein lies the problem when the numbers fall below the decimal point. So this is definition of float and double in SWIFT. So float is 32 bits and then double is 64 bits, which gives us slightly more precision because there's limited number of bytes that their computer can store for each number. And so if you look at 920, number I showed you earlier, it actually is displayed as this in base two. If you want to, you can check it out if you want. Okay, but it cannot be stored particularly accurately, like when it's in size of float or double. For example, this one looks like float and that one looks like a double. So not exactly $9.20, maybe a bit less. I mean, if I owed you $9.20 and I gave you back maybe 9.19, I'm not sure, and if you'd be happy with that. All right, so what's the rest of my money? So another example I want to show is, okay, another example that I have is, okay, for example, I borrow $1 from Vina. So I'm kind of short of cash, so I decide to pay her back every day $0.01. And then, and I pay her back $0.01 every day through a not very well made app that uses a float to store $0.01. So okay, so every day I will pay Vina that $0.01 through this app. So if I pay her back once and every day for 100 days, can anybody tell me how much I will end up paying her back? Okay, so all right, I hope so. Maybe, maybe, so okay, let's take a look. All right, anyway, my name Pan, so it's there. So it will be there. So not quite a dollar as we hoped, right, through this like incremental errors do end up adding up. So, I mean, this is only a small example, but if you're adding like huge sums and numbers over a long period of time there, error can be a lot larger, right. So I have another question for you. So if I add, okay, 0.1 plus 0.2, what does it give me? Who says 0.3? Okay, so all right, let's take a look. Oh, a little bit more than that. I hope, I don't think you meant to pay me that much more. Okay, so, so my proposed solution to like alternative to using floating point numbers to, to store your units of currency or money, right, is to use an integer, an integer to store like every, I'm sorry, an integer to store every like smallest unit of currency there is. So now programming languages have a solution built into, to solve this problem of floating point numbers. So like a solution that is readily available and swift is using like an integer to represent the smallest unit of currency and single dollars that happens to be one cent. So, one cent is the smallest unit. So, I have a, like a sample implementation of like how I would, I would write my, create my money structure that I think works. It works for me, depending on what you need, it could work for you. So, basically, every single kind of money type will have an amount that thought is an integer. And then this amount will have an associated currency with it because there's no point in having money without currency associated with it. So, I've created my, okay, I reference this with somebody. So, and I made a couple of changes to it, like the currency type, sort of a code, because every single currency has a specific code, its name, and then the minor unit, which is defined as the number of decimal points it has when displayed, it has. So, in terms of Euro, it's called UR and then the minor unit is 2 because it has cents. And for Japanese yen, it has no cents. So, zero minus. Okay, so if I also created a method to format that currency because we tend to, well, I think the reason like why we deal money so that we can display to our users, right? So, we have to format it based on, like, in my case, like dividing the number of cents to display as money. So, I divided by 10 to the power of the number of minor units it has and then I format the string. So, it works like that. So, if I have my, I have 1046 cents and then I'm trying to add 846 cents together. So, I will have, oh yeah, as I mentioned, I also created some operation type so that you can add these together. So, just at left and right. Okay, and then if I add it together and then I format my currency, you will have that in 2020. So, another advantage is that you can, like, if you don't want to display Euro, you can easily just change it to Japanese yen which doesn't have a minor unit. So, just change it and then I'll show that. Okay, so, I, like, I guess currency is something pretty, like, like, if we to work with in a sense that we always want to maintain the highest possible level of accuracy that we can get. So, I think that it's best to work in the smallest, you know, currency by, by, like, by taking account of, like, the whole amount of money I need to deal with in sense and then displaying it only, like, dividing it only to display it but never doing any of the operations using that value to display, like, for example, like over here. When I do divide it to show I'm converting it to a decimal and then, and then displaying it. But I do not use this value to add it together, add anything together or to subtract anything from anywhere because I find it a lot safer to do it this way. But you can do it any other way if you want. But this way works for me. And so, yeah, that's, so that's all I have. I just hope that if you can take away anything, try to avoid using float or any floating point types to represent money. Yeah, thank you.