You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You may have heard that you should not use floats for monetary calculations. The reason behind this advice is that some inexperienced programmers may think because currencies have a decimal point in their representation, they should be represented by floats. For example, they think because there is a decimal point in $1.23 they need to store it in a float. As you know 1.23 does not have a finite binary expansion. 1.23 in the binary world is like 4/3 in the decimal world, which is 1.33333... and needs an unlimited number of digits to be represented. Converting 1.23 to a binary floating point will always introduce a small error. That is because the binary representation does not have a finite expansion and you'll need an unlimited number of binary digits. So, when you convert it back to a decimal number, it may be converted to something like: 1.2299999999999999822364316 (64bit) or 1.2300000190734863281250 (32 bit). This small error is somehow unexpected, because you've not done any operations on your number!
The fact is, as long as you are doing only addition and subtraction, you don't need float arithmetic. That decimal point in USD is irrelevant, since the number of digits after the decimal point is always fixed (2 digits), you can just use integers. In everyday monetary calculations, like simple buying and selling of non-fungible goods usually we only use addition and subtraction and there is no need for floats.
However, things will change as soon as you need to divide two numbers. Again the decimal point is irrelevant here. This also applies to integers like 2, 5, 10. Doing arithmetic that includes division with integers is a dangerous practice. Integer arithmetic has a fixed absolute error: error < 1. That's why 99/100 completely disappears in integer arithmetic and equals 0. On the other hand, floats have a fixed fractional error instead of an absolute error and at the same time, they are very efficient. Any time you need divisions (or any function including division like log, sqrt , ... you should definitely use floats.
Of course when you are doing any type of arithmetic you should always think about error and how it is being accumulated and rounded. Removing floating point numbers will not remove arithmetic error magically. It actually makes handling it much more difficult.
It turns out that using equations including divisions is quite popular in smart contracts. That's why in a system which does not have floats they had to use 18 decimals for representing BUSD when in the real domain it doesn't need more than 2 or 3 decimals.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
You may have heard that you should not use floats for monetary calculations. The reason behind this advice is that some inexperienced programmers may think because currencies have a decimal point in their representation, they should be represented by floats. For example, they think because there is a decimal point in $1.23 they need to store it in a float. As you know 1.23 does not have a finite binary expansion. 1.23 in the binary world is like 4/3 in the decimal world, which is 1.33333... and needs an unlimited number of digits to be represented. Converting 1.23 to a binary floating point will always introduce a small error. That is because the binary representation does not have a finite expansion and you'll need an unlimited number of binary digits. So, when you convert it back to a decimal number, it may be converted to something like: 1.2299999999999999822364316 (64bit) or 1.2300000190734863281250 (32 bit). This small error is somehow unexpected, because you've not done any operations on your number!
The fact is, as long as you are doing only addition and subtraction, you don't need float arithmetic. That decimal point in USD is irrelevant, since the number of digits after the decimal point is always fixed (2 digits), you can just use integers. In everyday monetary calculations, like simple buying and selling of non-fungible goods usually we only use addition and subtraction and there is no need for floats.
However, things will change as soon as you need to divide two numbers. Again the decimal point is irrelevant here. This also applies to integers like 2, 5, 10. Doing arithmetic that includes division with integers is a dangerous practice. Integer arithmetic has a fixed absolute error:
error < 1
. That's why 99/100 completely disappears in integer arithmetic and equals 0. On the other hand, floats have a fixed fractional error instead of an absolute error and at the same time, they are very efficient. Any time you need divisions (or any function including division likelog
,sqrt
, ... you should definitely use floats.Of course when you are doing any type of arithmetic you should always think about error and how it is being accumulated and rounded. Removing floating point numbers will not remove arithmetic error magically. It actually makes handling it much more difficult.
It turns out that using equations including divisions is quite popular in smart contracts. That's why in a system which does not have floats they had to use 18 decimals for representing BUSD when in the real domain it doesn't need more than 2 or 3 decimals.
Beta Was this translation helpful? Give feedback.
All reactions