473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Printf formatting

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
Nov 13 '05 #1
3 2471
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote:
# 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]);
....
--
Derk Gwen http://derkgwen.250free.com/html/index.html
I ASSURE YOU WE'RE OPEN!
Nov 13 '05 #2

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message

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?
Nov 13 '05 #3
"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote (15 Jul 2003) in
news:bf**********@news.freedom2surf.net / comp.lang.c:
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.

--
Martin Ambuhl
Returning soon to the
Fourth Largest City in America
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Pontus F | last post by:
Hi I am learning C++ and I'm still trying to get a grip of pointers and other C/C++ concepts. I would appreciate if somebody could explain what's wrong with this code: ---begin code block--- ...
3
by: Matt | last post by:
Hello, Is there a means to use printf()-like formatting (eg: http://www.mkssoftware.com/docs/man1/printf.1.asp ) using class string and/or iostream constructs so that I need not have to define...
11
by: Steven T. Hatton | last post by:
OK, I take back the nasty stuff I said about printf. At least until I see a similarly concise way of writing this using C++ formatters. I want to convert the following to something I can put onto...
7
by: sunfiresg | last post by:
During an interview, I am asked to answer a question: Printf is a major formatted output function provided by the standard C library. Printf accepts a formatting string followed by a various...
8
by: jchludzinski | last post by:
Is there a format specification for printf that will result in: 1000000 being printed as 1,000,000? Or 1000000.0 as 1,000,000.0? ---John
27
by: jacob navia | last post by:
Has anyone here any information about how arrays can be formatted with printf? I mean something besides the usual formatting of each element in a loop. I remember that Trio printf had some...
18
by: Joah Senegal | last post by:
Hello all, I'm trying to print a string on my screen... But the string comes from a variable string... This is the code #include <cstdlib> #include <iostream> #include <string> using...
3
by: hg | last post by:
I have a function called logprintf that take a printf-like syntax. On top of that I want to implement a debug-printf function which calls logprintf but prefix the information using a string: ...
15
by: singhraghvendra | last post by:
Hi I have a function as below void print(char* str) { printf(str); } now the user of the function can pass anything as the argument for
25
by: jacob navia | last post by:
The C99 standard forgot to define the printf equivalent for complex numbers Since I am revising the lcc-win implementation of complex numbers I decided to fill this hole with "Z" for...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.