modal-121 / 05 - Operators and Operations with primitive types /002 Operations with integers and floating-point numbers, BigDecimal type_en.srt
Tan115's picture
Add files using upload-large-folder tool
ee218a7 verified
Raw
History Blame
6.06 kB
1
00:00:05,000 --> 00:00:10,000
Yes, students in this lesson, I would like to draw attention to some operations with integers and
2
00:00:10,000 --> 00:00:12,000
floating point numbers.
3
00:00:12,000 --> 00:00:14,000
Let's start with use this example.
4
00:00:14,000 --> 00:00:17,000
Imagine you want to divide two integers.
5
00:00:17,000 --> 00:00:23,000
Let's imagine you're implementing overapplication and you want to split the right charge between you
6
00:00:23,000 --> 00:00:23,000
and your friends.
7
00:00:24,000 --> 00:00:30,000
If right charge was 20 bucks and there were three of you, each of you should pay for six bucks.
8
00:00:32,000 --> 00:00:38,000
According to the results of our program, probably each of you will be happy because you all together
9
00:00:38,000 --> 00:00:42,000
saved two bucks, but Uber driver will hit you up.
10
00:00:43,000 --> 00:00:49,000
You need to remember that when you are dividing integers, you receive integer and there is no rounding
11
00:00:49,000 --> 00:00:50,000
fractional.
12
00:00:50,000 --> 00:00:55,000
Part of the result is just not taken into account what you can do about it now.
13
00:00:55,000 --> 00:01:00,000
Just change type of the option to double to get floating point number as a result.
14
00:01:02,000 --> 00:01:09,000
You see, I am custom integer to double here and right in the program, we'll run with you more about
15
00:01:09,000 --> 00:01:11,000
expression, type in the separate less than.
16
00:01:12,000 --> 00:01:18,000
Now you see double the result, but still this is something what the end user wouldn't expect and can't
17
00:01:18,000 --> 00:01:20,000
pay exactly this amount of money.
18
00:01:21,000 --> 00:01:22,000
So what to do in this case?
19
00:01:23,000 --> 00:01:26,000
The rule of thumb here is to use big decimal object.
20
00:01:27,000 --> 00:01:32,000
In previous lessons, we learned how to attach source code to eclipse and how to investigate the source
21
00:01:32,000 --> 00:01:33,000
code.
22
00:01:33,000 --> 00:01:37,000
So feel free to open Javadoc for that small type.
23
00:01:37,000 --> 00:01:37,000
To learn more.
24
00:01:39,000 --> 00:01:44,000
The big Desmonte class provides reparations for arithmetic, scale manipulation around in.
25
00:01:46,000 --> 00:01:53,000
Also, this class gives its user complete control of around in behavior, if no round in mode is specified
26
00:01:53,000 --> 00:01:57,000
and the exact result cannot be represented, an exception is thrown.
27
00:01:58,000 --> 00:02:00,000
Let's declare a variable of big decimal type here.
28
00:02:02,000 --> 00:02:09,000
To create object, object that small use, special method value of like this and calling value of method
29
00:02:09,000 --> 00:02:11,000
here of big decimal type.
30
00:02:12,000 --> 00:02:17,000
This message represents gradational architecture, Partan, which is called factory method.
31
00:02:17,000 --> 00:02:21,000
We'll discuss portions in the separate lesson for now.
32
00:02:21,000 --> 00:02:24,000
Just remember how to create an object of that small type.
33
00:02:25,000 --> 00:02:27,000
You can also set scale for this number.
34
00:02:27,000 --> 00:02:29,000
We are having to deal with money here.
35
00:02:29,000 --> 00:02:31,000
So let me set a scale of two.
36
00:02:32,000 --> 00:02:33,000
This is our right feet.
37
00:02:34,000 --> 00:02:37,000
Let's create a new variable which will contain amount of people.
38
00:02:37,000 --> 00:02:39,000
We have three people.
39
00:02:40,000 --> 00:02:46,000
And now let's divide right feet by amount of people without rounding more specified, we'll have arithmetic
40
00:02:46,000 --> 00:02:48,000
exception like this.
41
00:02:49,000 --> 00:02:54,000
Our program wants to tell us that we can't show the exact calculation result.
42
00:02:54,000 --> 00:02:59,000
Let's set up around in put call my hair in mode.
43
00:03:01,000 --> 00:03:01,000
Not.
44
00:03:03,000 --> 00:03:08,000
You can choose out of different rounded modes here, but let's select the one we learned at school.
45
00:03:09,000 --> 00:03:11,000
And now let's bring the result to console.
46
00:03:12,000 --> 00:03:13,000
And runs the program.
47
00:03:15,000 --> 00:03:18,000
And now we see the results, which we were expected to see.
48
00:03:20,000 --> 00:03:27,000
Let's also review operations with double imagine you have some double number three point one, and you
49
00:03:27,000 --> 00:03:32,000
need to subtract from it one point twenty one when you subtract double value from double value.
50
00:03:33,000 --> 00:03:34,000
You will see a result like this.
51
00:03:37,000 --> 00:03:44,000
The funny story that I order a pizza online recently and applied my bonuses as a result, I got no like
52
00:03:44,000 --> 00:03:44,000
this.
53
00:03:45,000 --> 00:03:49,000
I asked them how they would like me to pay this amount of money we love to meet.
54
00:03:49,000 --> 00:03:53,000
But still, as a programmer, you need to know what to do.
55
00:03:53,000 --> 00:03:53,000
In such cases.
56
00:03:54,000 --> 00:04:00,000
There are infinite number of real numbers in any range, but only a finite number of bits to represent
57
00:04:00,000 --> 00:04:00,000
them.
58
00:04:01,000 --> 00:04:05,000
For this reason, a rounding error is expected for doubles and flaws.
59
00:04:06,000 --> 00:04:12,000
The number you get is the closest number possible that can be represented by double in 14 points representation.
60
00:04:13,000 --> 00:04:15,000
Here's where Big Decimal was rounding.
61
00:04:15,000 --> 00:04:16,000
What will help us?
62
00:04:17,000 --> 00:04:23,000
Let's create the objects of big decimal here, do not forget to set the scale for them.
63
00:04:24,000 --> 00:04:30,000
And let's subtract this number from this one, I'm right in the program and here is the result.
64
00:04:31,000 --> 00:04:36,000
Today, you learned how to control precision in arithmetic operations and how to set up around in the
65
00:04:36,000 --> 00:04:37,000
rules.