From: "Nelson H. F. Beebe" <beebe@math.utah.edu>
To: gcc-help@gcc.gnu.org
Cc: beebe@math.utah.edu, Jim Cromie <jim.cromie@gmail.com>
Subject: Re: compile-time conversion of floating-point expressions to long longs
Date: Wed, 03 May 2006 22:57:00 -0000 [thread overview]
Message-ID: <CMM.0.95.0.1146697025.beebe@psi.math.utah.edu> (raw)
Jim Cromie <jim.cromie@gmail.com> asks on Wed, 03 May 2006 12:17:38 -0400:
>> ...
>> Are there any macros that can tear into a floating point number and
>> pull out the exponent and mantissa ?
>> ...
Yes, the C89 and C99 ISO C Standards include ldexp(x,n), which forms x
* 2**n, and "f = frexp(x,&n);", which returns the fraction as a value
in [1/2,1) (or 0 if x is zero) and the exponent of 2 in n. Both are
exact, and can be implemented reasonably efficiently.
Two related, and useful, functions are
(1) modf(x,*y), which splits x into an integral part stored in y as an
exactly-representable floating-point number, and returns the
fractional part as the function value. Both have the same sign as x.
(2) fmod(x,y), which returns the remainder of x divided by y.
All four of these functions are EXACT; no rounding errors whatsoever
are possible. Note that for fmod(), the computation involves finding
the integer close to x/y, and that can be very large: in IEEE 754
128-bit arithmetic, it is a number of nearly 9900 decimal digits.
Fortunately, it is possible to implement fmod() iteratively, without
access to high-precision floating-point.
ldexp() and frexp() were in C in Unix Version 7 in the late 1970s; the
other two are more recent. You can safely use all four of them on
modern systems. Pre-C99 implementations may lack the float and long
double counterparts. However, glibc on GNU/Linux offers all twelve
variants.
-------------------------------------------------------------------------------
- Nelson H. F. Beebe Tel: +1 801 581 5254 -
- University of Utah FAX: +1 801 581 4148 -
- Department of Mathematics, 110 LCB Internet e-mail: beebe@math.utah.edu -
- 155 S 1400 E RM 233 beebe@acm.org beebe@computer.org -
- Salt Lake City, UT 84112-0090, USA URL: http://www.math.utah.edu/~beebe/ -
-------------------------------------------------------------------------------
next reply other threads:[~2006-05-03 22:57 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-05-03 22:57 Nelson H. F. Beebe [this message]
2006-05-07 17:42 ` Jim Cromie
2006-05-07 19:47 ` Jim Cromie
-- strict thread matches above, loose matches on Subject: below --
2006-05-08 11:53 Nelson H. F. Beebe
2006-05-03 16:18 Jim Cromie
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CMM.0.95.0.1146697025.beebe@psi.math.utah.edu \
--to=beebe@math.utah.edu \
--cc=gcc-help@gcc.gnu.org \
--cc=jim.cromie@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).