473,659 Members | 3,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.0673828125000 000e-001
-4.9169921875000 000e-001
-4.2138671875000 000e-001
-3.6889648437500 000e-001
-2.4206542968750 000e-001
-2.0385742187500 000e-001
0.0000000000000 000e+000
4.3029785156250 000e-003
4.4750976562500 000e-001
7.2753906250000 000e-001
7.8857421875000 000e-001
8.7060546875000 000e-001
8.7890625000000 000e-001
8.9794921875000 000e-001
8.7011718750000 000e-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 2373
On Aug 23, 5:23*am, Ouroborus777 <deadchic...@gm ail.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

"Ouroborus7 77" <de*********@gm ail.comwrote in message
news:01******** *************** ***********@t1g 2000pra.googleg roups.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.0673828125000 000e-001
0.0000000000000 000e+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.0999999999999 999999945789891 375724778299627 359956502914428 710937500000000 0000000
Value: 0.1000000000000 000000013552527 156068805425093 160010874271392 822265625000000 0000000
After: 0.1000000000000 000000081315162 936412832550558 960065245628356 933593750000000 0000000

0.1 as double:
Before: 0.0999999999999 999916733273153 113259468227624 893188476562500 00
Value: 0.1000000000000 000055511151231 257827021181583 404541015625000 00
After: 0.1000000000000 000194289029309 402394574135541 915893554687500 00

0.1 as float:
Before: 0.0999999940395 355224609375000 000000000000000 000000000000000 00
Value: 0.1000000014901 161193847656250 000000000000000 000000000000000 00
After: 0.1000000089406 967163085937500 000000000000000 000000000000000 00
>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.0673828125000 000e-001
-4.9169921875000 000e-001
-4.2138671875000 000e-001
-3.6889648437500 000e-001
-2.4206542968750 000e-001
-2.0385742187500 000e-001
0.000000000000 0000e+000
4.302978515625 0000e-003
4.475097656250 0000e-001
7.275390625000 0000e-001
7.885742187500 0000e-001
8.706054687500 0000e-001
8.789062500000 0000e-001
8.979492187500 0000e-001
8.701171875000 0000e-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.302978515625 e-3
4.4750976562 5e-1
7.275390625e-1
7.8857421875 e-1
8.7060546875 e-1
8.7890625e-1
8.9794921875 e-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.co mwrote:
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.0999999999999 999999945789891 375724778299627 359956502914428 710937500000000 0000000
Value: 0.1000000000000 000000013552527 156068805425093 160010874271392 822265625000000 0000000
After: 0.1000000000000 000000081315162 936412832550558 960065245628356 933593750000000 0000000

0.1 as double:
Before: 0.0999999999999 999916733273153 113259468227624 893188476562500 00
Value: 0.1000000000000 000055511151231 257827021181583 404541015625000 00
After: 0.1000000000000 000194289029309 402394574135541 915893554687500 00

0.1 as float:
Before: 0.0999999940395 355224609375000 000000000000000 000000000000000 00
Value: 0.1000000014901 161193847656250 000000000000000 000000000000000 00
After: 0.1000000089406 967163085937500 000000000000000 000000000000000 00
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.099999999999 999999994578989 137572477829962 735995650291442 871093750000000 00000000
>Value:
0.100000000000 000000001355252 715606880542509 316001087427139 282226562500000 00000000
>After:
0.100000000000 000000008131516 293641283255055 896006524562835 693359375000000 00000000
>>
0.1 as double:
Before: 0.0999999999999 999916733273153 113259468227624 893188476562500 00
Value: 0.1000000000000 000055511151231 257827021181583 404541015625000 00
After: 0.1000000000000 000194289029309 402394574135541 915893554687500 00

0.1 as float:
Before: 0.0999999940395 355224609375000 000000000000000 000000000000000 00
Value: 0.1000000014901 161193847656250 000000000000000 000000000000000 00
After: 0.1000000089406 967163085937500 000000000000000 000000000000000 00

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.bu rditt.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
2593
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 of classes that allow me to interact with network devices. In other words, if I have a path and filename inside of a string variable in python, how do I send the file to the printer?? Thanks in advance
4
4845
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 selected. For example, if I were to have 5 pages with every second page printing, I would get the following results: Page 1 = Print OK Page 2 = Blank Page 3 = Print OK Page 4 = Blank
6
6571
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
6871
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 Steve
1
2480
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 example tekst = DateTime.Today.Date.ToString("dd-MM-yyyy"); // asign content to the variable tekst
1
5704
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 of a Microsoft book, "Visual Basic,Net Step by Step" in Chapter 18. All but the bottom two subroutines will open a text file, and then allow me to use the above controls, example 1. The bottom two subroutines will print a graphic file, example...
0
1418
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 Compact Framework, so there is no PrintDocument or even a driver I can use. try {
7
2420
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 briefly in a number of text books, I have never seen it applied in reality satisfactorily. Can anyone point me to a real situation where this has been done please? I have tried in vain to find examples, but have been deluged with irrelevant...
1
6778
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 layout with a float left and a float right. When I view the page in Print Preview, the float right column breaks to the next line. I haven't been able to come up with a viable solution for this problem. I've tried creating my own stylesheet for...
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8335
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8747
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8627
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6179
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.