C语言 浮点类型的限制?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4786663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:36:13  来源:igfitidea点击:

Limits for floating point types?

ctypesfloating-point

提问by ferz

#include <stdio.h>
#include <limits.h>

int main(void){
        printf("Type                Size      Min                 Max\n----------------------------------------------------------------------\n");
        printf("%-20s%-10d%-20ld%-20ld\n", "long", sizeof(long), LONG_MIN, LONG_MAX);
        printf("%-20s%-10d%-20lu%-20lu\n", "unsigned long", sizeof(long), 0, ULONG_MAX);
        return 0;
}

where double? i.e. variable LONG_MIN be in file limits.h. in which type double?

哪里双?即变量 LONG_MIN 位于文件limits.h 中。在哪种类型双?

   int i, min, max;

    for (i = 1.0; i > 0; ++i)
    {
        max = i;
    };
    min = i;
    printf ("int: min: %d max: %d \n", min, max);

how do for float and double? how min calculated this variable? sorry bad english

float 和 double 怎么做?min 是如何计算这个变量的?抱歉英语不好

回答by Clifford

The limits for floating point types are defined in float.hnot limits.h

浮点类型的限制在float.h而非limits.h中定义

回答by vmpstr

on linux, I have float.h which has FLT_MAX and DBL_MAX defined for maximum float and double values respectively. I'm not sure how "standard" that is, though...

在 linux 上,我有 float.h,它分别为最大浮点数和双精度值定义了 FLT_MAX 和 DBL_MAX。不过,我不确定那是多么“标准”...

回答by Nick Banks

I think this is what you want:

我认为这就是你想要的:

float: %f

浮点数:%f

long float (double): %lf

长浮点数(双精度):%lf

You may also want to see it in exponential notation: %E

您可能还想以指数表示法查看它:%E

For min/max for float and double here is what you want

对于 float 和 double 的最小值/最大值,这是您想要的

Here's a snippet from float.h:

这是来自 float.h 的片段:

#define DBL_MAX 1.7976931348623158e+308 /* max value */
#define DBL_MIN 2.2250738585072014e-308 /* min positive value */

#define FLT_MAX 3.402823466e+38F /* max value */
#define FLT_MIN 1.175494351e-38F /* min positive value */

回答by BlackBear

To compute the limits for a given data-type, you have to compute simply (2^(sizeof(type) * 8)) - 1, which is (2^number_of_bits) - 1.

要计算给定数据类型的限制,您必须简单地计算(2^(sizeof(type) * 8)) - 1,即(2^number_of_bits) - 1

Then, if you consider this type to be signed min and max values are -2^(number_of_bits - 1)and (2^(number_of_bits - 1)) - 1or if they're unsigned MIN will be 0 and MAX (2^number_of_bits) - 1.

然后,如果您认为这种类型是有符号的 min 和 max 值是-2^(number_of_bits - 1)(2^(number_of_bits - 1)) - 1或者如果它们是无符号的 MIN 将是 0 和 MAX (2^number_of_bits) - 1

This applies only to integer types, so not for floats and doubles, and then only for Two's Complement integer representations.

这仅适用于整数类型,因此不适用于浮点数和双精度数,然后仅适用于二进制补码整数表示。