3

There is a class written by Jama Matrix in Java. The class is like that

public Matrix (int m, int n, double s) {
  this.m = m;
  this.n = n;
  A = new double[m][n];
  for (int i = 0; i < m; i++) {
     for (int j = 0; j < n; j++) {
        A[i][j] = s;
     }
  }
}

It can create a matrix with m*n dimension whose datatype is double that means it take 6 digit after point(.). But I need a matrix which can take minimum 10 digit after point(like 12.1234567890). So after searching I find BigDecimal data type which can take this type of value. So I slightly modify the previous code.

 public Matrix(int m,int n,BigDecimal s){
   this.m=m;
   this.n=n;
   A= new BigDecimal[m][n];
   for(int i=0;i<m;i++){
       for(int j=0;j<n;j++){
           A[i][j]=s;
       }
   }
}

But it throws error. Is there any other datatype which is used in java for floating point number.

I am little bit confused when run this code

 public class T {
public static void main(String args[]){
    double a= 3.256147001235;
    double b=4.200001258920;
    double c=a+b;
    System.out.println(c);
}
 }

Here datatype is also double but the output is 7.456148260155. So here it take this big number but in matrix class it cannot take this big number.

Saswati
  • 192
  • 8
  • 2
    "It throws error": what error? Please provide a [mcve]. – Daniel Pryden Jun 06 '18 at 18:42
  • Change code as ` A[i][j] = s.clone();` BigDecimal is a class – Victor Gubin Jun 06 '18 at 18:44
  • 2
    FWIW a `double` shouldn't have any problem storing 15 total decimal digits (regardless of where the decimal point is placed in that range). However, the 16th digit cannot be stored exactly, which may mean that values won't round-trip to decimal exactly. (Do you need 10 digits discrete, or is this a 10-place precision sample of a continuous value, where an error of 1e-16 is not a problem?) – Daniel Pryden Jun 06 '18 at 18:46
  • 2
    @VictorGubin: `BigDecimal` is an immutable type. There is no need to clone the object for each entry in the array; it's fine to have a million references to e.g. `BigDecimal.ZERO`, since it will never "become" another number. – Daniel Pryden Jun 06 '18 at 18:47
  • Question is actually duplicate of https://stackoverflow.com/questions/36344758/how-to-determine-the-max-precision-for-double/36345535 – Victor Gubin Jun 06 '18 at 18:54
  • @DanielPryden It throws error `A= new BigDecimal[m][n];` and ` A[i][j]=s;` in this line. Error is: Incompatible types. `Required`: double[][] `Found`: java.math.BigDecimal[] – Saswati Jun 06 '18 at 19:30
  • 1
    @Saswati This error can be fixed by declaring `A` as `BigDecimal[][] A` at the class level. – Sergey Kalinichenko Jun 06 '18 at 19:49
  • @dasblinkenlight oh the method you told is work fine. Previous error is solve. A new problem created when I call this class in another file it take that method which content double value.`public Matrix (int m, int n, double s)`. Don't take my created method that is `public Matrix (int m, int n, BigDecimal s)`. Then I disable those method which content double s. Then it throw a error when I call this method into another class file.Error: `Cannot resolve constructor 'Matrix(int,int,double)` . – Saswati Jun 06 '18 at 20:38

1 Answers1

4

Unlike double or String, BigDecimal does not have built-in support from Java compiler for constructing objects with literals. You need to construct it either with a constructor or by calling valueOf, depending on the source of data:

Matrix bigDecimalMatrix = new Matrix(100, 80, BigDecimal.valueOf(4.200001258920));

or

Matrix bigDecimalMatrix = new Matrix(100, 80, new BigDecimal("4.200001258920"));

Similarly, BigDecimal does not have compiler support for running arithmetic operations. Hence, instead of

double c = a + b;

you would need to write

BigDecimal c = a.add(b);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • The question doesn't actually show an attempt to initialize a `BigDecimal` parameter using a literal. But I guess it's a reasonable guess based on the premise of the question. – Daniel Pryden Jun 06 '18 at 18:48
  • @DanielPryden When I encountered `BigDecimal` for the first time I had the same kind of reaction to it: I had no idea how to make new `BigDecimal`s or to add/subtract/divide them. That's why I suspect that's what OP needs to solve his problem. – Sergey Kalinichenko Jun 06 '18 at 18:52
  • @dasblinkenlight No I don't want to know the method how to add up the two BigDecimal number. I want to know in regular arithmetic operation double can take 10digit after point(.) but when i create a matrix of double type why does it take only 6 digit after(.). And is there any better option besides bigdecimal to represent huge floating number? – Saswati Jun 06 '18 at 19:35
  • @Saswati The number of meaningful digits after the dot in a `double` depends on the number of digits before the dot, because of the way `double`s are represented. `BigDecimal` does not have this problem, it gives you unlimited precision. The part about "only six digits after dot" does not make any sense, sorry. – Sergey Kalinichenko Jun 06 '18 at 19:51