如何使用直接计算确定浮点类型的范围?
我正在尝试解决《C 编程语言》第二版中的练习 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: %d\n", ch);
--ch;
printf("Maximum range of signed char variable: %d\n", ch);
--uch;
printf("Maximum range of unsigned char variable: %u\n", 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: %d\n", sh);
--sh;
printf("Maximum range of signed short variable: %d\n", sh);
--ush;
printf("Maximum range of unsigned short variable: %u\n", 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: %d\n", i);
--i;
printf("Maximum range of signed int variable: %d\n", i);
--ui;
printf("Maximum range of unsigned int variable: %u\n", 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: %d\n", l);
--l;
printf("Maximum range of signed long variable: %d\n", l);
--ul;
printf("Maximum range of unsigned long variable: %lu\n", 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: %lld\n", ll);
--ll;
printf("Maximum range of signed long long variable: %lld\n", ll);
--ull;
printf("Maximum range of unsigned long long variable: %llu\n", ull);
printf("\nSize of char: %d\n", CHAR_BIT); /* Max and min ranges using limits.h and float.h header */
printf("Minimum range of signed char variable: %d\n", SCHAR_MIN);
printf("Maximum range of signed char variable: %d\n", SCHAR_MAX);
printf("Maximum range of unsigned char variable: %u\n", UCHAR_MAX);
printf("Minimum range of signed short variable: %d\n", SHRT_MIN);
printf("Maximum range of signed short variable: %d\n", SHRT_MAX);
printf("Maximum range of unsigned short variable: %u\n", USHRT_MAX);
printf("Minimum range of int variable: %d\n", INT_MIN);
printf("Maximum range of int variable: %d\n", INT_MAX);
printf("Maximum range of unsigned int variable: %u\n", UINT_MAX);
printf("Minimum range of signed long variable: %ld\n", LONG_MIN);
printf("Maximum range of signed long variable: %ld\n", LONG_MAX);
printf("Maximum range of unsigned long variable: %lu\n", ULONG_MAX);
printf("Minimum range of long long variable: %lld\n", LLONG_MIN);
printf("Maximum range of long long variable: %lld\n", LLONG_MAX);
printf("Maximum range of unsigned long long variable: %llu\n\n", ULONG_LONG_MAX);
printf("Minimum range of float variable: %f\n", FLT_MIN);
printf("Maximum range of float variable: %f\n", FLT_MAX);
printf("Minimum range of double variable: %f\n", DBL_MIN);
printf("Maximum range of double variable: %f\n", DBL_MAX);
printf("Minimum range of long double variable: %lf\n", LDBL_MIN);
printf("Maximum range of long double variable: %lf\n", LDBL_MAX);
return 0;
}
I'm trying to solve exercise 2-1 from "The C Programming Language", 2nd edition, which asks to:
"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."
I've managed to determine the ranges of all types except floating-points, both by using minimum and maximum values from standard headers and by direct computation.
How do I determine the ranges of floating-point types using direct computation?
#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: %d\n", ch);
--ch;
printf("Maximum range of signed char variable: %d\n", ch);
--uch;
printf("Maximum range of unsigned char variable: %u\n", 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: %d\n", sh);
--sh;
printf("Maximum range of signed short variable: %d\n", sh);
--ush;
printf("Maximum range of unsigned short variable: %u\n", 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: %d\n", i);
--i;
printf("Maximum range of signed int variable: %d\n", i);
--ui;
printf("Maximum range of unsigned int variable: %u\n", 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: %d\n", l);
--l;
printf("Maximum range of signed long variable: %d\n", l);
--ul;
printf("Maximum range of unsigned long variable: %lu\n", 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: %lld\n", ll);
--ll;
printf("Maximum range of signed long long variable: %lld\n", ll);
--ull;
printf("Maximum range of unsigned long long variable: %llu\n", ull);
printf("\nSize of char: %d\n", CHAR_BIT); /* Max and min ranges using limits.h and float.h header */
printf("Minimum range of signed char variable: %d\n", SCHAR_MIN);
printf("Maximum range of signed char variable: %d\n", SCHAR_MAX);
printf("Maximum range of unsigned char variable: %u\n", UCHAR_MAX);
printf("Minimum range of signed short variable: %d\n", SHRT_MIN);
printf("Maximum range of signed short variable: %d\n", SHRT_MAX);
printf("Maximum range of unsigned short variable: %u\n", USHRT_MAX);
printf("Minimum range of int variable: %d\n", INT_MIN);
printf("Maximum range of int variable: %d\n", INT_MAX);
printf("Maximum range of unsigned int variable: %u\n", UINT_MAX);
printf("Minimum range of signed long variable: %ld\n", LONG_MIN);
printf("Maximum range of signed long variable: %ld\n", LONG_MAX);
printf("Maximum range of unsigned long variable: %lu\n", ULONG_MAX);
printf("Minimum range of long long variable: %lld\n", LLONG_MIN);
printf("Maximum range of long long variable: %lld\n", LLONG_MAX);
printf("Maximum range of unsigned long long variable: %llu\n\n", ULONG_LONG_MAX);
printf("Minimum range of float variable: %f\n", FLT_MIN);
printf("Maximum range of float variable: %f\n", FLT_MAX);
printf("Minimum range of double variable: %f\n", DBL_MIN);
printf("Maximum range of double variable: %f\n", DBL_MAX);
printf("Minimum range of long double variable: %lf\n", LDBL_MIN);
printf("Maximum range of long double variable: %lf\n", LDBL_MAX);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些天我也做了同样的练习。我设法在假设 2 补码和 IEEE 浮点(单精度和双精度)的情况下计算机器上浮点数和双精度数的大小。
如果有任何反馈,特别是有关浮点范围的计算的反馈,我将不胜感激。
I did the same exercise these days. I managed to compute the sizes of floats and doubles on my machine assuming 2-complement and IEEE floating point (single and double precision).
I would appreciate any feedback, in particular about the computation of the floating point ranges.
首先,您的代码中似乎存在错误。要获得最大值,您需要
在修复该问题后,对浮点数使用相同的公式。
first off, it seems you have bugs in the code. to get max, you need
once fixed that, use the same formula on floating point number.