0

I am changing my program from floating-point precision to double precision. Nevertheless I start this routine by reading floating point numbers from a binary file.

bufz = alloc1float(nz);
for (i=0; i<nx; i++) {
    nu = efread(bufz,sizeof(float),nz,vel); 
    for (k=0; k<nz; k++)
        vn2[k][i] = bufz[k];
}

I proceed with calculations that shall be of double precision and write the calculated values into a file:

matlabfile=fopen("matlabfile","a+");
fprintf(matlabfile,"%d;%15.7e;%15.7e\n",l,amxs,amxr);
fclose(matlabfile);

(amxs and amxr are of size double, l is int and matlabfile is a filepointer)

My question: How does C proceed with the input parameters of type float when the arithmetic is done with double precision and how does it round the result in the output? Is it that easy as shown or do I miss something significantly?

EDIT: FILE *vel, *matlabfile;

 int main(int argc, char *argv[])
 {

 int i, nx, nz, k, nu, l;
 double amxs, amxr;
 double *buf;
 double **vn2;

 vn2    = alloc2double(nx,nz);
 buf = alloc1float(nz);

   /* read input binary file */
   for (i=0; i<nx; i++) {
    nu = efread(buf,sizeof(float),nz,vel); 
    for (k=0; k<nz; k++) vn2[k][i] = bufz[k];
   }

 Arithmetics with double precision arrays, variables and vn2...

// save output
matlabfile=fopen("matlabfile","a+");
fprintf(matlabfile,"%d;%15.7e;%15.7e\n",l,amxs,amxr);
fclose(matlabfile);
MichaelScott
  • 387
  • 1
  • 3
  • 21
  • 1
    you need to show us your variable declarations, ie what type everything is. – Nicu Stiurca Mar 25 '13 at 21:22
  • You've shown us everything but the relevant code! Where is the code for the double-precision calculation that starts with single-precision data? – Oliver Charlesworth Mar 25 '13 at 21:33
  • As the name suggests, double datatype has 2x precision as compared to float. So for converting from float to double there is no loss of precision(the extra precision places can be filled with 0). Truncation happens on the reverse conversion. – CuriousSid Mar 25 '13 at 21:49
  • The precision is just important for the arithmetics since instabilities may occur in my algorithm due to roundoff errors. The output is just a quality proof. – MichaelScott Mar 25 '13 at 22:04

1 Answers1

1

It doesn't. It (implicitly) converts the double into a float and you lose precision. It essentially chops off the least significant bits.

Sorry for the lack of concrete examples, but something like 9.223372036854775808 will become 9.223372037.

A similar thing happens when you load your floats into doubles. It converts them into a different format. EDIT... which works out fine.

Philip
  • 1,539
  • 14
  • 23
  • 1
    " if you cast it back as a float, it's not guaranteed to be the same number you had before" - are you sure? – Oliver Charlesworth Mar 25 '13 at 21:45
  • thanks for the fast reply Philip. Does it cast the floating-point numbers automatically into double precision or do I have to convert my input before calculation? I mean I store them in the bufz array of type float first. – MichaelScott Mar 25 '13 at 21:47
  • 2
    There's no such thing as an "implicit cast" in C. A cast is an explicit conversion. "It (implicitly) *converts* ..." would be more correct. – Keith Thompson Mar 25 '13 at 21:50
  • Thank you all for the corrections. Michael, third line, by storing your `floats` in a `double` field, it automatically converts them to doubles. – Philip Mar 25 '13 at 22:15
  • is it also possible to store them into a `float` array first and move them later to a `double` array? – MichaelScott Mar 25 '13 at 22:18
  • Sure, not that I can really think of a reason why you would want to do that... but whatever floats your goat. – Philip Mar 25 '13 at 22:20
  • 1
    The low bits are rounded (in the default mode in most implementations), not chopped. – Eric Postpischil Mar 26 '13 at 01:04
  • @KeithThompson umm, unsigned int x = 5; int y = x; [implicit cast], int z = (int) x; [explicit cast]. These are the definitions of implicit and explicit casts in C-like languages. Implicit - compiler does it silently. Explicit, you force it to happen. Typically, one avoids implicit because compilers are lazy (as in they are optimized to do as little work as possible so relying on implicit casts for anything other than basic numeric types is asking for trouble). – iheanyi Apr 02 '14 at 15:50
  • 1
    @iheanyi: Your terminology is wrong (or at least inconsistent with that used by the language standard). `unsigned int x = 5; int y = x;` -- That's an implicit *conversion*. `int z = (int)x);` -- that's a *cast*, which is an explicit conversion. A cast is an operator, consisting of a type name in parentheses; it specifies an explicit conversion. A cast occurs in source code. A conversion happens at run time (or possibly at compile time if the compiler is able to optimize it). – Keith Thompson Apr 02 '14 at 15:54
  • 1
    @KeithThompson Thanks for that response. I withdraw my correction. – iheanyi Apr 02 '14 at 15:55