l i n u x - u s e r s - g r o u p - o f - d a v i s
Next Meeting:
July 7: Social gathering
Next Installfest:
Latest News:
Jun. 14: June LUGOD meeting cancelled
Page last updated:
2002 Jan 14 16:32

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] totally confused about C promotion
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] totally confused about C promotion

begin Rod Roark <rod@sunsetsystems.com> 
> It's not sqrt that is producing nonsense, but rather printf.  Printf 
> accepts any types in its arguments, and it's your responsibility to make 
> sure that the data types match up with what's in the format string.  The 
> compiler can't fix it because it's a runtime issue.
> This is generally considered a weakness in the way that printf works.
i understand printf well enough (and printf really isn't important to
me, since i'm not really printing these values.  just calculating them).

> Regarding the math function, the compiler can cast the argument (perhaps 
> with a warning) because it knows how sqrt is declared.
so you're saying it's fine to pass sqrt a long double, or "it could be

also, i think i might've mispoke before.  the info docs talk about
long double precision functions, but i'm having trouble finding them
anywhere.  perhaps they're not implemented yet.  there's something
called tdmath.h that i'm trying to look into right now.  can't see any
mention of it in the glibc docs.  looking at google right now...


> On Monday 14 January 2002 15:16, Peter Jay Salzman wrote:
> > from kernighan and ritchie, page 45:
> >
> >   For example, the library routine sqrt expects a double argument, and
> >   will produce nonsense if inadvertantly handed something else.
> >
> > consider the following code1.c:
> >
> >    int main(void)
> >    {
> >    	long double var = 4.0L;
> >
> >    	printf("%Lf\n", sqrt(var));
> >    	return 0;
> >    }
> >
> > this prints nonsense (0.0000), since sqrt returns double and i'm
> > printing a long double.  next, consider this code2.c:
> >
> >    int main(void)
> >    {
> >    	long double var = 4.0L;
> >
> >    	printf("%Lf\n", (long double)sqrt(var));
> >    	return 0;
> >    }
> >
> > this works.  but i'm not really interested in just "working" because
> > this particular code is extremely important to me.  i don't want to base
> > my code on something that works by accident.
> >
> > what happens when you hand a long double to a math function which
> > expects a double?  i can't find any rules for "demotion" in K+R.
> > does code2.c work by accident or is there a concept of demoting floating
> > points to a narrower width when the function expects them to be
> > narrower?
> >
> > pete
> _______________________________________________
> vox-tech mailing list
> vox-tech@lists.lugod.org
> http://lists.lugod.org/mailman/listinfo/vox-tech

The mathematics [of physics] has become ever more abstract, rather than more
complicated.  The mind of God appears to be abstract but not complicated.
He also appears to like group theory.  --  Tony Zee's `Fearful Symmetry'

PGP Fingerprint: B9F1 6CF3 47C4 7CD8 D33E  70A9 A3B9 1945 67EA 951D
vox-tech mailing list

LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
O'Reilly and Associates
For numerous book donations.