Hello.
In this lecture, we're going to talk about Question 22.
What is the difference between double and decimal?
This is a question that you can hear quite often during interviews, especially in the financial sector.
After this lecture, you will have a clear understanding of why the difference between double and decimal
is that double is our floating point.
Binary number one decimal is a floating point decimal number.
What matters most for us is that double is optimized for performance.
Well, this email is optimized for precision.
First, let's understand the floating point parts.
Floating point numbers cannot be integers, but numbers like half one, third, one fourth, etc. We
can actually represent such numbers using integers.
If you use the concept of Mandisa and Exponent Mandisa, give some skilled representation of the number
one exponent since, well, the scale is, in other words, where the decimal point will be.
That's why they are called floating point numbers as the point is moving.
This probably sounds a bit mysterious, so let's try to represent number 324 0.56 with monetizable and
exponent.
The pattern is simple we multiply the Mandisa by the base of the system raised to the power of the exponent
10 to the power of minus two is zero point zero one.
So the result is 224.
That 56 as expected.
As you can see, we manage to represent our floating point number with integers only.
We said that a double is a binary floating point number, which means the base of the system it uses
is to four decimal.
It is done.
All right, here comes the problem with floating point numbers.
If the monkeys are limited precision and it does in computers, or even if we try to write it down on
a piece of paper, it means we can't represent some numbers in a perfectly precise way.
For example, if you wanted to write one third as a floating point number on a piece of paper, you
would fill it with 0.3 free, free, free, free and so on.
But finally, you would run out of paper.
Well, there are no you would, right?
It would be some approximation of the number that is actually one third, the same as with the paper.
You can run out of bits when representing such a number in C Sharp, which leads to representation that
is not precise.
Double is a binary floating point number, occupying 64 bits.
One bit is reserved for the sign, so to know whether the number is positive or negative.
Fifty two bits are reserved for monkeys and 11 bits are reserved for the exponent.
We said that numbers, like one third, are impossible to be represented in the decimal number system,
with finite number of digits in the binary system.
An example of such unrepresentative number is one tenth.
Let's see some code that will show the lack of precision of taboos.
From the point of view of real world mathematics, this should evaluate to true.
Let's see what the computer's mathematics has to say about that.
Well, that's obviously not correct, but it shouldn't be that surprising, given that we already established
that the floating point numbers are not represented precisely in the computer's memory.
And here's an important note since doubles are approximations of numbers and are not very precise.
We should avoid simply comparing them with the quality operator.
Think of it like this in real life, you are measuring borrowed to build something.
You want the boards to be of the same length.
You measure them carefully and you are happy to see that.
Both have the length of exactly 273 centimeters and zero millimeters.
So you can say to yourself, yes, those boards are equal and move on to constructing a shed for gardening
tools.
But are they truly equal?
I bet if you measure them with some advanced machinery, it would actually turn out.
Not one of them is two hundred seventy three point zero one, while the other is 273 point zero four
centimeters long.
So they are not exactly equal, but they are equal enough for your needs.
It's the same thing which doubles when checking for the equality.
We should rather check if the difference between them is so small that we don't actually care about
it.
So the simplest implementation of a method checking the equality could be this.
In this matter, I simply check if the absolute of the difference is smaller than the tolerance.
Let's see what will be printed from this line now through has been printed.
Because the difference between those two numbers is smaller than the given tolerance.
One more note about Dubbo's a variable of the type can have a specific value called a noun, not a number.
It is reserved for representing undefined mathematical operations like, for example, dividing zero
by zero.
As you can see, this gave a result, which is not a number.
All right.
That closes the topic of doubles.
But before we move on to decimals, let's mention floats.
Float acts exactly the same as double.
The only difference is that float is scored on 42 minutes.
Well, double is on 64.
So Double simply has a larger range.
All right.
We learned that doubles are not very precise, and we must be careful when making assumptions about
them.
For example, the equality of two double numbers may not be the same as it would be in a real world
mathematics for some scenarios.
We need precision and we can't make any compromises about that.
For example, imagine a banking system that cross-check some transactions.
It must be sure that after the money transfer, the amount that was taken from your account is exactly
the same as the amount that was added to the receiver's account.
If you use double for representing money.
This could lead to errors.
Both amounts would be represented as some approximation, and they could not be exactly equal.
That's why, for representing money, we should always use decimals.
Decimal is optimized for precision.
It occupies more memory than the double because a smaller range and operations on it are slower, but
it guarantees precision.
Of course, like other C-sharp numeric type, because its size, limitations and country presents numbers
smaller or larger than this.
But within this range, the operations will give correct results without any surprises.
First, let's see the code that we had before, but this time let's use decimals.
As you can see, this time, nothing unexpected happens, so we gained precision.
But at what price?
First, let's measure the performance.
Here are defined amateur doing some operations on Dubbo's, and he was exactly the same method, but
using decimals.
Let's see how both of them will do when the number of iterations is far million.
Yikes.
Calculating decimals took almost seven times longer than doubles.
There is one more thing.
Doubles are not only faster, but they also have much larger range while occupying less memory.
As you can see, doubles rent is just ridiculous, which makes them perfect for some scientific users.
When one often operates on very small or very large, numbers, also double takes only eight bytes while
decimal take 16.
In general, we should use doubles when representing some numbers coming from nature, like physical
measurements, which by definition are not perfectly precise.
That was our right to represent things like land speed, position on the map and such.
Of their great performance.
They are widely used in some industrial applications, games, etc. Decimals are much slower.
They occupy more memory and have a smaller range.
But they guarantee precision will use them when we can't allow any approximations.
For example, when representing money points in games or other human made concepts that we need to represent
precisely.
Let's summarize the topic of double versus decimal doubles are optimized for performance.
Decimals are optimized for precision.
Decimals have much worse performance than doubles.
They also have a smaller range, so they are not good for representing really small or really large
numbers.
Because of that, decimals shall be used when we care about precision.
For example, when we want to compare two sums of money and tell if they are exactly equal or not,
doubles are less precise, but faster.
They are perfect for representing numbers that are not human made, but rather come from nature or physics,
like the speed of a car or the length of a wave when talking to doubles for equality.
We should only check if they are close to each other within some tolerance.
During the interview, you can hear questions like What is the difference between double and float?
The only difference is that double occupies 64 bits of memory, while float occupies 42, giving double
our larger range.
Except for that, they work exactly the same.
What is the and then in the end, the means not a number, and it's a special value that double or float
can be.
It's reserved for representing results of undefined mathematical operations like dividing zero by zero
or infinity by infinity.
What numeric type should we use to represent money?
What we present in money, we should always use decimals.
All right, that's it for this lecture and in general, the topic of representing numbers.
Thanks for watching and see you in the next lecture.