如何找到特定机器上的尾数长度? [英] How to find mantissa length on a particular machine?

查看:82
本文介绍了如何找到特定机器上的尾数长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到特定计算机上的尾数位数和四舍五入单位.我了解它们是什么,但不知道如何找到它们-尽管我知道它们可能因计算机而异.

我需要这个数字才能执行数值分析的某些方面,例如分析错误.

我目前在想的是,我可以编写一个小型的c ++程序来逐渐增加一个数字,直到出现溢出为止,但是我不确定要使用哪种数字.

我在正确的轨道上吗?究竟该如何计算呢?

解决方案

我认为您使用的任何语言都将指定浮点数的存储方式.我知道Java通过使用特定的IEEE标准(我认为是754)来做到这一点.

如果未指定,我想您可以通过将0.5加1来进行实际检查,以查看实际数字是否发生了变化.如果是这样,则将0.25加1,将0.125加1,依此类推,直到数字不变为止,例如:

float a = 1;
float b = 0.5;
int bits = 0;
while (a + b != a) {
    bits = bits + 1;
    b = b / 2;
}

如果只有3个尾数位,则1 + 1/16将等于1.

然后,您已经用尽了尾数.

实际上,您可能需要将基数设为2而不是1,因为IEEE754在一开始就使用了隐含的"1 +".

上述方法似乎有一些问题,因为它为明显具有4字节浮点数的系统提供了63位.

这是否与中间结果有关(我怀疑这是因为具有显式强制转换[while (((float)(a + b) != (float)(a))]的相同代码也存在类似的问题)还是(更可能的是,我相信)可以表示单位值a的可能性我还不知道通过调整指数使其更接近小数b.

目前,最好依靠我上面提到的语言信息,例如使用IEEE754(如果该信息可用).

我会将有问题的代码留给谨慎的玩家使用.也许某人具有更多的浮点知识,那么我可以留下一条注释来解释为什么它的行为异常(不要猜想,请:-).

这段代码通过确保将中间体存储在浮点中来对其进行修复.事实证明,乔纳森·莱夫勒(Jonathan Leffler)是对的-这是中间结果.

#include <stdio.h>
#include <float.h>

int main(void) {
    float a = 1;
    float b = 0.5;
    float c = a + b;
    int bits = 1;
    while (c != a) {
        bits = bits + 1;
        b = b / 2;
        c = a + b;
    }
    printf("%d\n",FLT_MANT_DIG);
    printf("%d\n",bits);
    return 0;

}

此代码输出(24,24),以显示计算出的值与头文件中的值匹配.

尽管使用C语言编写,但它应适用于任何语言(特别是在标头中不可用或由于语言文档中指定了信息而无法使用的一种语言).我之所以只在C语言中进行测试,是因为Eclipse需要很长时间才能在我的Ubuntu盒中启动:-).

I'm wanting to find the number of mantissa digits and the unit round-off on a particular computer. I have an understanding of what these are, just no idea how to find them - though I understand they can vary from computer to computer.

I need this number in order to perform certain aspects of numerical analysis, like analyzing errors.

What I'm currently thinking is that I could write a small c++ program to slowly increment a number until overflow occurs, but I'm not sure what type of number to use.

Am I on the right track? How exactly does one go about calculating this?

解决方案

I would think that whatever language you were using would specify how floats were stored. I know Java does this by use of a specific IEEE standard (754, I think).

If it's not specified, I would think you could just do your own check by adding 0.5 to 1 to see if the actual number changes. If it does, then add 0.25 to 1, the 0.125 to 1, and so on until the number doesn't change, something like:

float a = 1;
float b = 0.5;
int bits = 0;
while (a + b != a) {
    bits = bits + 1;
    b = b / 2;
}

If you only had 3 mantissa bits, then 1 + 1/16 would be equal to 1.

Then you've exhausted your mantissa bits.

You might actually need the base number to be 2 rather than 1, since IEEE754 uses an implied '1+' at the start.

EDIT:

It appears the method described above may have some issues as it gives 63 bits for a system that clearly has 4-byte floats.

Whether that's to do with intermediate results (I doubt it since the same code with explicit casts [while (((float)(a + b) != (float)(a))] has similar problems) or (more likely, I believe) the possibility that the unit value a can be represented with bits closer to the fractional b by adjusting the exponent, I don't yet know.

For now, it's best to rely on the language information I mentioned above such as use of IEEE754 (if that information is available).

I'll leave the problematic code in as a trap for wary players. Maybe someone with more floating point knowledge then I can leave a note explaining why it acts strangely (no conjecture, please :-).

EDIT 2:

This piece of code fixes it by ensuring intermediates are stored in floats. Turns out Jonathan Leffler was right - it was intermediate results.

#include <stdio.h>
#include <float.h>

int main(void) {
    float a = 1;
    float b = 0.5;
    float c = a + b;
    int bits = 1;
    while (c != a) {
        bits = bits + 1;
        b = b / 2;
        c = a + b;
    }
    printf("%d\n",FLT_MANT_DIG);
    printf("%d\n",bits);
    return 0;

}

This code outputs (24,24) to show that the calculated value matches the one in the header file.

Whilst written in C, it should be applicable to any language (specifically one where the information isn't available in a header or by virtue that it's specified in the language documentation). I only tested in C because Eclipse takes so long to start on my Ubuntu box :-).

这篇关于如何找到特定机器上的尾数长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆