title: A problem in saving "long double" variables to hdf5 file
body: <p>I'm new to <code>hdf5</code>. I'm trying to save the results of a simulation in <code>hdf5</code> file. The variables are <code>long double</code>. So, I mapped them into <code>NATIVE_LDOUBLE</code>. However, the saved values are completely wrong (fluctuating between very small and very large values).</p>
  <p>When I save them with <code>NATIVE_DOUBLE</code>, everything is ok. But I need to save <code>long double</code>.</p>
  <p>My question is how to properly save <code>long double</code> variables, and moreover <code>quadruple</code> precision variables?</p>
  <p>I deeply appreciate your help. Examples are also appreciated.</p>
  <p>here is the code</p>
  <pre class="lang-cpp prettyprint-override"><code> void createHDF5_2DProjectionFile(char* file_name, 
                                          CarGrid1D3V&lt;long double&gt;&amp; ph_grid,
                                          std::string first_dim, 
                                          std::string second_dim,
                                          long double *x1, int size_x1, 
                                          long double *x2, int size_x2)
      {
          try
          {
              /* define the size  of the datasets containing the coordinates x1
              and x2    
              */
  
              PredType h5Int = PredType::NATIVE_INT;
              PredType h5DoubleL = PredType::NATIVE_LDOUBLE;
              PredType h5Double = PredType::NATIVE_DOUBLE;        
  
              /* Define the parameters of grid space 
                  DS --&gt; Data Space
              */
              hsize_t x1_dims[1], x2_dims[1];
              x1_dims[0] = size_x1;
              x2_dims[0] = size_x2;
              
              
              H5File *file_id = new H5File(file_name, H5F_ACC_TRUNC);
              
              /* Saving string attribute
                  Create dataspace with H5S_SCALAR
                  Create string datatype of specific length of characters
                  Create attribute and write to it
              */
  
              DataSpace attr_stringDS = DataSpace(H5S_SCALAR);
              StrType strdatatype(PredType::C_S1, 64);
  
              Attribute original_DistFun = file_id-&gt;createAttribute(&quot;/OriginalDistFun&quot;, 
                                                          strdatatype, attr_stringDS);
              original_DistFun.write(strdatatype, &quot;1D3V&quot;);
  
              Attribute projection = file_id-&gt;createAttribute(&quot;/Projection&quot;, 
                                                          strdatatype, attr_stringDS);
              projection.write(strdatatype, first_dim + &quot; - &quot; + second_dim);
  
  
              /* Create the data spaces for grid points along each direction */
              DataSpace* first_dimDS_id = new DataSpace(1, x1_dims, NULL);
              DataSpace* second_dimDS_id = new DataSpace(1, x2_dims, NULL);
              
              /* Create and fille the datasets for grid points along each direction */
              DataSet *data_dim1 = new DataSet(file_id-&gt;createDataSet(first_dim, 
                                                  h5DoubleL, *first_dimDS_id));
  
              data_dim1-&gt;write(x1, h5DoubleL);
  
              DataSet *data_dim2 = new DataSet(file_id-&gt;createDataSet(second_dim, 
                                                  h5DoubleL, *second_dimDS_id));
              data_dim2-&gt;write(x2, h5DoubleL);
              
              /* Important attributes added to the file */
              long double x_minmax[2], px_minmax[2], 
                          py_minmax[2], pz_minmax[2], mom_steps[3],
                          ph_vols[3], spatial_steps[1];
  
              x_minmax[0] = ph_grid.x_min_;
              x_minmax[1] = ph_grid.x_max_;
              px_minmax[0] = ph_grid.px_min_;
              px_minmax[1] = ph_grid.px_max_;
              py_minmax[0] = ph_grid.py_min_;
              py_minmax[1] = ph_grid.py_max_;
              pz_minmax[0] = ph_grid.pz_min_;
              pz_minmax[1] = ph_grid.pz_max_;
              mom_steps[0] = ph_grid.dpx_;
              mom_steps[1] = ph_grid.dpy_;
              mom_steps[2] = ph_grid.dpz_;
              ph_vols[0] = ph_grid.dvs_;
              ph_vols[1] = ph_grid.dvp_;
              ph_vols[2] = ph_grid.dv_; 
              spatial_steps[0] = ph_grid.dx_;
              ph_grid.print_characteristics();
              std::cout &lt;&lt; x_minmax[0] &lt;&lt; &quot; , &quot; &lt;&lt; x_minmax[1] &lt;&lt; &quot;\n&quot;;
              /* define attributes configuration */
              hsize_t space_1[1];
              space_1[0] = 1;
  
              hsize_t space_2[1];
              space_2[0] = 2; 
              
              hsize_t space_3[1];
              space_3[0] = 3; 
              
              DataSpace attr_space_1 = DataSpace(1, space_1);
              DataSpace attr_space_2 = DataSpace(1, space_2);
              DataSpace attr_space_3 = DataSpace(1, space_3);
  
              Attribute x_interval = file_id-&gt;createAttribute(&quot;[x_min,x_max]&quot;,
                                                          h5DoubleL, attr_space_2);
              x_interval.write(h5DoubleL, x_minmax);
  
              Attribute px_interval = file_id-&gt;createAttribute(&quot;[px_min,px_max]&quot;,
                                                          h5DoubleL, attr_space_2);
              px_interval.write(h5DoubleL, px_minmax);
  
              Attribute py_interval = file_id-&gt;createAttribute(&quot;[py_min,py_max]&quot;,
                                                          h5DoubleL, attr_space_2);
              py_interval.write(h5DoubleL, py_minmax);
  
              Attribute pz_interval = file_id-&gt;createAttribute(&quot;[pz_min,pz_max]&quot;,
                                                          h5DoubleL, attr_space_2);
              pz_interval.write(h5DoubleL, pz_minmax);
  
              Attribute MomVolumes = file_id-&gt;createAttribute(&quot;[dpx,dpy,dpz]&quot;, 
                                                              h5DoubleL, attr_space_3);
              MomVolumes.write(h5DoubleL, mom_steps);
  
              Attribute PhVolumes = file_id-&gt;createAttribute(&quot;[dv_s, dv_m, dv_t]&quot;,
                                                              h5DoubleL, attr_space_3);
              PhVolumes.write(h5DoubleL, ph_vols);
  
              Attribute SpatialVolumes = file_id-&gt;createAttribute(&quot;[dx]&quot;, PredType::NATIVE_DOUBLE,
                                                                  attr_space_1);
              SpatialVolumes.write(h5DoubleL, spatial_steps);
  
              /* Free memory */
              delete data_dim1;
              delete data_dim2;
              delete first_dimDS_id;
              delete second_dimDS_id;
              delete file_id;
          }
          catch(DataSetIException error)
          {
              error.printErrorStack();
          }
          catch(DataSpaceIException error)
          {
              error.printErrorStack();
          }
          catch(FileIException error)
          {
              error.printErrorStack();
          }     
      }
  </code></pre>
  <p><strong>Update</strong></p>
  <p>A great discussion and explanations are available on HDF5 forum where I posted the same question
  <a href="https://forum.hdfgroup.org/t/a-problem-when-saving-native-ldouble-variables/9504" rel="nofollow noreferrer">https://forum.hdfgroup.org/t/a-problem-when-saving-native-ldouble-variables/9504</a></p>
  <p>Also, <a href="https://stackoverflow.com/users/1536326/steven-varga">Steven Varga</a> provided examples to answer this question on his GitHub by constructing a user-defined datatype (see this <a href="https://github.com/steven-varga/HDFGroup-mailinglist/tree/master/longdouble-custom-2022-03-15" rel="nofollow noreferrer">link</a>).</p>