可以用可移植的C语言编写从double到int的转换吗 [英] Can a conversion from double to int be written in portable C
问题描述
我需要编写类似double_to_int(double val, int *err)
的函数
在可能的情况下将double val转换为整数;否则报告错误(NAN/INFs/OUT_OF_RANGE).
I need to write function like double_to_int(double val, int *err)
which
would covert double val to integer when it's possible; otherwise report an error (NAN/INFs/OUT_OF_RANGE).
因此伪代码实现将如下所示:
so pseudo code implementation would look like:
if isnan(val):
err = ERR_NAN
return 0
if val < MAX_INT:
err = ERR_MINUS_INF
return MIN_INT
if ...
return (int)val
关于SO至少有两个类似的问题:
在此答案中,尽管它是C ++解决方案-在C中,我们没有用于签名int的可移植数字.
在此答案中,解释了为什么我们不能仅检查(val > INT_MAX || val < INT_MIN)
.
There are at least two similar questions on SO:
in this answer it's solved in enough clean way, though it's C++ solution - in C we do not have portable digits for signed int.
In this answer, it's explained why we cannot just check (val > INT_MAX || val < INT_MIN)
.
因此,我看到的唯一可能的干净方法是使用浮点环境,但这被声明为实现定义的功能.
So the only possible clean way i see is to use floating point environment, but it's stated as implementation-defined feature.
所以我的问题是:有没有办法以跨平台的方式实现double_to_int
功能(仅基于C标准,甚至没有考虑
目标平台以支持IEEE-754)??
So my question: is there any way to implement double_to_int
function in cross-platform way (basing only on C standard, even not considering
target platforms to support IEEE-754).?
推荐答案
[此答案已经使用全新的方法进行了编辑.]
[This answer has been edited with a completely new approach.]
此方法使用C标准中的浮点格式定义-作为有符号的base- b 数字乘以 b 的幂.知道有效位数(由DBL_MANT_DIG
提供)和指数极限(由DBL_MAX_EXP
提供)的位数,可以让我们准备精确的double
值作为端点.
This approach uses the definition of floating-point formats in the C standard—as a signed base-b numeral multiplied by a power of b. Knowing the number of digits in the significand (provided by DBL_MANT_DIG
) and the exponent limit (provided by DBL_MAX_EXP
) allows us to prepare exact double
values as end points.
我相信它可以在所有符合标准的C实现中使用,但要符合初始评论中所述的适度附加要求.
I believe it will work in all conforming C implementations subject to the modest additional requirements stated in the initial comment.
/* This code demonstrates safe conversion of double to int in which the
input double is converted to int if and only if it is in the supported
domain for such conversions (the open interval (INT_MIN-1, INT_MAX+1)).
If the input is not in range, an error is indicated (by way of an
auxiliary argument) and no conversion is performed, so all behavior is
defined.
There are a few requirements not fully covered by the C standard. They
should be uncontroversial and supported by all reasonable C implementations:
Conversion of an int that is representable in double produces the
exact value.
The following operations are exact in floating-point:
Dividing by the radix of the floating-point format, within its
range.
Multiplying by +1 or -1.
Adding or subtracting two values whose sum or difference is
representable.
FLT_RADIX is representable in int.
DBL_MIN_EXP is not greater than -DBL_MANT_DIG. (The code can be
modified to eliminate this requirement.)
Deviations from the requested routine include:
This code names the routine DoubleToInt instead of double_to_int.
The only error indicated is ERANGE. Code to distinguish the error more
finely, such as providing separate values for NaNs, infinities, and
out-of-range finite values, could easily be added.
*/
#include <float.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
/* These values will be initialized to the greatest double value not greater
than INT_MAX+1 and the least double value not less than INT_MIN-1.
*/
static double UpperBound, LowerBound;
/* Return the double of the same sign of x that has the greatest magnitude
less than x+s, where s is -1 or +1 according to whether x is negative or
positive.
*/
static double BiggestDouble(int x)
{
/* All references to "digits" in this routine refer to digits in base
FLT_RADIX. For example, in base 3, 77 would have four digits (2212).
In this routine, "bigger" and "smaller" refer to magnitude. (3 is
greater than -4, but -4 is bigger than 3.)
*/
// Determine the sign.
int s = 0 < x ? +1 : -1;
// Count how many digits x has.
int digits = 0;
for (int t = x; t; ++digits)
t /= FLT_RADIX;
/* If the double type cannot represent finite numbers this big, return the
biggest finite number it can hold, with the desired sign.
*/
if (DBL_MAX_EXP < digits)
return s*DBL_MAX;
// Determine whether x is exactly representable in double.
if (DBL_MANT_DIG < digits)
{
/* x is not representable, so we will return the next lower
representable value by removing just as many low digits as
necessary. Note that x+s might be representable, but we want to
return the biggest double less than it, which is also the biggest
double less than x.
*/
/* Figure out how many digits we have to remove to leave at most
DBL_MANT_DIG digits.
*/
digits = digits - DBL_MANT_DIG;
// Calculate FLT_RADIX to the power of digits.
int t = 1;
while (digits--) t *= FLT_RADIX;
return x / t * t;
}
else
{
/* x is representable. To return the biggest double smaller than
x+s, we will fill the remaining digits with FLT_RADIX-1.
*/
// Figure out how many additional digits double can hold.
digits = DBL_MANT_DIG - digits;
/* Put a 1 in the lowest available digit, then subtract from 1 to set
each digit to FLT_RADIX-1. (For example, 1 - .001 = .999.)
*/
double t = 1;
while (digits--) t /= FLT_RADIX;
t = 1-t;
// Return the biggest double smaller than x+s.
return x + s*t;
}
}
/* Set up supporting data for DoubleToInt. This should be called once prior
to any call to DoubleToInt.
*/
static void InitializeDoubleToInt(void)
{
UpperBound = BiggestDouble(INT_MAX);
LowerBound = BiggestDouble(INT_MIN);
}
/* Perform the conversion. If the conversion is possible, return the
converted value and set *error to zero. Otherwise, return zero and set
*error to ERANGE.
*/
static int DoubleToInt(double x, int *error)
{
if (LowerBound <= x && x <= UpperBound)
{
*error = 0;
return x;
}
else
{
*error = ERANGE;
return 0;
}
}
#include <string.h>
static void Test(double x)
{
int error, y;
y = DoubleToInt(x, &error);
printf("%.99g -> %d, %s.\n", x, y, error ? strerror(error) : "No error");
}
#include <math.h>
int main(void)
{
InitializeDoubleToInt();
printf("UpperBound = %.99g\n", UpperBound);
printf("LowerBound = %.99g\n", LowerBound);
Test(0);
Test(0x1p31);
Test(nexttoward(0x1p31, 0));
Test(-0x1p31-1);
Test(nexttoward(-0x1p31-1, 0));
}
这篇关于可以用可移植的C语言编写从double到int的转换吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!