如何确定浮动范围
我试图从“The C Programming Language”第二版中解决练习2-1,它要求:
“编写一个程序,通过从标准头文件和直接计算中打印适当的值来确定char,short,int和long变量(有符号和无符号)的范围,如果计算它们,则更难:确定各种浮点数的范围,点类型“。
我已经设法确定除了浮点之外的所有类型的范围,都使用标准头文件中的最小值和最大值以及直接计算。
如何使用直接计算确定浮点类型的范围?
#include <stdio.h>
#include <limits.h>
#include <float.h>
#define TESTBIT 2
/* write a program to determine the ranges of char, short, int, and long variables,
both signed and unsigned by printing appropriate values from standard headers
and by direct computation
harder if you compute them - determine the ranges of the various floating-point types */
main()
{
char ch, chtest;
unsigned char uch;
short sh, shtest;
unsigned short ush;
int i, itest;
unsigned int ui;
long l, ltest;
unsigned long ul;
long long ll, lltest;
unsigned long long ull;
ch = uch = sh = ush = i = ui = l = ul = ll = ull = 0;
++ch; /* Maximum and minimum ranges using direct computation */
chtest = 0;
while (chtest >= 0) {
chtest = ch * TESTBIT;
if (chtest > 0)
ch = ch * TESTBIT;
}
ch = ch * 2;
printf("Minimum range of signed char variable: %dn", ch);
--ch;
printf("Maximum range of signed char variable: %dn", ch);
--uch;
printf("Maximum range of unsigned char variable: %un", uch);
++sh;
shtest = 0;
while (shtest >= 0) {
shtest = sh * TESTBIT;
if (shtest > 0)
sh = sh * TESTBIT;
}
sh = sh * 2;
printf("Minimum range of signed short variable: %dn", sh);
--sh;
printf("Maximum range of signed short variable: %dn", sh);
--ush;
printf("Maximum range of unsigned short variable: %un", ush);
++i;
itest = 0;
while (itest >= 0) {
itest = i * TESTBIT;
if (itest > 0)
i = i * TESTBIT;
}
i = i * 2;
printf("Minimum range of signed int variable: %dn", i);
--i;
printf("Maximum range of signed int variable: %dn", i);
--ui;
printf("Maximum range of unsigned int variable: %un", ui);
++l;
ltest = 0;
while (ltest >= 0) {
ltest = l * TESTBIT;
if (ltest > 0)
l = l * TESTBIT;
}
l = l * 2;
printf("Minimum range of signed long variable: %dn", l);
--l;
printf("Maximum range of signed long variable: %dn", l);
--ul;
printf("Maximum range of unsigned long variable: %lun", ul);
++ll;
lltest = 0;
while (lltest >= 0) {
lltest = ll * TESTBIT;
if (lltest > 0)
ll = ll * TESTBIT;
}
ll = ll * 2;
printf("Minimum range of signed long long variable: %lldn", ll);
--ll;
printf("Maximum range of signed long long variable: %lldn", ll);
--ull;
printf("Maximum range of unsigned long long variable: %llun", ull);
printf("nSize of char: %dn", CHAR_BIT); /* Max and min ranges using limits.h and float.h header */
printf("Minimum range of signed char variable: %dn", SCHAR_MIN);
printf("Maximum range of signed char variable: %dn", SCHAR_MAX);
printf("Maximum range of unsigned char variable: %un", UCHAR_MAX);
printf("Minimum range of signed short variable: %dn", SHRT_MIN);
printf("Maximum range of signed short variable: %dn", SHRT_MAX);
printf("Maximum range of unsigned short variable: %un", USHRT_MAX);
printf("Minimum range of int variable: %dn", INT_MIN);
printf("Maximum range of int variable: %dn", INT_MAX);
printf("Maximum range of unsigned int variable: %un", UINT_MAX);
printf("Minimum range of signed long variable: %ldn", LONG_MIN);
printf("Maximum range of signed long variable: %ldn", LONG_MAX);
printf("Maximum range of unsigned long variable: %lun", ULONG_MAX);
printf("Minimum range of long long variable: %lldn", LLONG_MIN);
printf("Maximum range of long long variable: %lldn", LLONG_MAX);
printf("Maximum range of unsigned long long variable: %llunn", ULONG_LONG_MAX);
printf("Minimum range of float variable: %fn", FLT_MIN);
printf("Maximum range of float variable: %fn", FLT_MAX);
printf("Minimum range of double variable: %fn", DBL_MIN);
printf("Maximum range of double variable: %fn", DBL_MAX);
printf("Minimum range of long double variable: %lfn", LDBL_MIN);
printf("Maximum range of long double variable: %lfn", LDBL_MAX);
return 0;
}
#include <stdio.h>
main()
{
float fl, fltest, last;
double dbl, dbltest, dblast;
fl = 0.0;
fltest = 0.0;
while (fl == 0.0) {
last = fltest;
fltest = fltest + 1111e28;
fl = (fl + fltest) - fltest;
}
printf("Maximum range of float variable: %en", last);
dbl = 0.0;
dbltest = 0.0;
while (dbl == 0.0) {
dblast = dbltest;
dbltest = dbltest + 1111e297;
dbl = (dbl + dbltest) - dbltest;
}
printf("Maximum range of double variable: %en", dblast);
return 0;
}
我最近做了同样的练习。 我设法计算我的机器上浮点和双精度的大小,假设2补充和IEEE浮点(单精度和双精度)。
我会很感激任何反馈,特别是关于浮点范围的计算。
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
#include <math.h>
int main(int argc, char** argv) {
printf("-----------Limits Header-----------n");
printf("CHAR_BIT : %dn", CHAR_BIT);
printf("CHAR_MAX : %dn", CHAR_MAX);
printf("CHAR_MIN : %dn", CHAR_MIN);
printf("INT_MAX : %dn", INT_MAX);
printf("INT_MIN : %dn", INT_MIN);
printf("LONG_MAX : %ldn", (long) LONG_MAX);
printf("LONG_MIN : %ldn", (long) LONG_MIN);
printf("SCHAR_MAX : %dn", SCHAR_MAX);
printf("SCHAR_MIN : %dn", SCHAR_MIN);
printf("SHRT_MAX : %dn", SHRT_MAX);
printf("SHRT_MIN : %dn", SHRT_MIN);
printf("UCHAR_MAX : %dn", UCHAR_MAX);
printf("UINT_MAX : %un", (unsigned int) UINT_MAX);
printf("ULONG_MAX : %lun", (unsigned long) ULONG_MAX);
printf("USHRT_MAX : %dn", (unsigned short) USHRT_MAX);
printf("FLT_MAX : %gn", (float) FLT_MAX);
printf("-FLT_MAX : %gn", (float) -FLT_MAX);
printf("FLT_MIN : %gn", (float) FLT_MIN);
printf("-FLT_MIN : %gn", (float) -FLT_MIN);
printf("DBL_MAX : %gn", (double) DBL_MAX);
printf("-DBL_MAX : %gn", (double) -DBL_MAX);
printf("DBL_MIN : %gn", (double) DBL_MIN);
printf("-DBL_MIN : %gn", (double) -DBL_MIN);
printf("n");
printf("--------Assuming 2-complement--------n");
printf("-----------Computed limits-----------n");
char c = 1;
while (c > 0)
c *= 2;
c--;
printf("Char max : %dn", c);
c = 1;
while (c > 0)
c *= 2;
printf("Char min : %dn", c);
int i = 1;
while (i > 0)
i *= 2;
i--;
printf("Int max : %dn", i);
i = 1;
while (i > 0)
i *= 2;
printf("Int min : %dn", i);
long l = 1;
while (l > 0)
l *= 2;
l--;
printf("Long max : %ldn", l);
l = 1;
while (l > 0)
l *= 2;
printf("Long min : %ldn", l);
signed char sc = 1;
while (sc > 0)
sc *= 2;
sc--;
printf("Sig Char max: %dn", sc);
sc = 1;
while (sc > 0)
sc *= 2;
printf("Sig Char min: %dn", sc);
short si = 1;
while (si > 0)
si *= 2;
si--;
printf("Sig Int max : %dn", si);
si = 1;
while (si > 0)
si *= 2;
printf("Sig Int min : %dn", si);
unsigned char uc = 1;
while (uc > 0)
uc *= 2;
uc--;
printf("U Char max : %dn", uc);
unsigned int ui = 1;
while (ui > 0)
ui *= 2;
ui--;
printf("U Int max : %un", ui);
unsigned long ul = 1;
while (ul > 0)
ul *= 2;
ul--;
printf("U Long max : %lun", ul);
unsigned short us = 1;
while (us > 0)
us *= 2;
us--;
printf("U Short max : %un", us);
union {
int i;
float f;
} uf;
float f;
for (f = 1; f < INFINITY; uf.f = f, f *= 1e1);
for (i = 2, f = uf.f; f * i < INFINITY; uf.f = f * i, i++);
uf.i = uf.i | 0x7FFFFFu; // Mantissa for float is 23 bit on my machine
printf("FLT max : %g (float)n", uf.f);
printf("-FLT max : %g (float)n", -uf.f);
uf.i = uf.i & 0u;
uf.i = uf.i | 0x7FFFFFu;
printf("FLT min : %g (float)n", uf.f);
printf("-FLT min : %g (float)n", -uf.f);
union {
long int i;
double d;
} ud;
double d;
for (d = 1; d < INFINITY; ud.d = d, d *= 1e1);
for (i = 2, d = ud.d; d * i < INFINITY; ud.d = d * i, i++);
ud.i = ud.i | 0xFFFFFFFFFFFFFlu; // Mantissa for double is 52 bit on my machine
printf("DBL max : %g (double) n", ud.d);
printf("-DBL max : %g (double)n", -ud.d);
ud.i = ud.i & 0u;
ud.i = ud.i | 0xFFFFFFFFFFFFFlu;
printf("DBL min : %g (double)n", ud.d);
printf("-DBL min : %g (double)n", -ud.d);
return (EXIT_SUCCESS);
}
首先,看起来你有代码中的错误。 得到最大,你需要
x = 1;
while(x*2>x)x = x*2 + 1;
一旦解决了,在浮点数上使用相同的公式。
链接地址: http://www.djcxy.com/p/90297.html上一篇: How to determine the ranges of floating
下一篇: printf by given pointer and format string. Issue with floats