[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: converting int 2 real and vice versa
From: |
John W. Eaton |
Subject: |
Re: converting int 2 real and vice versa |
Date: |
Thu, 3 Sep 1998 10:44:06 -0500 (CDT) |
On 3-Sep-1998, Daniel Heiserer <address@hidden> wrote:
| Unfortunately I don't know what it is as long as I don't look at the
| value. Else I could specify on the fread what I want to read. Probably
| the best thing is writing a C-code translating all the stuff into
| doubles with the right conversion:
| // As far as I know that works for IEEE-be and IEEE-le
| // No idea about cray formats
| // all the values are represented by a 4byte-word.
| // read the 4byte-word into a signed integer my_int
| // then make the decision
| if (my_int>limit || my_int<-limit){
| my_doub=equivalence(my_int);
| }
| else{
| my_doub=(double) my_int;
| }
| It would be nice to do something like that in octave.
|
| Thanks for your help. Any more ideas?
Here is a possibility. Read all the data as integer values. Select
the ones that need to be converted to floats, write them (as integers)
to a temporary file, reread them as floats and then insert them back
into the array. Obligatory-Famous-Last-Words: I've not tested this,
but I think it should work.
fid = fopen (data_file, "r");
data = fread (fid, Inf, "int");
fclose (fid);
idx = find (data >= -limit && data <= limit);
tmp_file = tmpnam ();
fid = fopen (tmp_file, "w+");
fwrite (fid, data(idx), "int");
frewind (fid);
tmp_data = fread (fid, Inf, "float");
fclose (fid);
unlink (tmp_file);
data(idx) = tmp_data;
jwe