1

I have a bit of a head scratcher here.

I have a code snippet that reads certain columns from a text file and saves them in a vector. I have compiled this code on two different machines, using as far as I can tell the same compiler and the same flags. On system A, the columns are read in correctly and accurate reflect the original text file. On system B, one column is fine, and the other is fine for the first ~80% of the column, but then row by row it starts to lose precision and the last few rows are all zeros. So much about this is weird to me.

The compiler versions on each system are different. On system A it is g++ 7.4.0 and on system B it is g++ 4.8.5.

Here is a code snippet describing the above. I will stress that this snippet is not a minimal working example since this is all part of a larger program and its somewhat tedious to pull pieces out (plus, given that this program works on certain machines, I am not sure it would even help. Let me know if it would).

#include <stdio.h>
#include <stdlib.h>
#include <cmath>

enum FILE_COLUMNS{
    COL_A,
    COL_B,
    NUM_COLS,
};

int main(){

    ///stuff before here

    std::stringstream inputFile;

    //function call that reads a file line by line into the above stringstream

    std::vector<double> cola;
    std::vector<double> colb;

    double inputLine[NUM_COLS];
    while(!inputFile.eof()){
        for(int j=0; j<NUM_COLS; j++){
            inputFile >> inputLine[j];
        }

        cola.push_back(inputLine[COL_A]);
        colb.push_back(abs(inputLine[COL_B]));
    }
    for(int i=0; i<cola.size(); i++){
        std::cout<<cola.at(i)<<" "<<colb.at(i)<<std::endl;        
    } //copy this to a text file
}

As you can see, pretty standard.

Now, when I compile and run this on system A and system B, and diff the outputs, I get:

288,291c288,291
< 8.555120e-01 9.975880e+04
< 9.212890e-01 8.810480e+04
< 9.921240e-01 7.768050e+04
< 1.068400e+00 6.834660e+04
---
> 8.555120e-01 9.975800e+04
> 9.212890e-01 8.810400e+04
> 9.921240e-01 7.768000e+04
> 1.068400e+00 6.834600e+04
293,294c293,294
< 1.239010e+00 5.245460e+04
< 1.334280e+00 4.567180e+04
---
> 1.239010e+00 5.245400e+04
> 1.334280e+00 4.567100e+04
296,363c296,363
< 1.547340e+00 3.394440e+04
< 1.666310e+00 2.883040e+04
< 1.794430e+00 2.411980e+04
< 1.932390e+00 1.975690e+04
< 2.080970e+00 1.570320e+04
< 2.240970e+00 1.194440e+04
< 2.413270e+00 8.498740e+03
< 2.598820e+00 5.425040e+03
< 2.798630e+00 2.826230e+03
< 3.013810e+00 8.386150e+02
< 3.245530e+00 4.064980e+02
< 3.495070e+00 8.593010e+02
< 3.763790e+00 6.517280e+02
< 4.053170e+00 1.481500e+02
< 4.364810e+00 1.836440e+02
< 4.700400e+00 1.236170e+02
< 5.061800e+00 6.031890e+01
< 5.450990e+00 2.895470e+01
< 5.870090e+00 3.426040e+01
< 6.321420e+00 1.841130e+01
< 6.807460e+00 9.602120e+00
< 7.330860e+00 6.440940e+00
< 7.894500e+00 3.559000e+00
< 8.501490e+00 4.236020e-01
< 9.155140e+00 4.947580e-01
< 9.859040e+00 1.843450e-01
< 1.061710e+01 1.562160e-01
< 1.143340e+01 1.495830e-01
< 1.231250e+01 7.880730e-02
< 1.325910e+01 1.863010e-02
< 1.427860e+01 8.453570e-03
< 1.537640e+01 1.053520e-02
< 1.655860e+01 3.751260e-03
< 1.783180e+01 2.555030e-03
< 1.920280e+01 1.741550e-03
< 2.067920e+01 6.329080e-04
< 2.226920e+01 2.502660e-04
< 2.398140e+01 1.166210e-04
< 2.582520e+01 1.140960e-04
< 2.781080e+01 2.413880e-04
< 2.994910e+01 1.226810e-04
< 3.225180e+01 5.613870e-05
< 3.473150e+01 4.378650e-05
< 3.740190e+01 3.691210e-05
< 4.027760e+01 2.556930e-05
< 4.337440e+01 1.277570e-05
< 4.670940e+01 7.803100e-06
< 5.030070e+01 3.035830e-06
< 5.416810e+01 1.209200e-05
< 5.833290e+01 3.022850e-06
< 6.281800e+01 1.327160e-06
< 6.764780e+01 6.414590e-06
< 7.284900e+01 5.188940e-06
< 7.845010e+01 1.751470e-06
< 8.448190e+01 1.948600e-06
< 9.097740e+01 1.293250e-06
< 9.797240e+01 1.121840e-07
< 1.055050e+02 1.361170e-06
< 1.136170e+02 1.068700e-06
< 1.223530e+02 6.928450e-07
< 1.317600e+02 6.505850e-07
< 1.418910e+02 3.639500e-07
< 1.528000e+02 2.822170e-07
< 1.645480e+02 2.694410e-07
< 1.772000e+02 1.293620e-07
< 1.908240e+02 1.733030e-07
< 2.054960e+02 1.355590e-07
< 2.054960e+02 1.355590e-07
---
> 1.547340e+00 3.394400e+04
> 1.666310e+00 2.883000e+04
> 1.794430e+00 2.411900e+04
> 1.932390e+00 1.975600e+04
> 2.080970e+00 1.570300e+04
> 2.240970e+00 1.194400e+04
> 2.413270e+00 8.498000e+03
> 2.598820e+00 5.425000e+03
> 2.798630e+00 2.826000e+03
> 3.013810e+00 8.380000e+02
> 3.245530e+00 4.060000e+02
> 3.495070e+00 8.590000e+02
> 3.763790e+00 6.510000e+02
> 4.053170e+00 1.480000e+02
> 4.364810e+00 1.830000e+02
> 4.700400e+00 1.230000e+02
> 5.061800e+00 6.000000e+01
> 5.450990e+00 2.800000e+01
> 5.870090e+00 3.400000e+01
> 6.321420e+00 1.800000e+01
> 6.807460e+00 9.000000e+00
> 7.330860e+00 6.000000e+00
> 7.894500e+00 3.000000e+00
> 8.501490e+00 0.000000e+00
> 9.155140e+00 0.000000e+00
> 9.859040e+00 0.000000e+00
> 1.061710e+01 0.000000e+00
> 1.143340e+01 0.000000e+00
> 1.231250e+01 0.000000e+00
> 1.325910e+01 0.000000e+00
> 1.427860e+01 0.000000e+00
> 1.537640e+01 0.000000e+00
> 1.655860e+01 0.000000e+00
> 1.783180e+01 0.000000e+00
> 1.920280e+01 0.000000e+00
> 2.067920e+01 0.000000e+00
> 2.226920e+01 0.000000e+00
> 2.398140e+01 0.000000e+00
> 2.582520e+01 0.000000e+00
> 2.781080e+01 0.000000e+00
> 2.994910e+01 0.000000e+00
> 3.225180e+01 0.000000e+00
> 3.473150e+01 0.000000e+00
> 3.740190e+01 0.000000e+00
> 4.027760e+01 0.000000e+00
> 4.337440e+01 0.000000e+00
> 4.670940e+01 0.000000e+00
> 5.030070e+01 0.000000e+00
> 5.416810e+01 0.000000e+00
> 5.833290e+01 0.000000e+00
> 6.281800e+01 0.000000e+00
> 6.764780e+01 0.000000e+00
> 7.284900e+01 0.000000e+00
> 7.845010e+01 0.000000e+00
> 8.448190e+01 0.000000e+00
> 9.097740e+01 0.000000e+00
> 9.797240e+01 0.000000e+00
> 1.055050e+02 0.000000e+00
> 1.136170e+02 0.000000e+00
> 1.223530e+02 0.000000e+00
> 1.317600e+02 0.000000e+00
> 1.418910e+02 0.000000e+00
> 1.528000e+02 0.000000e+00
> 1.645480e+02 0.000000e+00
> 1.772000e+02 0.000000e+00
> 1.908240e+02 0.000000e+00
> 2.054960e+02 0.000000e+00
> 2.054960e+02 0.000000e+00

As you can see, the first column is fine, but the second column starts to diverge around line 290 out of 362.

Does anyone know any reason that this might be happening? It looks like in the second column I gradually lose precision (progressively more trailing 0s) until I just have 0, but I have no clue why this would be the case. I suspect this might be a machine specific thing so it's possible there is not a known answer. Looking for any suggestion here. Of course, let me know how I can be more clear.

Thanks

EDIT: This is C++11 and the compiler used is mpicxx with flags

-O3 -std=c++11 -fopenmp -MMD -MP

The input file is:

  0.704225E-04 0.203590E+08
  0.786985E-04 0.203586E+08
  0.879471E-04 0.203581E+08
  0.982826E-04 0.203575E+08
  0.109833E-03 0.203568E+08
  0.122740E-03 0.203558E+08
  0.137164E-03 0.203547E+08
  0.153284E-03 0.203532E+08
  0.171298E-03 0.203514E+08
  0.191428E-03 0.203492E+08
  0.213925E-03 0.203464E+08
  0.239065E-03 0.203430E+08
  0.267160E-03 0.203387E+08
  0.298556E-03 0.203335E+08
  0.333642E-03 0.203270E+08
  0.372851E-03 0.203190E+08
  0.416669E-03 0.203092E+08
  0.465635E-03 0.202972E+08
  0.520356E-03 0.202827E+08
  0.581508E-03 0.202651E+08
  0.649846E-03 0.202440E+08
  0.726215E-03 0.202186E+08
  0.811560E-03 0.201885E+08
  0.906933E-03 0.201528E+08
  0.101352E-02 0.201108E+08
  0.113262E-02 0.200614E+08
  0.126573E-02 0.200038E+08
  0.141447E-02 0.199366E+08
  0.158070E-02 0.198586E+08
  0.176646E-02 0.197684E+08
  0.197406E-02 0.196645E+08
  0.220605E-02 0.195452E+08
  0.246530E-02 0.194089E+08
  0.275502E-02 0.192537E+08
  0.307879E-02 0.190776E+08
  0.344060E-02 0.188788E+08
  0.384494E-02 0.186552E+08
  0.429679E-02 0.184048E+08
  0.441782E-02 0.183378E+08
  0.454226E-02 0.182689E+08
  0.467020E-02 0.181982E+08
  0.480175E-02 0.181255E+08
  0.493700E-02 0.180510E+08
  0.507606E-02 0.179745E+08
  0.521904E-02 0.178960E+08
  0.536605E-02 0.178154E+08
  0.551719E-02 0.177328E+08
  0.567260E-02 0.176482E+08
  0.583238E-02 0.175614E+08
  0.599666E-02 0.174724E+08
  0.616557E-02 0.173813E+08
  0.633923E-02 0.172880E+08
  0.651779E-02 0.171924E+08
  0.670138E-02 0.170946E+08
  0.689014E-02 0.169944E+08
  0.708422E-02 0.168920E+08
  0.728376E-02 0.167872E+08
  0.748892E-02 0.166799E+08
  0.769986E-02 0.165703E+08
  0.791675E-02 0.164582E+08
  0.813974E-02 0.163437E+08
  0.836901E-02 0.162266E+08
  0.860474E-02 0.161070E+08
  0.884711E-02 0.159848E+08
  0.909631E-02 0.158601E+08
  0.935253E-02 0.157327E+08
  0.961596E-02 0.156026E+08
  0.988682E-02 0.154699E+08
  0.101653E-01 0.153345E+08
  0.104516E-01 0.151964E+08
  0.107460E-01 0.150554E+08
  0.110487E-01 0.149118E+08
  0.113599E-01 0.147653E+08
  0.116799E-01 0.146160E+08
  0.120089E-01 0.144638E+08
  0.123471E-01 0.143088E+08
  0.126949E-01 0.141509E+08
  0.130525E-01 0.139901E+08
  0.134202E-01 0.138263E+08
  0.137982E-01 0.136597E+08
  0.141868E-01 0.134901E+08
  0.145864E-01 0.133177E+08
  0.149973E-01 0.131423E+08
  0.154197E-01 0.129640E+08
  0.158540E-01 0.127829E+08
  0.163006E-01 0.125988E+08
  0.167598E-01 0.124120E+08
  0.172318E-01 0.122223E+08
  0.177172E-01 0.120300E+08
  0.182162E-01 0.118349E+08
  0.187293E-01 0.116373E+08
  0.192569E-01 0.114371E+08
  0.197993E-01 0.112345E+08
  0.203570E-01 0.110296E+08
  0.209304E-01 0.108226E+08
  0.215199E-01 0.106135E+08
  0.221261E-01 0.104027E+08
  0.227493E-01 0.101901E+08
  0.233901E-01 0.997614E+07
  0.240490E-01 0.976098E+07
  0.247263E-01 0.954488E+07
  0.254228E-01 0.932812E+07
  0.261389E-01 0.911102E+07
  0.268752E-01 0.889389E+07
  0.276322E-01 0.867712E+07
  0.284105E-01 0.846107E+07
  0.292107E-01 0.824614E+07
  0.300335E-01 0.803277E+07
  0.308795E-01 0.782138E+07
  0.317493E-01 0.761245E+07
  0.326435E-01 0.740644E+07
  0.335630E-01 0.720381E+07
  0.345084E-01 0.700502E+07
  0.354804E-01 0.681054E+07
  0.364798E-01 0.662083E+07
  0.375073E-01 0.643636E+07
  0.385638E-01 0.625756E+07
  0.396500E-01 0.608473E+07
  0.407668E-01 0.591813E+07
  0.419151E-01 0.575800E+07
  0.430958E-01 0.560458E+07
  0.443096E-01 0.545796E+07
  0.455577E-01 0.531800E+07
  0.468410E-01 0.518459E+07
  0.481603E-01 0.505745E+07
  0.495169E-01 0.493638E+07
  0.509116E-01 0.482081E+07
  0.523457E-01 0.471013E+07
  0.538201E-01 0.460360E+07
  0.553361E-01 0.450042E+07
  0.569010E-01 0.440119E+07
  0.584659E-01 0.430427E+07
  0.600309E-01 0.421043E+07
  0.615958E-01 0.411870E+07
  0.631608E-01 0.402822E+07
  0.647257E-01 0.393846E+07
  0.662907E-01 0.384895E+07
  0.678556E-01 0.375938E+07
  0.694206E-01 0.366963E+07
  0.709855E-01 0.357969E+07
  0.725505E-01 0.348969E+07
  0.741154E-01 0.339984E+07
  0.756803E-01 0.331039E+07
  0.772453E-01 0.322172E+07
  0.788102E-01 0.313427E+07
  0.803752E-01 0.304842E+07
  0.819401E-01 0.296462E+07
  0.835051E-01 0.288324E+07
  0.850700E-01 0.280472E+07
  0.866350E-01 0.272936E+07
  0.881999E-01 0.265752E+07
  0.897648E-01 0.258940E+07
  0.913298E-01 0.252520E+07
  0.928947E-01 0.246499E+07
  0.944597E-01 0.240882E+07
  0.960246E-01 0.235664E+07
  0.975896E-01 0.230836E+07
  0.991545E-01 0.226381E+07
  0.100719E+00 0.222276E+07
  0.102284E+00 0.218494E+07
  0.103849E+00 0.215005E+07
  0.105414E+00 0.211774E+07
  0.106979E+00 0.208770E+07
  0.108544E+00 0.205951E+07
  0.110109E+00 0.203278E+07
  0.111674E+00 0.200721E+07
  0.113239E+00 0.198239E+07
  0.114804E+00 0.195804E+07
  0.116369E+00 0.193387E+07
  0.117934E+00 0.190964E+07
  0.119499E+00 0.188515E+07
  0.121064E+00 0.186023E+07
  0.122629E+00 0.183476E+07
  0.124194E+00 0.180864E+07
  0.125759E+00 0.178184E+07
  0.127324E+00 0.175436E+07
  0.128888E+00 0.172626E+07
  0.130453E+00 0.169762E+07
  0.132018E+00 0.166855E+07
  0.133583E+00 0.163919E+07
  0.135148E+00 0.160969E+07
  0.136713E+00 0.158022E+07
  0.138278E+00 0.155095E+07
  0.139843E+00 0.152204E+07
  0.141408E+00 0.149364E+07
  0.142973E+00 0.146591E+07
  0.144538E+00 0.143903E+07
  0.146103E+00 0.141310E+07
  0.147668E+00 0.138828E+07
  0.149233E+00 0.136460E+07
  0.150798E+00 0.134211E+07
  0.152363E+00 0.132094E+07
  0.153928E+00 0.130100E+07
  0.155493E+00 0.128232E+07
  0.157057E+00 0.126487E+07
  0.158622E+00 0.124859E+07
  0.160187E+00 0.123338E+07
  0.161752E+00 0.121918E+07
  0.163317E+00 0.120588E+07
  0.164882E+00 0.119337E+07
  0.166447E+00 0.118154E+07
  0.168012E+00 0.117024E+07
  0.169577E+00 0.115936E+07
  0.171142E+00 0.114877E+07
  0.172707E+00 0.113836E+07
  0.174272E+00 0.112802E+07
  0.175837E+00 0.111769E+07
  0.177402E+00 0.110726E+07
  0.178967E+00 0.109667E+07
  0.180532E+00 0.108585E+07
  0.182097E+00 0.107479E+07
  0.183662E+00 0.106344E+07
  0.185227E+00 0.105180E+07
  0.186791E+00 0.103987E+07
  0.188356E+00 0.102769E+07
  0.189921E+00 0.101526E+07
  0.191486E+00 0.100265E+07
  0.193051E+00 0.989898E+06
  0.194616E+00 0.977062E+06
  0.196181E+00 0.964219E+06
  0.197746E+00 0.951396E+06
  0.199311E+00 0.938649E+06
  0.200876E+00 0.926079E+06
  0.202441E+00 0.913736E+06
  0.204006E+00 0.901668E+06
  0.206690E+00 0.881723E+06
  0.209410E+00 0.862762E+06
  0.212165E+00 0.844887E+06
  0.214957E+00 0.828208E+06
  0.217785E+00 0.812764E+06
  0.220651E+00 0.798506E+06
  0.223554E+00 0.785305E+06
  0.226495E+00 0.772960E+06
  0.229475E+00 0.761215E+06
  0.232495E+00 0.749796E+06
  0.235554E+00 0.738447E+06
  0.238653E+00 0.726926E+06
  0.241793E+00 0.715031E+06
  0.244975E+00 0.702647E+06
  0.248198E+00 0.689716E+06
  0.251464E+00 0.676295E+06
  0.254772E+00 0.662494E+06
  0.258125E+00 0.648487E+06
  0.261521E+00 0.634480E+06
  0.264962E+00 0.620721E+06
  0.268448E+00 0.607413E+06
  0.271980E+00 0.594729E+06
  0.275559E+00 0.582773E+06
  0.279185E+00 0.571578E+06
  0.282858E+00 0.561106E+06
  0.286580E+00 0.551242E+06
  0.290351E+00 0.541818E+06
  0.294171E+00 0.532649E+06
  0.298042E+00 0.523554E+06
  0.301963E+00 0.514367E+06
  0.305936E+00 0.504979E+06
  0.309962E+00 0.495361E+06
  0.314040E+00 0.485538E+06
  0.318172E+00 0.475587E+06
  0.322359E+00 0.465647E+06
  0.326600E+00 0.455864E+06
  0.330898E+00 0.446370E+06
  0.335251E+00 0.437279E+06
  0.339663E+00 0.428600E+06
  0.344132E+00 0.420367E+06
  0.348660E+00 0.412500E+06
  0.353247E+00 0.404905E+06
  0.357895E+00 0.397467E+06
  0.362604E+00 0.390073E+06
  0.367375E+00 0.382652E+06
  0.372209E+00 0.375178E+06
  0.377107E+00 0.367655E+06
  0.382068E+00 0.360147E+06
  0.387096E+00 0.352727E+06
  0.392189E+00 0.345474E+06
  0.397349E+00 0.338452E+06
  0.402577E+00 0.331687E+06
  0.407874E+00 0.325169E+06
  0.439234E+00 0.290629E+06
  0.473006E+00 0.258753E+06
  0.509373E+00 0.230541E+06
  0.548537E+00 0.205114E+06
  0.590713E+00 0.182313E+06
  0.636130E+00 0.161941E+06
  0.685040E+00 0.143697E+06
  0.737711E+00 0.127382E+06
  0.794431E+00 0.112797E+06
  0.855512E+00 0.997588E+05
  0.921289E+00 0.881048E+05
  0.992124E+00 0.776805E+05
  0.106840E+01 0.683466E+05
  0.115055E+01 0.599770E+05
  0.123901E+01 0.524546E+05
  0.133428E+01 0.456718E+05
  0.143686E+01 0.395320E+05
  0.154734E+01 0.339444E+05
  0.166631E+01 0.288304E+05
  0.179443E+01 0.241198E+05
  0.193239E+01 0.197569E+05
  0.208097E+01 0.157032E+05
  0.224097E+01 0.119444E+05
  0.241327E+01 0.849874E+04
  0.259882E+01 0.542504E+04
  0.279863E+01 0.282623E+04
  0.301381E+01 0.838615E+03
  0.324553E+01-0.406498E+03
  0.349507E+01-0.859301E+03
  0.376379E+01-0.651728E+03
  0.405317E+01-0.148150E+03
  0.436481E+01 0.183644E+03
  0.470040E+01 0.123617E+03
  0.506180E+01-0.603189E+02
  0.545099E+01-0.289547E+02
  0.587009E+01 0.342604E+02
  0.632142E+01-0.184113E+02
  0.680746E+01 0.960212E+01
  0.733086E+01-0.644094E+01
  0.789450E+01 0.355900E+01
  0.850149E+01 0.423602E+00
  0.915514E+01-0.494758E+00
  0.985904E+01-0.184345E+00
  0.106171E+02 0.156216E+00
  0.114334E+02 0.149583E+00
  0.123125E+02-0.788073E-01
  0.132591E+02-0.186301E-01
  0.142786E+02-0.845357E-02
  0.153764E+02-0.105352E-01
  0.165586E+02 0.375126E-02
  0.178318E+02 0.255503E-02
  0.192028E+02-0.174155E-02
  0.206792E+02-0.632908E-03
  0.222692E+02 0.250266E-03
  0.239814E+02 0.116621E-03
  0.258252E+02 0.114096E-03
  0.278108E+02 0.241388E-03
  0.299491E+02-0.122681E-03
  0.322518E+02-0.561387E-04
  0.347315E+02 0.437865E-04
  0.374019E+02-0.369121E-04
  0.402776E+02 0.255693E-04
  0.433744E+02 0.127757E-04
  0.467094E+02-0.780310E-05
  0.503007E+02-0.303583E-05
  0.541681E+02-0.120920E-04
  0.583329E+02 0.302285E-05
  0.628180E+02-0.132716E-05
  0.676478E+02 0.641459E-05
  0.728490E+02 0.518894E-05
  0.784501E+02-0.175147E-05
  0.844819E+02-0.194860E-05
  0.909774E+02 0.129325E-05
  0.979724E+02-0.112184E-06
  0.105505E+03 0.136117E-05
  0.113617E+03-0.106870E-05
  0.122353E+03 0.692845E-06
  0.131760E+03-0.650585E-06
  0.141891E+03-0.363950E-06
  0.152800E+03-0.282217E-06
  0.164548E+03 0.269441E-06
  0.177200E+03-0.129362E-06
  0.190824E+03 0.173303E-06
  0.205496E+03-0.135559E-06
gabe
  • 193
  • 1
  • 10
  • 4
    Not the bug you are looking for, but [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – user4581301 Dec 18 '19 at 02:10
  • Can you please add an example of the input file and add the C++ standard version and compiler used? – walnut Dec 18 '19 at 02:13
  • 3
    It also looks to me more likely that you have UB somewhere in the parts of the program not shown. – walnut Dec 18 '19 at 02:17
  • Since you know the line-numbers, drop a breakpoint with a skip count of the line you want on the read and see what you really read. – user4581301 Dec 18 '19 at 02:19
  • 1
    Sorry @walnut, I'm not familiar with the terminology UB. If you could explain further I'd greatly appreciate it. Also, I added the edits you asked for. – gabe Dec 18 '19 at 02:24
  • @gabe UB means [undefined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior). If you have undefined behavior anywhere in your code then the effects can manifest everywhere in any form, e.g. as in this example. Some possible examples of UB here could be writing out-of-bounds to another array or data races (openmp), etc. in the code not shown. – walnut Dec 18 '19 at 02:26
  • UB is 'undefined behaviour'. It is dangerous because when the compiler detects UB, it can do anything; the C++ standard places no constraints on what it should do because the required behaviour is undefined. – Jonathan Leffler Dec 18 '19 at 02:28
  • If you can, try running your program with the address sanitizer enabled and see whether it gives you any errors (on either machine). – walnut Dec 18 '19 at 02:32
  • 1
    I see, so the problem could have crept in earlier and is only manifesting here. Looks like this will be a tougher fix than I had thought. – gabe Dec 18 '19 at 02:39
  • This is why stackoverflow.com's [help] gives you instructions for preparing a [mre] that anyone can cut/paste, run, and reproduce what you're asking about. Otherwise, it is unlikely that anyone will be able to tell you anything. – Sam Varshavchik Dec 18 '19 at 02:43
  • @SamVarshavchik I can add an example in the morning if it helps. – gabe Dec 18 '19 at 02:49
  • `if (!(inputFile >> inputLine[j])) { std::cerr << "error: invalid double input.\n"; return 1; }` Always ***Validate*** Every Input. – David C. Rankin Dec 18 '19 at 04:15
  • `abs` should be `std::abs` if you used `#include` (which you should) rather than `#include`. If `abs` is the only difference in how the columns are processed, have you tried running it without the `abs`? Does that change the results? – walnut Dec 18 '19 at 17:53
  • @walnut This was exactly the problem, I was in the process of typing up an answer after that edit and got interrupted. If you want to write an answer noting this I'd happily give you the rep. – gabe Dec 18 '19 at 18:06
  • @gabe Go ahead with your answer. I will see if there is anything to add for me. – walnut Dec 18 '19 at 18:07

3 Answers3

2

I figured this issue out. The issue came from the absolute value function in adding the read-in values to the array. Apologies to those who spent time thinking about this before my edit including the abs(). Here is an example that demonstrated the problem:

#include <stdio.h>
#include <stdlib.h> //comment out to reproduce
#include <cmath>

int main(){

    double init,end;
    init = -0.5;

    end = abs(init);

    printf("%f\n", end);

    return 1;
}

Compiling this with g++ -Wall -Wextra -Werror abs_bug.cxx -o example produced different results on system A and system B. On system A, the output is .5 and on system B it is 0. However, commenting out the indicated include makes both systems give 0.

The compiler versions on each system are different. On system A it is g++ 7.4.0 and on system B it is g++ 4.8.5. It was a mistake in my original post to claim they were the same - again, apologies for time wasted.

I'm assuming that the newer compiler is able to recognize I am passing a floating point number to abs() and replace it with some overloaded function, while the older version can not.

gabe
  • 193
  • 1
  • 10
2

Adding on to @gabe's own answer:

std::abs in C++ comes with multiple overloads for different integer and floating point types. Originally abs is a C function, though, and in C overloads do not exist and neither do namespaces.

In C abs always takes an int as argument and is declared in <stdlib.h>. Therefore, if it is included in C++, an abs overload taking int will be introduced into the global namespace scope.

<cmath> includes the floating point overloads for std::abs, but is only required to declare them in the std namespace. It is unspecified whether the overloads are also made available in the global namespace. This behavior is true for all of the C-derived <cname> header files.

Therefore, when #includeing only <cmath> and <stdlib.h>, we will have the floating point overloads in the std namespace and the int overload in the global namespace.

So, since the floating point overload may not be present in the global namespace, calling abs with unqualified name could call the integer overload instead.

[Actually, there are further integer overloads for abs that should be declared in the global namespace by <stdlib.h>, but apparently the compiler version you are using is not doing that (which is a bug).

If multiple integer overloads, but no floating point overload, were declared, overload resolution would be ambiguous and cause a compilation error, but that seems to not be happening here.]

Now, if the integer overload is used, the double argument will first be converted to the integer type. This looses all of the fractional part of the number and the conversion back from the integer result of abs to double may cause additional representation error.

If the double's value is out-of-range for the integer type, the conversion will even cause undefined behavior.


The moral of this is to always include the <cname> headers and use the names declared in std::, not the global ones. (You can import them into the current scope with using std::abs; instead.)

The <name.h> standard library headers have always been deprecated in C++ and should be avoided.

Since it is unspecified whether the <cname> headers also make their names available in the global namespace, the names in the global namespace should be avoided as well.

Also make sure to include both <cstdlib> and <cmath> for std::abs, because (pre-C++17) the former is only required to include the integer overloads and the latter only the floating point overloads. You want to avoid surprises like the above in reverse when using abs on integer types.

walnut
  • 21,629
  • 4
  • 23
  • 59
0

It may be the case that you are experiencing an end of file error, which means that the penultimate double values remain default constructed. This may be as a result of different c++ implementations on different machines. If you are sure that the program behaves as expected on one machine and on another it functions differently, the only logical thing to conclude is that it is an implementation issue.

cmcl141
  • 17
  • 1
  • The other obvious line of enquiry is that the loading component is where the error is occuring. Perhaps you could implement a means of checking this. – cmcl141 Dec 18 '19 at 03:38
  • In the shown code the same `double` objects are used to read into, meaning that after the first iteration they are not in their default-constructed state anymore. An end-of-file issue would manifest on both machines in the same way and would only affect the last iteration. It is not helpful to speculate about problems in code not shown. The answer section is not meant for that. Once you have enough reputation you can comment on the question to ask for clarifications. – walnut Dec 18 '19 at 04:06
  • I don't see how any of what OP is showing has implementation-defined behavior. The program behaving as expected on one machine does not mean that it doesn't have undefined behavior and that therefore the other C++ implementation is wrong in behaving as OP observed. – walnut Dec 18 '19 at 04:09
  • I completely misunderstood the implementation and therefore retract my statements. – cmcl141 Dec 18 '19 at 04:22
  • The problem is most certainly with the loading component. – cmcl141 Dec 18 '19 at 04:24