www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2003/07/20/02:17:51

Newsgroups: comp.os.msdos.djgpp
Subject: Re: Doubles won't see double!!
From: Martin Ambuhl <mambuhl AT earthlink DOT net>
References: <KrkSa.23882$pK2 DOT 37447 AT news DOT indigo DOT ie>
Organization: Nocturnal Aviation
Message-ID: <Xns93BE42C022A7mambuhlearthlinknet@207.217.77.26>
User-Agent: Xnews/5.04.25
X-Face: #PT?r&aN>ro*?:r AT h&l1(3WchlNz?9-y/wnr]CjPxdjd/*4lVC\*M+aZLO?E!2Q)]vD*M8oL(Zu2:VR17$t7-eVdNQH'!xp'5'NAP4$\fb"zAcf60HTC%S!<~xD}TFE76>a)IF-xa%$2at+D^;7~$mW^aDR)]bBmO``s<c=AN_wKI(G1PX/L02Q~BX<O}T<@916W22JYTnlp`C:a~[$WAdF9XpX'x!3jFgPK&*O6'^%RkX-C`F$7A7^}V$.K=
Lines: 55
Date: Sun, 20 Jul 2003 05:24:54 GMT
NNTP-Posting-Host: 65.148.4.228
X-Complaints-To: abuse AT earthlink DOT net
X-Trace: newsread2.prod.itd.earthlink.net 1058678694 65.148.4.228 (Sat, 19 Jul 2003 22:24:54 PDT)
NNTP-Posting-Date: Sat, 19 Jul 2003 22:24:54 PDT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

"Noel O'Donnell" <nodger AT eircom DOT net> wrote (18 Jul 2003) in
news:KrkSa.23882$pK2 DOT 37447 AT news DOT indigo DOT ie / comp.os.msdos.djgpp: 

> Hello all,
> 
> Just wondering this:
> How can I perform maths on doubles, so that the result uses the
> full potential of a double's decimal spaces?
> 
> confused? ok, perhaps an example:
> 
> double x,y,z;
> x=22;
> y=7;
> z=x/y;
> cout<<z;
> 
> This will generally output something like:
> 3.1425(or whatever)
> however if I do the same sum on the windoze calculator:
> 3.142857142957142857...................................you get the
> picture 
> 
> so my question is this: How can I make MY doubles behave like
> this? is there a library that i need to include? any suggestions?

In C++ the , the ios_base classes have member functions precision() 
and width(); <iomanip> also provides setprecision and setw 
manipulators.  Your C++ text should give you the information you 
need.  Below is an example of accomplishing this in C: 

#include <stdio.h>
#include <float.h>

int main(void)
{
    double x = 22, y = 7;
    long double xl = 22, yl = 7;
    float xf = 22, yf = 7;
    printf("double result: %.*g\n", DBL_DIG, x / y);
    printf("long double result: %.*Lg\n", LDBL_DIG, xl / yl);
    printf("float result: %.*g\n", FLT_DIG, xf / yf);
    return 0;
}


double result: 3.14285714285714
long double result: 3.14285714285714286
float result: 3.14286


-- 
Martin Ambuhl
Returning soon to the
Fourth Largest City in America

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019