So we talked a lot about int 32 and int 64
and these are super important topics.
Let me really highlight again that we can sort with these types of numbers, that we can query for them
with greater than and all the things you could do with the normal numbers,
these things are possible with number int and number long too or with int 32 and int 64 as I should
say because number int and number long are really just the constructors we use in the shell to create these
number types.
But let's now move towards our high precision double because that will also be interesting to understand.
To understand what it does,
let's say we have some database which we use for scientific calculations.
So I have my collection science in there and I insert one new document and let's say we have two
values, A which is .3 and B which is .1
and please note these are the normal numbers as I used them in the shell and therefore, because the shell
is based on Javascript, these are 64 bit floating point numbers,
so they are not these high precision decimals.
If I insert this, this will work, if I then find all my elements in there,
this looks good right,
the values look the way I store them.
Now keep in mind what I mentioned earlier in this module,
behind the scenes they will actually be stored with something like this.
So there will be more decimal places than you see here and there will be some imprecision,
it simply approximated to this and Javascript,
so the shell here simply outputs it in this approximated value which of course looks all right here.
But we can see the difference if we do a calculation with the help of the aggregate function, so of the
aggregation framework. Here
I only want to pass one stage, one step in my pipeline and that is the projection stage,
so I will find all elements and project them into something new and there I will add a new field and
let's name this result, whatever you want. And the value for this field has to be a document as you learned
in the aggregation framework module
and here I will subtract. I will subtract two values which are passed to this array and I will subtract
a and b
and they have to be referenced with dollar signs here so that mongodb will look for these
fields.
If I hit enter here, you see something strange, the result of A - B is not 0.2
as you would have expected
but this value and this shows you the imprecision I'm talking about.
These values are not stored exactly and in some use cases, in a lot of use cases, this might not matter even
if you are building an online shop with products where you store the price where you would say of course
I don't
want to lose some fraction of a cent, even in such a case, you could be fine with these values here
because you might only use that price to display it on your web page where the approximation will be
all right
because you don't have that many digits you approximate, you only show two decimal places and these
will be correct.
And even for you then kind of charging this, you might be fine because you send this to some third party
service,
you send your value as it is returned by the database,
so something like this, you send that to the provider and you then rely on the provider charging exactly
that and not some incrementally lower amount.
So you might be fine using these values but if you do calculations with them, if you work with them on
the server here because aggregate of course executes this on the mongodb server,
so if you do something like this where you extract data, return value from the database and you end up
with a result like this, you might of course have a problem because that is probably not acceptable for
your application then
and this is exactly where this high precision double, the 128 bit double can help you.
So let's have a look at that in the next lecture.