473,320 Members | 1,839 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,320 software developers and data experts.

printing variable length floats

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?
Aug 23 '08 #1
7 2359
On Aug 23, 5:23*am, Ouroborus777 <deadchic...@gmail.comwrote:
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.
Aug 23 '08 #2

"Ouroborus777" <de*********@gmail.comwrote in message
news:01**********************************@t1g2000p ra.googlegroups.com...
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.
--
Bartc

Aug 23 '08 #3
>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?
The number of digits you get might be surprising. For example, 0.1
as a double ends up with 55 digits, since it can't be represented
exactly. And even the slightest roundoff error can change the
number of digits.

0.1 as long double:
Before: 0.099999999999999999994578989137572477829962735995 65029144287109375000000000000000
Value: 0.100000000000000000001355252715606880542509316001 08742713928222656250000000000000
After: 0.100000000000000000008131516293641283255055896006 52456283569335937500000000000000

0.1 as double:
Before: 0.099999999999999991673327315311325946822762489318 847656250000
Value: 0.100000000000000005551115123125782702118158340454 101562500000
After: 0.100000000000000019428902930940239457413554191589 355468750000

0.1 as float:
Before: 0.099999994039535522460937500000000000000000000000 000000000000
Value: 0.100000001490116119384765625000000000000000000000 000000000000
After: 0.100000008940696716308593750000000000000000000000 000000000000
>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?

Aug 23 '08 #4
On Aug 23, 6:02*am, "Bartc" <b...@freeuk.comwrote:
Try using g instead of e.
Ah, that does the trick. By itself, it still clips to 7 significant
digits but with "%.99g", it works great.
Aug 24 '08 #5
Gordon Burditt wrote:
The number of digits you get might be surprising. For example, 0.1
as a double ends up with 55 digits, since it can't be represented
exactly. And even the slightest roundoff error can change the
number of digits.

0.1 as long double:
Before: 0.099999999999999999994578989137572477829962735995 65029144287109375000000000000000
Value: 0.100000000000000000001355252715606880542509316001 08742713928222656250000000000000
After: 0.100000000000000000008131516293641283255055896006 52456283569335937500000000000000

0.1 as double:
Before: 0.099999999999999991673327315311325946822762489318 847656250000
Value: 0.100000000000000005551115123125782702118158340454 101562500000
After: 0.100000000000000019428902930940239457413554191589 355468750000

0.1 as float:
Before: 0.099999994039535522460937500000000000000000000000 000000000000
Value: 0.100000001490116119384765625000000000000000000000 000000000000
After: 0.100000008940696716308593750000000000000000000000 000000000000
Your representations are deceptively wide. The 64-bit mantissa of long
double has about 19 digits of precision. The 53-bit double about 16 digits.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Aug 24 '08 #6
>The number of digits you get might be surprising. For example, 0.1
>as a double ends up with 55 digits, since it can't be represented
exactly. And even the slightest roundoff error can change the
number of digits.

0.1 as long double:
Before:
0.09999999999999999999457898913757247782996273599 565029144287109375000000000000000
>Value:
0.10000000000000000000135525271560688054250931600 108742713928222656250000000000000
>After:
0.10000000000000000000813151629364128325505589600 652456283569335937500000000000000
>>
0.1 as double:
Before: 0.099999999999999991673327315311325946822762489318 847656250000
Value: 0.100000000000000005551115123125782702118158340454 101562500000
After: 0.100000000000000019428902930940239457413554191589 355468750000

0.1 as float:
Before: 0.099999994039535522460937500000000000000000000000 000000000000
Value: 0.100000001490116119384765625000000000000000000000 000000000000
After: 0.100000008940696716308593750000000000000000000000 000000000000

Your representations are deceptively wide. The 64-bit mantissa of long
double has about 19 digits of precision. The 53-bit double about 16 digits.
I'm still printing the exact value that is present in the floating-point
variable. There's nothing wrong with that, especially when you're
talking about and/or debugging issues of floating-point roundoff.

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

Aug 25 '08 #7
On Aug 24, 10:18*pm, gor...@hammy.burditt.org (Gordon Burditt) wrote:
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.
Aug 25 '08 #8

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

Similar topics

2
by: Jody Burgess | last post by:
Hi; I am writing my first python program and would like to know how to change stdout to refer to my default printer or any other printer on my network. The other question is, Is there an API set...
4
by: Jody Gelowitz | last post by:
I am having a problem with printing selected pages. Actually, the problem isn't with printing selected pages as it is more to do with having blank pages print for those pages that have not been...
6
by: memocan | last post by:
#include <iostream> using namespace std; int x; //global variable matrix int main() { x= new float ; //initialize the size now }
7
by: Steve M | last post by:
Hello, I'm having problems sending information from a python script to a printer. I was wondering if someone might send me in the right direction. I wasn't able to find much by Google TIA...
1
by: G.Esmeijer | last post by:
Friends, I'm (a bit) dissapointed about the printing possibilities in c#. It could also be lack of knowedge. I would like to align a text on the right side of the paper when printing. For...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
0
by: mottebelke | last post by:
I want to print a bitmap directly to a network printer using a socket. To do this I use PCL codes to set the printer in the right mode and print the image. Note that this code is used on the...
7
by: johnacooke | last post by:
The W3C added THEAD, TFOOT and TBODY elements to facilitate the printing of long tables, so that header and footer information could be repeated on each page. While I have seen this discussed...
1
by: spoonybard | last post by:
Hi Everyone, I have a tableless website design and I am trying to add the feature to print the middle column content on one of my pages. The information that is being displayed is a two column...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.