Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Why is the scale field of a BigDecimal not a BigInteger? I assume currently it makes maybe no sense because calculations with that many decimal places are likely never performed, but wouldn't it make sense for the future to rather use a BigInteger?
The scale of a BigDecimal is the number of digits it stores to the right of the decimal point. It is an amount of memory in that those digits are actually stored, and it is an amount of work in that most operations on a BigDecimal will have to do work on all of those digits.
It is never going to be a good idea to use a BigDecimal that takes an amount of memory or work that doesn't fit into an int, so an int is used for scale. That's the same reason int is used for string lengths and collection sizes, etc.
In the rare cases that a reasonable amount of memory or work doesn't fit into an int, it certainly fits into a long. Longs are used for file sizes and position, for example. A BigInteger is never required.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I'm a beginner in java and our professor avoided discussing BinInteger and BigDecimal classes from java.math package.I wonder why.Are they not that useful?When exactly we must need to use BigInteger?
You don't need them particularly often, but when you do need them you really need them. You really only need them when you need to actually store arbitrary precision integers or real numbers. long goes up to 263-1, which is a pretty big number.
Always use the primitives when possible because:
They have operators rather than methods, so code is easier to read/write.
They are a lot more efficient.
long, the largest primitive integer type, has a maximum value of 9,223,372,036,854,775,807, or 2^63 - 1, and a minimum value of -2^63.
double, the most precise primitive floating point type, has 64 bits of precision, which is a lot.
However, if you really need an arbitrarily large/small integer or arbitrarily precise decimal number, the BigInteger and BigDecimal types are appropriate. Such scenarios aren't that common, however, which is probably why your professor didn't discuss these types.
Decimal data types are essential when dealing with currencies
The primitive types of int and long have a limited range of values they can represent. The same is true for the floating point primitives float and double. There however you also face the issue of a limited precision. For many cases this does not pose any problem however when larger numbers or exact precision is required (e.g. in a banking application you will want to be as precise as possible) you will use BigInteger and BigDecimal however.
BIGINT is always the product of two Ints.
Example 99X99 = 10000 upto twice as big.
Both of the libraries you mentioned has their uses, if they didn't they wouldn't exist. However, your teacher probably elected not to discuss them as you probably wont be using them in your specific course. (He has to put a limit somewhere, you cannot cover the entirety of Java libs in one course.)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to take a sum, quotient, remainder of two numbers using an array in java.
123456789012345+7654321, 123456789012345/7654321. What is a simplest way to calculate it using Java?(I am new to Java.)
Since you are new to java I recommend reading up on some tutorials. As it seems you are not familiar with java in general. An example, which I have not used myself, is http://www.javaworld.com/blog/java-101. It may be worth your time to read this over.
As for your actual question, you would create a variable in java. Then assign your first number to this variable. After doing this, you can perform some operations on the number.
An example in sudo code to give you an idea while not doing the work for you.
void method
var number = 100
number = number + 200
number = number / 20
print("result" . number)
If you plan to use an array its the same process in a loop.
http://www.tutorialspoint.com/java/java_loop_control.htm
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Can anyone explain me how modular arithmetic works in programming? I know it is used to operate on large values.
For example, to calculate the binomial coefficient of B(1000000, 2) using int data-type. i assume we couldn't multiply using int data-type, since it involves calculating factorials of big values like 1000000! which has millions of digits, which don't fit in an 32-bit or 64-bit integer.
I know modular arithmetic is used to these type of problems, But i don't understand exactly how that works.
The modulo operation is a simple operation that calculates the remainder of a division.
For instance
5 % 3 = 2 as dividing 5 by 3 will give you a a remainder of .
A common usecase for this is checking whether a number is even or odd.
number % 2 == 0 means the number is even.
For more information please check Wikipedia.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have been storing date records (Date().getTime()) in an SQLite database with an Integer type. I realize now that Date().getTime() is returning Long values, not Integer.
Is there any way I can rescue the date data that is already stored in the database? Going forward I can reduce the resolution of the time, to make it fit into an integer. (ie. divide by 1000, and cast to int)
I expect that forcing a long value into an integer space has truncated the most significant digits - which might work in my favour, as the dates in question have all occurred within the past 6 months, so can probably be calculated.
question
so, how exactly would the long representation of today's date map onto an integer, and how might I use that knowledge (combined with the restricted time range) to build these integers back into their original long values?
Any suggestions?
Turns out the SQLite database integer type is perfectly capable of storing long values. So the data rescue was unnecessary in the end.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Why do most programmers avoid using the float and long data types in their video tutorials?
Is it only to avoid the "0000f" and "0000L" notation?
I avoid float has it has poor precision. I would rather use double (or long with fixed precision or if I have to BigDecimal).
I suspect long is not often used as int is usually enough and many Java APIs only accept int values. e.g. array sizes and Collection/Map size() must be int.
And why should they use them? Perhaps the one reason is to avoid writing a letter indicating that the float/long type is used.
However if you don't need any special precision, why use double instead of float or long instead of int?