printing variable length floats

O

Ouroborus777

I've been messing around with printing floats. It seems that printf()
is only capable of printing the fractional portion at a fixed length.
Is there some way to print floats such that the full fraction is shown
but without the trailing zeroes? I realize that this could be an
exercise in string manipulation (ie: stringify the float giving a
length that's longer than the float could possibly be then trim the
trailing zeroes) but I would prefer to do this as modification

What I have ("%.16e"):
-9.0673828125000000e-001
-4.9169921875000000e-001
-4.2138671875000000e-001
-3.6889648437500000e-001
-2.4206542968750000e-001
-2.0385742187500000e-001
0.0000000000000000e+000
4.3029785156250000e-003
4.4750976562500000e-001
7.2753906250000000e-001
7.8857421875000000e-001
8.7060546875000000e-001
8.7890625000000000e-001
8.9794921875000000e-001
8.7011718750000000e-001

What I would like:
-9.0673828125e-1
-4.9169921875e-1
-4.2138671875e-1
-3.68896484375e-1
-2.420654296875e-1
-2.03857421875e-1
0 (or 0.0, or 0.0e+0, but I would prefer just 0)
4.302978515625e-3
4.47509765625e-1
7.275390625e-1
7.8857421875e-1
8.7060546875e-1
8.7890625e-1
8.9794921875e-1
8.701171875e-1

So, any suggestions or maybe point me to some code?
 
O

Ouroborus777

I've been messing around with printing floats. It seems that printf()
is only capable of printing the fractional portion at a fixed length.
Is there some way to print floats such that the full fraction is shown
but without the trailing zeroes? I realize that this could be an
exercise in string manipulation (ie: stringify the float giving a
length that's longer than the float could possibly be then trim the
trailing zeroes) but I would prefer to do this as modification

err, clicked send to early. I meant to continue:

... but I would prefer to do this as a modification to printf.
 
B

Bartc

Ouroborus777 said:
I've been messing around with printing floats. It seems that printf()
is only capable of printing the fractional portion at a fixed length.
Is there some way to print floats such that the full fraction is shown
but without the trailing zeroes? I realize that this could be an
exercise in string manipulation (ie: stringify the float giving a
length that's longer than the float could possibly be then trim the
trailing zeroes) but I would prefer to do this as modification

What I have ("%.16e"):
-9.0673828125000000e-001
0.0000000000000000e+000
What I would like:
-9.0673828125e-1
0 (or 0.0, or 0.0e+0, but I would prefer just 0)
So, any suggestions or maybe point me to some code?

Try using g instead of e.
 
O

Ouroborus777

Granted, whatever calculation produced this value does not have
this much precision.

For the curious, they're from a 3D model file. The values are stored
as 16-bit reals. I'm using POV-Ray to render the data. While I
probably don't need what relatively little precision real16 offers, it
makes me happy in an OCD kind of way.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top