Printf formatting

A

Allan Bruce

Hi there,
I am dumping a lot of floats to file and wish to reduce the filesize.
This is my main loop (looped several thousand times)

fprintf(fptr, "%f %f %f ", normals[0][0], normals[0][1], normals[0][2]); //
output the normal
fprintf(fptr, "%f %f %f ", v1[0], v1[1], v1[2]); // output the vertice

fprintf(fptr, "%f %f %f ", normals[1][0], normals[1][1], normals[1][2]); //
output the normal
fprintf(fptr, "%f %f %f ", v2[0], v2[1], v2[2]); // output the vertice

fprintf(fptr, "%f %f %f ", normals[2][0], normals[2][1], normals[2][2]); //
output the normal
fprintf(fptr, "%f %f %f ", v3[0], v3[1], v3[2]); // output the vertice

fprintf(fptr, "%f %f %f ", normals[3][0], normals[3][1], normals[3][2]); //
output the normal
fprintf(fptr, "%f %f %f ", v4[0], v4[1], v4[2]); // output the vertice

fprintf(fptr, "\n");// go to next line

Most of my numbers come out as something like 0.500000
Is it possible to truncate these to just 0.5 but without losing precision of
some of the numbers which may use all the decimal places?
Does anybody have any other suggestions about how to cut down on the file
size?
Thanks
Allan
 
D

Derk Gwen

# Hi there,
# I am dumping a lot of floats to file and wish to reduce the filesize.
# This is my main loop (looped several thousand times)
#
# fprintf(fptr, "%f %f %f ", normals[0][0], normals[0][1], normals[0][2]); //
# output the normal
# fprintf(fptr, "%f %f %f ", v1[0], v1[1], v1[2]); // output the vertice
#
# fprintf(fptr, "%f %f %f ", normals[1][0], normals[1][1], normals[1][2]); //
# output the normal
# fprintf(fptr, "%f %f %f ", v2[0], v2[1], v2[2]); // output the vertice
#
# fprintf(fptr, "%f %f %f ", normals[2][0], normals[2][1], normals[2][2]); //
# output the normal
# fprintf(fptr, "%f %f %f ", v3[0], v3[1], v3[2]); // output the vertice
#
# fprintf(fptr, "%f %f %f ", normals[3][0], normals[3][1], normals[3][2]); //
# output the normal
# fprintf(fptr, "%f %f %f ", v4[0], v4[1], v4[2]); // output the vertice
#
# fprintf(fptr, "\n");// go to next line
#
# Most of my numbers come out as something like 0.500000
# Is it possible to truncate these to just 0.5 but without losing precision of
# some of the numbers which may use all the decimal places?
# Does anybody have any other suggestions about how to cut down on the file

You can sprintf and then edit that before the real print.

void printfl(FILE *fptr,float f) {
char b[50],*p;
sprintf(b,"%f",f);
for (p=b+strlen(b); p>b; p--)
if (p[-1]!='0') break;
*p = 0;
fputs(b,fptr); fputc(' ',fptr);
}
....
printfl(fptr,normals[0][0]); printfl(fptr,normals[0][1]); printfl(fptr,normals[0][2]);
printfl(fptr,v1[0]); printfl(fptr,v1[1]); printfl(fptr,v1[2]);
....
 
M

Malcolm

Allan Bruce said:
Most of my numbers come out as something like 0.500000
Is it possible to truncate these to just 0.5 but without losing precision of
some of the numbers which may use all the decimal places?
%g is your friend.

Does anybody have any other suggestions about how to cut down on the
file size?
The obvious solution is to use a binary format and some type of compression.

If you can't do this, I'd concentrate on the normals. Are values like 0,1,0
so common that you could replace them with a token?
If not, how accurate does the normal need to be? Could you quantise to units
of a hundred without losing anything important?
 
M

Martin Ambuhl

Most of my numbers come out as something like 0.500000
Is it possible to truncate these to just 0.5 but without losing
precision of some of the numbers which may use all the decimal
places?

"%g" should do the job. Check your documentation for the effects of
modifiers to it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top