LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-20-2006, 10:43 AM   #1
mjamal
LQ Newbie
 
Registered: Oct 2006
Posts: 1

Rep: Reputation: 0
fprint Exponent printing question


Hi,
I am writing a tool that needs to print doubles as exponential number with a certain precision and such.
I am using the %E modifier with the right precision (*.14), but my requirements need the number of digits after the E to be 3. Valid output example:
-9.00000000000000E-002
However, I can only get two digits after the E symbol... This is what I am doing right now:
double d = 1.232;
printf("double: %+.14E\n", d);
This prints as: +1.23200000000000E+00.

Thank you in advance for suggestions that may help!

Mansoor
 
Old 10-20-2006, 11:32 AM   #2
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,450

Rep: Reputation: 63
Acording to "The C Programming Language" the E gives formatted output in this form ill retype what it says

Quote:
e,E double; [-]m.dddddde+/-xx or [-]m.ddddddE+/-xx, where the number of d's is given by the precision (default 6)
Thus I would surmize that printing output in the way that you need is not auto built into the printf function. You with either have to do 1 of 2 things.

1. Look around for a print function written in another library to do what you want.

2. Write a new printf (possibly from the printf source) that can output in the way that you want.


Good luck

This link might be helpful
http://www.mathworks.com/access/help...f/sprintf.html

Last edited by exvor; 10-20-2006 at 11:42 AM.
 
Old 10-22-2006, 12:24 AM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian
Posts: 401

Rep: Reputation: 187Reputation: 187
Hello!

As a kind of joke, you can use following:
Code:
#include <math.h>
#include <stdio.h>
#include <stdlib.h> // for `int abs(int)'
main()
    {
        double x = -1.1500018e18;
        int ex  = log10( fabs(x) );
        double mantissa = (x/pow(10.0, ex));
        printf("x=%+.14fe%c%.3d\n", mantissa, (ex<0)?'-':'+', abs(ex));
    }
You have to compile this with -lm flag:
Code:
gcc num.c -lm
WARNING: I strongly recommend you to not use this approach in scientific software!
It's: a) very inefficient, b) may cause overflows and loosing of accuracy, c)maybe smth. else.

Nevertheless, I hope, this is useful for you.
 
Old 10-22-2006, 02:27 PM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 706

Rep: Reputation: 68
Hi.

Fortran seems more flexible, allowing you to specify the width of the exponent field. If you can arrange to call a Fortran routine from C, you might save yourself some trouble. For example, g77 on this code:
Code:
    program main

    d = 1.232
    x = -1.1500018e18

    write(6,9), d, x
9   format( "d = ", e14.6e3, " x = ", e20.10e4 )

    end
produces this:
Code:
% ./a.out
d =  0.123200E+001 x =  -0.1150001820E+0019
Best wishes ... cheers, makyo
 
  


Reply

Tags
printf


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cups printing question gsmonk Linux - General 4 11-22-2006 12:03 PM
oowriter printing question puishor Linux - Software 1 09-30-2005 09:40 AM
fprint hangs at __lll_mutex_lock_wait ugtech Linux - Enterprise 1 04-20-2005 08:56 AM
Problems with Apache2 and exponent CMS install dthacker Linux - Software 0 01-29-2005 05:48 PM
exponent suchi_s Programming 1 10-30-2004 07:11 AM


All times are GMT -5. The time now is 04:33 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration