1
00:00:00,180 --> 00:00:05,010
The next step will quickly take a look at two other data types we have at our disposal that we could

2
00:00:05,010 --> 00:00:07,050
use to store decimal numbers.

3
00:00:07,080 --> 00:00:09,600
One is float and the other is double.

4
00:00:09,990 --> 00:00:14,430
How are they different from decimal and why would you use one versus the other?

5
00:00:14,460 --> 00:00:18,120
Well, it's a little confusing, but here's what it boils down to.

6
00:00:18,510 --> 00:00:19,500
Decimals.

7
00:00:19,500 --> 00:00:25,980
The decimal data type we just saw, it can be more exact, but it takes up more space or it can take

8
00:00:25,980 --> 00:00:26,940
up more space.

9
00:00:26,940 --> 00:00:34,200
If we use float or double, we can store larger numbers or larger decimal points using less space,

10
00:00:34,320 --> 00:00:36,690
but it comes at the cost of precision.

11
00:00:36,690 --> 00:00:41,670
So it may not be as exact, but most of the time it's also perfectly fine.

12
00:00:41,670 --> 00:00:46,350
I mean, if you're working with something where you know you need a decimal, then you'll use decimal.

13
00:00:46,470 --> 00:00:49,110
But a lot of the time you can get away with float or double.

14
00:00:49,110 --> 00:00:54,180
It's not like they're horribly imprecise or anything, but there is a difference in how they're implemented

15
00:00:54,180 --> 00:00:56,790
behind the scenes and how they actually store the numbers.

16
00:00:57,060 --> 00:01:03,900
Decimals, store numbers as decimals and memory, whereas floating point numbers don't floats and doubles.

17
00:01:03,900 --> 00:01:05,760
But again, we're getting too much into the weeds.

18
00:01:05,760 --> 00:01:09,360
So here's a table that illustrates the difference between float and double.

19
00:01:09,390 --> 00:01:15,930
Both of them store numbers in the same sort of way, but they are different sizes.

20
00:01:15,930 --> 00:01:21,390
So a float is going to take up four bytes, a double takes up eight bytes, and then we have precision

21
00:01:21,390 --> 00:01:28,290
issues after around seven digits for a float and after around 15 digits for a double.

22
00:01:28,290 --> 00:01:29,700
So let me show you an example.

23
00:01:29,700 --> 00:01:33,360
Let's just make another create table.

24
00:01:34,600 --> 00:01:39,010
Let's just call this numbers and we'll have N.

25
00:01:39,040 --> 00:01:40,210
How about X and Y?

26
00:01:40,210 --> 00:01:43,450
It's stupid, but X is going to be a float.

27
00:01:43,870 --> 00:01:47,800
Y will be a double OC just like that.

28
00:01:48,130 --> 00:01:50,230
And oh no, I already have numbers.

29
00:01:50,260 --> 00:01:51,880
Must have done that in a previous video.

30
00:01:51,910 --> 00:01:53,230
How about Nums?

31
00:01:53,230 --> 00:01:54,880
Terrible name, but that's fine.

32
00:01:55,090 --> 00:02:05,050
So if I do insert into nums x comma, y values and I do something like 1.12, three, four, five,

33
00:02:05,050 --> 00:02:09,970
six, seven, eight, actually let's just do one, two, three, four, both of them.

34
00:02:09,970 --> 00:02:13,000
So 1.1, two, three.

35
00:02:14,820 --> 00:02:16,500
I've got an extra paren here.

36
00:02:16,650 --> 00:02:17,640
I hit enter.

37
00:02:17,850 --> 00:02:20,390
This should work with no issue whatsoever.

38
00:02:20,400 --> 00:02:21,990
Select start from nums.

39
00:02:22,200 --> 00:02:26,650
Both of them X and Y are storing 1.1 to 3 OC.

40
00:02:26,880 --> 00:02:33,450
But remember we could run into precision issues after around seven digits after the decimal for a float.

41
00:02:33,450 --> 00:02:34,230
So what if I do?

42
00:02:34,230 --> 00:02:37,680
One, two, three, four, five, six, seven, eight maybe.

43
00:02:37,680 --> 00:02:39,120
And then same thing over here.

44
00:02:39,120 --> 00:02:40,770
Three, four, five, six, seven, eight.

45
00:02:41,520 --> 00:02:42,690
Select star.

46
00:02:43,170 --> 00:02:44,550
And what do we see?

47
00:02:44,550 --> 00:02:50,010
Well, over here we have for our float column a much less precise number.

48
00:02:50,010 --> 00:02:51,250
We lost digits, right?

49
00:02:51,270 --> 00:02:53,130
We ended up getting a rounded number.

50
00:02:53,130 --> 00:02:58,620
And then over here for the double, which again is double the size in memory.

51
00:02:58,620 --> 00:03:00,840
So eight bytes instead of four.

52
00:03:01,170 --> 00:03:05,190
We didn't lose precision, but we will if I keep going.

53
00:03:05,190 --> 00:03:09,180
If I did, you know, eight, nine, eight, seven, six, five, four, three, two.

54
00:03:09,180 --> 00:03:11,100
I don't know why I'm doing these consecutive numbers.

55
00:03:11,100 --> 00:03:11,970
We don't have to.

56
00:03:12,000 --> 00:03:19,170
If I do that, you'll see at some point we lose precision and it gets cut off to what, six, five,

57
00:03:19,170 --> 00:03:24,060
four, three, three OC So there is a difference between float and double.

58
00:03:24,060 --> 00:03:25,290
You can see it right there.

59
00:03:25,500 --> 00:03:26,790
There's a clear difference.

60
00:03:26,790 --> 00:03:32,610
We inserted the same number here, but only one of them was preserved as the whole number we inserted

61
00:03:32,610 --> 00:03:33,840
with all the digits.

62
00:03:33,840 --> 00:03:39,660
That was the double, but the float was truncated and that might not be a problem depending on the size

63
00:03:39,660 --> 00:03:42,240
of the number you're trying to store and the precision you need.

64
00:03:42,240 --> 00:03:49,410
Remember, these take up less space than using a decimal, but it comes at the potential cost of precision.

65
00:03:49,500 --> 00:03:53,610
So when you should use one versus the other is a tricky subject.

66
00:03:54,060 --> 00:04:01,140
The docs have a very brief article about this and they say you should try and use the most precise type

67
00:04:01,140 --> 00:04:02,280
whenever you can.

68
00:04:02,280 --> 00:04:08,220
If accuracy is not that important or if speed is the highest priority, then double might be good enough.

69
00:04:08,220 --> 00:04:14,610
But for high precision use decimal or for even higher precision, they suggest some fancier operations.

70
00:04:14,610 --> 00:04:20,760
You can use big int turn every number into an integer, do math with it and then convert it back to

71
00:04:20,760 --> 00:04:21,630
a floating point.

72
00:04:21,630 --> 00:04:22,800
We're not going to do that.

73
00:04:22,800 --> 00:04:29,820
But if that is a serious concern of really, really concerned about precision, then you could do some

74
00:04:29,820 --> 00:04:32,340
fancier operations and use big ints.

75
00:04:32,340 --> 00:04:38,880
Because any time we do math with decimals, any sort of decimal, not just the decimal type, but any

76
00:04:38,880 --> 00:04:45,060
numbers that have a decimal point and digits afterwards, we may risk losing precision.

77
00:04:45,060 --> 00:04:50,250
So if we instead turned everything into a whole number and then did math and then converted it back

78
00:04:50,250 --> 00:04:55,200
anyway, in this course, it's not going to matter if we use decimal versus float versus double.

79
00:04:55,230 --> 00:04:55,980
If you prefer.

80
00:04:55,980 --> 00:04:59,520
You can stick with decimal or stick with double or float.

81
00:04:59,520 --> 00:05:04,290
What does matter though, is remembering the distinction between float and double.

82
00:05:04,290 --> 00:05:12,390
Just that size difference where float is less precise, smaller space double is also not perfectly precise

83
00:05:12,390 --> 00:05:18,690
of course, but it's twice as precise as float or double as precise and takes up double the storage.

84
00:05:19,020 --> 00:05:20,640
All right, so that's enough with numbers.

85
00:05:20,640 --> 00:05:21,930
We've been in the weeds for a while.

86
00:05:21,930 --> 00:05:27,090
I know this has been kind of a dense technical section, not super exciting, but we are going to move

87
00:05:27,090 --> 00:05:29,400
on to talk about dates and times.

88
00:05:29,400 --> 00:05:31,860
And that actually is a little bit more interesting, I promise.

