JavaRush /Java Blog /Random EN /A little deeper about BigDecimal numbers
grishin
Level 27
Харьков

A little deeper about BigDecimal numbers

Published in the Random EN group
Finally, after a 3-month break, I’m returning to taking the course. I went on a break at the beginning of January. I then decided to switch to studying other disciplines, such as HTML/CSS/Javascript. Along the way, I started learning technologies (servlets and JSP). At that time, I had completed 24 levels and was a little stuck on the internal classes - not that anything was not clear, it was just that the knowledge gained had to be systematized, since too much of it had been acquired in a short period in the “gallop across Europe” mode. So, now I decided to carefully go through the first volume of Horstman to consolidate my knowledge of Java Core and at the same time complete the remaining tasks from the previous levels, and then move on through the levels. I started with the bonus problem from level 15 about factorial. Having dealt with it, I decided to take a deeper look at the BigDecimal class. I didn’t find any particularly popular articles on the internet, so I looked into the documentation and the first paragraph in it immediately made me think, somehow not everything in it was clear. I decided to get to the bottom of the truth, and so that my work would not be in vain, I formatted everything in the form of an article and now I am posting it to the community for the exchange of opinions, and maybe it will be of help to someone. So I took the first paragraph and broke it down into sentences. There are only 5 sentences in it. Sentences number 3 and 4 were underlined and moved to the end of the paragraph (why I did this - read on). And the translation was made accordingly in this changed order. Original first paragraph
  1. Immutable, arbitrary-precision signed decimal numbers.
  2. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale.
  3. If zero or positive, the scale is the number of digits to the right of the decimal point.
  4. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale.
  5. The value of the number represented by the BigDecimal is therefore ( unscaledValue × 10 -scale ).
Original with rearranged sentence order
  1. Immutable, arbitrary-precision signed decimal numbers.
  2. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale.
  3. The value of the number represented by the BigDecimal is therefore ( unscaledValue × 10 -scale ).
  4. If zero or positive, the scale is the number of digits to the right of the decimal point.
  5. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale.
Expanded translation (each point in the translation corresponds to a point in the original)
  1. These are immutable fractional 1 decimal numbers with a sign of 2 , of arbitrary length 34 .
  2. The BigDecimal number is specified by two parameters. The first of them, the so-called. unscaled value is an integer of unlimited length. Knowing only this parameter, one cannot say anything about the actual value of the BigDecimal number. To do this, you must know the second parameter - a 32-bit integer called scale.
  3. Then the value of the BigDecimal number can be calculated using the following formula: unscaledValue × 10 -scale
  4. If BigDecimal is zero or positive, the scale is the number of digits after the decimal point.
  5. If BigDecimal is negative, its unscaled value is multiplied by 10 raised to a power equal to scale with a minus sign.
Translation Notes
1. Fractional, because “arbitrary-precision” includes, among other things,
   that these are exact numbers, and exact numbers must have a decimal point,
   in other words - fractional.
2. That is can be both positive and negative.
3. Arbitrary length, because "arbitrary-precision" also means
   "An arbitrary number of digits."
4. In fact, BigDecimal numbers are real numbers. But installing
   different settings, you can use them not only in scientific calculations,
   and also in financial ones.
So, the underlined sentences were the main confusing factor for me - I would actually just remove them from the text, because in fact they are about the same thing, and besides, they are about the same thing that is said in sentence number 5. Those. three sentences in a row say only that unscaled value is multiplied by 10 raised to a power equal to scale with a minus sign. This is confusing when you first read it, because when reading sentence number 3, you understand that since there is if, it means there will be one thing here, and in the next sentence something else. But no, both sentences say the same thing, just in different words. Well, the third (last in the paragraph) sentence in a row about the same thing is probably like a control shot. It is unclear why the text was compiled in such a confusing way, because the formula unscaledValue × 10 -scale from the last sentence gives the definition of a large number as clearly, unambiguously and comprehensively as possible.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION