java - DataInputStream sometimes reads NaN instead of a float -


bit of java newb. writing stl file viewer. i'm reading floats binary file using datainputstream randomly getting nan on values. file, when opened on pc in stl file readers seems intact, nan values i'm getting mean quite few of facets in file corrupted. circumstances lead datainputstream returning nan?

my code seems work fine, these random nans driving me crazy. here's truncated version of reading code - pretty straightforward think. appreciated:

public static int getfacetbinary(datainputstream in, arraylist al, arraylist aln) {          <snip>          // read 1st vertex         fxr = in.readfloat();         f1x = swap(fxr);         al.add(f1x);         if (fminx > f1x) fminx = f1x;         if (fmaxx < f1x) fmaxx = f1x;          fyr = in.readfloat();         f1y = swap(fyr);         al.add(f1y);         if (fminy > f1y) fminy = f1y;         if (fmaxy < f1y) fmaxy = f1y;          fzr = in.readfloat();         f1z = swap(fzr);         al.add(f1z);         if (fminz > f1z) fminz = f1z;         if (fmaxz < f1z) fmaxz = f1z;          ivertcount++;                     <snip> 

edit: found workaround still no idea why readfloat returns nan. i'd still interested in answer question. read 4 bytes, bitshift them, convert float , works perfectly:

in.readfully(fourbytes); v1  = ((fourbytes[3]&0xff) << 24) | ((fourbytes[2]&0xff) << 16) |        ((fourbytes[1]&0xff) << 8) | ((fourbytes[0]&0xff) << 0); f1x = float.intbitstofloat(v1); 

and doing shift later anyway convert endianness, not needed, guess isn't lot slower.

if i'm not mistaking think nan stands "not number" hence reading stl file might work flawlessly parsing value might fail due numerous reasons.

spontaneously think you're trying parse empty string file (by in.readfloat() lines), else not possible parse float (like "abc" or maybe "1,3" - opposed "1.3").

i don't know "stl file" or how it's built, there guarantee values you're reading possible parse float values?


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -