blob: 732c7d3e4c3ce22c3f9d92c0f6d7cc4fa763a81f [file] [log] [blame]
<html lang="en">
<head>
<title>FP Bit Twiddling - The GNU C Library</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="The GNU C Library">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Arithmetic-Functions.html#Arithmetic-Functions" title="Arithmetic Functions">
<link rel="prev" href="Remainder-Functions.html#Remainder-Functions" title="Remainder Functions">
<link rel="next" href="FP-Comparison-Functions.html#FP-Comparison-Functions" title="FP Comparison Functions">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This file documents the GNU C library.
This is Edition 0.12, last updated 2007-10-27,
of `The GNU C Library Reference Manual', for version
2.8 (Sourcery G++ Lite 2011.03-41).
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007, 2008, 2010 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``Free Software Needs Free Documentation''
and ``GNU Lesser General Public License'', the Front-Cover texts being
``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A
copy of the license is included in the section entitled "GNU Free
Documentation License".
(a) The FSF's Back-Cover Text is: ``You have the freedom to
copy and modify this GNU manual. Buying copies from the FSF
supports it in developing GNU and promoting software freedom.''-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
<link rel="stylesheet" type="text/css" href="../cs.css">
</head>
<body>
<div class="node">
<a name="FP-Bit-Twiddling"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="FP-Comparison-Functions.html#FP-Comparison-Functions">FP Comparison Functions</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Remainder-Functions.html#Remainder-Functions">Remainder Functions</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Arithmetic-Functions.html#Arithmetic-Functions">Arithmetic Functions</a>
<hr>
</div>
<h4 class="subsection">20.8.5 Setting and modifying single bits of FP values</h4>
<p><a name="index-FP-arithmetic-2481"></a>
There are some operations that are too complicated or expensive to
perform by hand on floating-point numbers. ISO&nbsp;C99<!-- /@w --> defines
functions to do these operations, which mostly involve changing single
bits.
<!-- math.h -->
<!-- ISO -->
<div class="defun">
&mdash; Function: double <b>copysign</b> (<var>double x, double y</var>)<var><a name="index-copysign-2482"></a></var><br>
<blockquote><!-- math.h -->
<!-- ISO -->
&mdash; Function: float <b>copysignf</b> (<var>float x, float y</var>)<var><a name="index-copysignf-2483"></a></var><br>
<blockquote><!-- math.h -->
<!-- ISO -->
&mdash; Function: long double <b>copysignl</b> (<var>long double x, long double y</var>)<var><a name="index-copysignl-2484"></a></var><br>
<blockquote><p>These functions return <var>x</var> but with the sign of <var>y</var>. They work
even if <var>x</var> or <var>y</var> are NaN or zero. Both of these can carry a
sign (although not all implementations support it) and this is one of
the few operations that can tell the difference.
<p><code>copysign</code> never raises an exception.
<!-- except signalling NaNs -->
<p>This function is defined in IEC&nbsp;559<!-- /@w --> (and the appendix with
recommended functions in IEEE&nbsp;754<!-- /@w -->/IEEE&nbsp;854<!-- /@w -->).
</p></blockquote></div>
<!-- math.h -->
<!-- ISO -->
<div class="defun">
&mdash; Function: int <b>signbit</b> (<em>float-type</em><var> x</var>)<var><a name="index-signbit-2485"></a></var><br>
<blockquote><p><code>signbit</code> is a generic macro which can work on all floating-point
types. It returns a nonzero value if the value of <var>x</var> has its sign
bit set.
<p>This is not the same as <code>x &lt; 0.0</code>, because IEEE&nbsp;754<!-- /@w --> floating
point allows zero to be signed. The comparison <code>-0.0 &lt; 0.0</code> is
false, but <code>signbit (-0.0)</code> will return a nonzero value.
</p></blockquote></div>
<!-- math.h -->
<!-- ISO -->
<div class="defun">
&mdash; Function: double <b>nextafter</b> (<var>double x, double y</var>)<var><a name="index-nextafter-2486"></a></var><br>
<blockquote><!-- math.h -->
<!-- ISO -->
&mdash; Function: float <b>nextafterf</b> (<var>float x, float y</var>)<var><a name="index-nextafterf-2487"></a></var><br>
<blockquote><!-- math.h -->
<!-- ISO -->
&mdash; Function: long double <b>nextafterl</b> (<var>long double x, long double y</var>)<var><a name="index-nextafterl-2488"></a></var><br>
<blockquote><p>The <code>nextafter</code> function returns the next representable neighbor of
<var>x</var> in the direction towards <var>y</var>. The size of the step between
<var>x</var> and the result depends on the type of the result. If
<var>x</var> = <var>y</var> the function simply returns <var>y</var>. If either
value is <code>NaN</code>, <code>NaN</code> is returned. Otherwise
a value corresponding to the value of the least significant bit in the
mantissa is added or subtracted, depending on the direction.
<code>nextafter</code> will signal overflow or underflow if the result goes
outside of the range of normalized numbers.
<p>This function is defined in IEC&nbsp;559<!-- /@w --> (and the appendix with
recommended functions in IEEE&nbsp;754<!-- /@w -->/IEEE&nbsp;854<!-- /@w -->).
</p></blockquote></div>
<!-- math.h -->
<!-- ISO -->
<div class="defun">
&mdash; Function: double <b>nexttoward</b> (<var>double x, long double y</var>)<var><a name="index-nexttoward-2489"></a></var><br>
<blockquote><!-- math.h -->
<!-- ISO -->
&mdash; Function: float <b>nexttowardf</b> (<var>float x, long double y</var>)<var><a name="index-nexttowardf-2490"></a></var><br>
<blockquote><!-- math.h -->
<!-- ISO -->
&mdash; Function: long double <b>nexttowardl</b> (<var>long double x, long double y</var>)<var><a name="index-nexttowardl-2491"></a></var><br>
<blockquote><p>These functions are identical to the corresponding versions of
<code>nextafter</code> except that their second argument is a <code>long
double</code>.
</p></blockquote></div>
<p><a name="index-NaN-2492"></a><!-- math.h -->
<!-- ISO -->
<div class="defun">
&mdash; Function: double <b>nan</b> (<var>const char *tagp</var>)<var><a name="index-nan-2493"></a></var><br>
<blockquote><!-- math.h -->
<!-- ISO -->
&mdash; Function: float <b>nanf</b> (<var>const char *tagp</var>)<var><a name="index-nanf-2494"></a></var><br>
<blockquote><!-- math.h -->
<!-- ISO -->
&mdash; Function: long double <b>nanl</b> (<var>const char *tagp</var>)<var><a name="index-nanl-2495"></a></var><br>
<blockquote><p>The <code>nan</code> function returns a representation of NaN, provided that
NaN is supported by the target platform.
<code>nan ("</code><var>n-char-sequence</var><code>")</code> is equivalent to
<code>strtod ("NAN(</code><var>n-char-sequence</var><code>)")</code>.
<p>The argument <var>tagp</var> is used in an unspecified manner. On IEEE&nbsp;754<!-- /@w --> systems, there are many representations of NaN, and <var>tagp</var>
selects one. On other systems it may do nothing.
</p></blockquote></div>
</body></html>