简体   繁体   中英

Floating-point issue in R

I am doing colSums(x) of a matrix x in R

and to compare results I do it C#

the matrix has 1000 rows and 25 columns ans is something like:

0.438,0.498,3.625,3.645,5.000,2.918,5.000,2.351,2.332,2.643,1.698,1.687,1.698,1.717,1.744,0.593,0.502,0.493,0.504,0.445,0.431,0.444,0.440,0.429,1.0
0.438,0.498,3.625,3.648,5.000,2.918,5.000,2.637,2.332,2.649,1.695,1.687,1.695,1.720,1.744,0.592,0.502,0.493,0.504,0.449,0.431,0.444,0.443,0.429,1.0
0.438,0.498,3.625,3.629,5.000,2.918,5.000,2.637,2.334,2.643,1.696,1.687,1.695,1.717,1.744,0.593,0.502,0.493,0.504,0.449,0.431,0.444,0.446,0.429,1.0
0.437,0.501,3.625,3.626,5.000,2.918,5.000,2.353,2.334,2.642,1.730,1.687,1.695,1.717,1.744,0.593,0.502,0.493,0.504,0.449,0.431,0.444,0.444,0.429,1.0
0.438,0.498,3.626,3.629,5.000,2.918,5.000,2.640,2.334,2.639,1.696,1.687,1.695,1.717,1.744,0.592,0.502,0.493,0.504,0.449,0.431,0.444,0.441,0.429,1.0
0.439,0.498,3.626,3.629,5.000,2.918,5.000,2.633,2.334,2.645,1.705,1.686,1.694,1.719,1.744,0.589,0.502,0.493,0.504,0.446,0.431,0.444,0.444,0.430,1.0
0.440,5.000,3.627,3.628,5.000,2.919,3.028,2.346,2.330,2.638,1.727,1.684,1.692,1.714,1.745,0.588,0.501,0.492,0.504,0.451,0.433,0.446,0.444,0.432,1.0
0.444,5.021,3.631,3.634,5.000,2.919,5.000,2.626,2.327,2.638,1.698,1.680,1.688,1.709,1.740,0.595,0.500,0.491,0.503,0.453,0.436,0.448,0.444,0.436,1.0
0.451,5.025,3.635,3.639,5.000,2.920,3.027,2.620,2.323,2.632,1.706,1.673,1.681,1.703,0.753,0.595,0.499,0.491,0.502,0.457,0.440,0.453,0.454,0.442,2.0
0.458,5.022,3.640,3.644,5.000,2.922,5.000,2.346,2.321,2.628,1.688,1.666,1.674,1.696,0.744,0.590,0.496,0.490,0.498,0.462,0.444,0.458,0.461,0.449,2.0
0.465,0.525,3.646,3.670,5.000,2.923,5.000,2.611,2.315,2.631,1.674,1.658,1.666,1.688,0.735,0.593,0.495,0.488,0.497,0.467,0.449,0.462,0.469,0.457,2.0
0.473,0.533,3.652,3.676,5.000,2.925,5.000,2.607,2.310,2.623,1.669,1.651,1.659,1.684,0.729,0.578,0.496,0.487,0.498,0.469,0.454,0.467,0.476,0.465,2.0
0.481,0.544,3.658,3.678,5.000,2.926,5.000,2.606,2.303,2.619,1.668,1.643,1.651,1.275,0.723,0.581,0.495,0.486,0.497,0.477,0.459,0.472,0.484,0.472,2.0
0.484,0.544,3.661,3.665,5.000,2.928,5.000,2.321,2.304,5.022,1.647,1.639,1.646,1.270,0.757,0.623,0.493,0.484,0.495,0.480,0.461,0.474,0.485,0.476,2.0
0.484,0.532,3.669,3.662,2.945,2.926,5.000,2.326,2.306,2.620,1.648,1.639,1.646,1.270,0.760,0.533,0.493,0.483,0.494,0.507,0.461,0.473,0.486,0.476,2.0
0.482,0.520,3.685,3.664,2.952,2.927,5.000,2.981,2.307,2.329,1.650,1.640,1.644,1.268,0.757,0.533,0.492,0.482,0.492,0.513,0.459,0.474,0.485,0.474,2.0
0.481,0.522,3.682,3.661,2.955,2.927,2.957,2.984,1.700,2.622,1.651,1.641,1.645,1.272,0.761,0.530,0.492,0.482,0.492,0.513,0.462,0.486,0.483,0.473,2.0
0.480,0.525,3.694,3.664,2.948,2.926,2.950,2.995,1.697,2.619,1.651,1.642,1.646,1.269,0.762,0.530,0.493,0.482,0.492,0.516,0.462,0.486,0.483,0.473,2.0
0.481,0.515,5.018,3.664,2.956,2.927,2.947,2.993,1.697,2.622,1.651,1.641,1.645,1.269,0.765,0.592,0.489,0.482,0.495,0.531,0.462,0.499,0.483,0.473,2.0
0.479,5.000,3.696,3.661,2.953,2.927,2.944,2.993,1.702,2.622,1.649,1.642,1.645,1.269,0.812,0.588,0.489,0.481,0.491,0.510,0.462,0.481,0.483,0.473,2.0
0.480,0.506,5.019,3.665,2.941,2.929,2.945,2.981,1.700,2.616,1.652,1.642,1.645,1.271,0.814,0.643,0.491,0.480,0.493,0.524,0.461,0.469,0.484,0.473,2.0
0.479,5.000,5.019,3.661,2.943,2.930,2.942,2.996,1.698,2.312,1.653,1.642,1.644,1.274,0.811,0.617,0.491,0.479,0.491,0.575,0.461,0.465,0.484,0.473,2.0
0.479,5.000,5.020,3.662,2.945,2.931,2.942,2.997,1.700,2.313,1.654,1.642,1.644,1.270,0.908,0.616,0.490,0.478,0.489,0.503,0.460,0.460,0.478,0.473,1.0
0.481,0.508,5.021,3.660,2.954,2.936,2.946,2.966,1.705,2.313,1.654,1.643,1.643,1.270,1.689,0.678,0.493,0.477,0.483,0.497,0.467,0.459,0.476,0.473,1.0
0.486,0.510,0.522,3.662,2.958,2.938,2.939,2.627,1.707,2.314,1.659,1.643,1.639,1.665,1.702,0.696,0.516,0.476,0.477,0.547,0.465,0.457,0.470,0.474,1.0

R displays this after colSums(x):

1463.739 2286.797 2624.315 2798.060 3003.597 2985.547 3322.785 2670.472 3261.508 3031.385 2614.597 1981.419 2185.030 2252.883 2320.446 1273.813 1020.814  907.378 1096.219 1044.184 1001.622 1915.268 1514.871 1515.182 2297.000 

which is correct but the decimal point is moved 3 positions I know this because in C# solution I get:

 1: 1463739
 2: 2286797
 3: 2624315
 4: 2798060
 5: 3003597
 6: 2985547
 7: 3322785
 8: 2670472
 9: 3261508
 10: 3031385
 11: 2614597
 12: 1981419
 13: 2185030
 14: 2252883
 15: 2320446
 16: 1273813
 17: 1020814
 18: 907378
 19: 1096219
 20: 1044184
 21: 1001622
 22: 1915268
 23: 1514871
 24: 1515182
 25: 22970

why is this happening? to read dataset I do

x <- read.csv( "C:\\x.txt", header=T , row.names=NULL)

in C#

double[]  L  = new double[25];
for ( i = 0; i < dimension; i++){
   array[i] += reader.GetDouble(i);
}

The correct values are those in C# .

I think you need to check the CultureInfo , may be that . rappresent thousand separator not decimal separator in your Culture .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM