Enter your email below to watch this video
Aaron is a natural teacher and this course is the best introduction to Postgres I have come across. Lessons are easy to follow and he recommends some great tools for working with Postgres.Joel Drumgoole
Shorten dev cycles with branching and zero-downtime schema migrations.
We're gonna take a little break from specific data types 'cause I want to show you a few oddities about some of these numeric types. Just real quick, this will be a fast one.
If we did select NaN cast it to a numeric, we would get a NaN, we get a not a number. Floats also have not a number, integers do not have not a number. A not a number is abbreviated as a NaN, numerics floats have them. Now, numerics floats also have infinity. Integers do not because, by definition, integers are bounded infinity is unbounded.
However, an open-ended numeric does have infinity, a closed numeric with a range does not have an infinity. It also has a negative infinity, you could change it to inf if you don't feel like typing. Now, I believe floats also have infinities negative infinities. One interesting thing about NaNs is not a number, I believe this is Postgres specific.
If you cast this to a numeric, all NaNs are equal to all other NaNs, all infinities, infinities are equal to all other infinities. When it comes to sorting, NaNs are huge. If we say NaN is greater than, you know, a big numeric, if we can type, we'll say cast as numeric, NaN is greater than the other numbers. Some things work.
Like infinity, infinity plus infinity, a little different style there, infinity plus infinity is infinity, infinity minus infinity is not a number. Infinity plus 1 is infinity. That checks out. Infinity minus 1 is still infinity. There's some deep math, there's some deep math or I guess that's more philosophy in there. NaN infinity do exist.
If you need something that is arbitrarily large or arbitrarily small, something that is unbounded unknowably large unbounded unknowably small, you have infinity negative infinity. Those actually can serve a purpose. Putting a NaN purposefully into your dataset, I can't think of a very good example. NaN is real, it exists, it represents not a number, there might be a very valid use case for that.
I don't fully know what it is because there are times when you can turn a NaN back into a real number. A NaN plus a 1 is gonna give you a NaN. If you raise it to the power of 0, it's going to give you a 1. Because any number raised to the power of 0 gives you a 1.
Incredibly useful? It is not. Good to know that it exists? Yeah, I think . I think infinity negative infinity are probably more useful. Knowing that NaN exists? Hmm, maybe helpful.
Remember, they only exist in the unbounded columns, which is numeric without a precision in scale or the floats in the integers. They do not exist.