0

i code a program in C#, this time i have to write in java and when i tried to add and subtract a simple number such like:

double array1 = new double array1[200];
double array2 = new double array2[200];
for (int var = 1; var < 200; var++)
        {
            array1[var] = Math.Round(array1[var] + 0.005,3);
            array2[var] = Math.Round(1 - array1[var],3);
        }

where the output is something like this:

array1[0]=0.005,array1[1]=0.010,array1[2]=0.015,array1[3]=0.020,array1[4]=0.025 ......
array2[0]=1.000,array2[1]=0.995,array2[2]=0.990,array2[3]=0.985,array2[4]=0.980 ......

when i tried to take to java i used the next code:

 double array1 = new double[200];
    double array2 = new double[200];
    for (int var = 1; var < 200; var++)
            {
                array1[var] = (array1[var] + 0.005);
                array2[var] = (1 - array1[var]);
            }

the output is the same, after the 6 one because of the imprecision of the double it starts to bring different result, i try to used BigDecimal how ever i still don't understand how to it works or how to add the result inside the array.

infused
  • 24,000
  • 13
  • 68
  • 78
JUAN
  • 523
  • 4
  • 10
  • 27
  • 2
    What does `(array1[var] + 0.005,3)` even mean? That's not valid code. (You're not calling a method.) – Jon Skeet Sep 26 '13 at 15:19
  • the first code is coded in C#, the math round makes that the result only bring certain number after the decimal point in this case after the sum i only want 3 digits after the decimal point – JUAN Sep 26 '13 at 15:23
  • Sounds like homework, if it is please tag it as such. – ubiquibacon Sep 26 '13 at 15:24
  • @JUAN: You've missed my point. The second code snippet is simply invalid. If you don't give us the actual code you're running, how can we help you? – Jon Skeet Sep 26 '13 at 15:25
  • just see the error in the explanation, and is not homework, it part of my job but i am new in java and i have more experience in C#, and this part i was not expecting – JUAN Sep 26 '13 at 15:29
  • Did you miss a Math.Round of the C# version? What is hoja3C anyway? And what's `new array1` or `new array2` supposed to do? – doctorlove Sep 26 '13 at 15:54

2 Answers2

0

no offense meant but you seriously have to go through java 101... 1st

double array1 = new array1[200];

is simply illegal.. i have no idea how it compiled, let alone giving the same result..

You are assigning an array to a primitive... Seriously wrong..!!!

2nd

new array1[200];

is another one

u cant have array1[200] unless array1 is a class name (a badly named one though)

check out some basic java tuts and then expiriment.. gud luck.. :)

Thirumalai Parthasarathi
  • 4,541
  • 1
  • 25
  • 43
  • sorry just some error while i was writing the question still this is not the part i need a answer, ass you said this is just 101 the basic but no the problem that i am explaining – JUAN Sep 26 '13 at 16:19
0

Precision is in BigDecimal parlance "scale" (setScale). However with a precision of 3, why not use long filled with double values * 1000, rounded.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138