一些浮点问题 [英] A few floating point issues

查看:67
本文介绍了一些浮点问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一些浮点问题/问题。一个答案或

讨论也会很好:)任何链接讨论主题将是

也很好..


1 。如何为测试目的生成下溢异常?

2.抑制浮点异常的最快值是多少?

以下列情况为例。 />
float x = 1.0e38,// max float value

y = 1.0e-45,// min float value

z = 0.0f; //无用的任务


z = x / y; //这应该会产生一个溢出异常


我可以像这样使用异常处理:

float x = 1.0e38,// max float value

y = 1.0e-45,// min float value

z = 0.0f; //无用的任务


尝试{

z = x / y; //这应该会产生溢出异常

}

catch(EOverflow& eo){

//做任何事情

z = 1.0e38;

}

catch(EZeroDivide& ez){

//做任何事情

z = 1.0e38;

}

catch(EUnderflow& eu){

//做任何事情

z = 0.0;

}

或者有更好的方法吗?


3.这个很奇怪,可能是我的编译器本身。当我使用下一个

示例(与第一个相同)并编译它以进行调试或发布时我将获得一个例外,有时我不会。如果它没有,我在调试模式下是
,我注意到z的值没有变化。无论什么

无用的任务的价值是。


浮动x = 1.0e38,//最大浮动值

y = 1.0e-45,// min float value

z = 0.0f; //无用的任务


z = x / y; //这应该会产生溢出

提前致谢..

Greetz,

Vinny

I have a few floating point questions/issues I wish to ask. An answer or
discussion would be nice too :) Any links discussion the subject would be
nice too..

1. How do I generate an underflow exception for testing purposes?
2. What is the fastest wat to suppress floating point exceptions?
Take the following situation as example.
float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

z = x / y; // this should generate a overflow exception

I could use exception handling like so:
float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

try {
z = x / y; // this should generate a overflow exception
}
catch( EOverflow &eo ) {
//do anything
z = 1.0e38;
}
catch( EZeroDivide &ez ) {
//do anything
z = 1.0e38;
}
catch( EUnderflow &eu ) {
//do anything
z = 0.0;
}
Or is there a better method?

3. This one is weird and may be my compiler itself. When i use the next
example (same as the first one) and compile it for debugging or release i
sometimes get an exception and sometimes i don''t. If it didn''t and i was
in debug mode, I notice the value of z was unchanged. No matter what the
"useless assignment"''s value was.

float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

z = x / y; // this should generate a overflow
Thanks in advance..
Greetz,
Vinny

推荐答案

Vinny写道:
我想问一些浮点问题/问题。答案或
讨论也会很好:)任何链接讨论主题都会很好......

1.如何为测试目的生成下溢异常?


我不认为有某种方式,但你可以尝试......


除以一小部分数字,比如


double a = 1e-200;

double b = 1e200;

double ab = a / b;

2.抑制浮点异常的最快值是多少?


语言没有办法做到这一点。它们是平台 -

具体,我相信。

以下列情况为例。
float x = 1.0e38,// max float value
y = 1.0e-45,// min float value
z = 0.0f; //无用的任务

z = x / y; //这应该会产生溢出异常


希望。

我可以像这样使用异常处理:
float x = 1.0e38,// max float value
y = 1.0e-45,// min float value
z = 0.0f; //无用的任务

尝试{
z = x / y; //这应该会产生溢出异常
}
catch(EOverflow& eo){
//做任何事情
z = 1.0e38;
} catch(EZeroDivide& ez){
//做任何事情
z = 1.0e38;
}
catch(EUnderflow& eu){
//做任何事情
z = 0.0;
}


我不这么认为。什么是EOverflow?什么是EZeroDivide?那些是

而不是标准类型。

或者有更好的方法吗?


您必须在新闻组中询问您的编译器。

3.这个很奇怪,可能是我的编译器本身。当我使用下一个
示例(与第一个相同)并编译它以进行调试或释放时我有时会得到一个例外,有时候我不会。如果它没有,我在调试模式下,我注意到z的值没有变化。无论无用的任务的价值是什么。

浮动x = 1.0e38,//最大浮动值
y = 1.0e-45,/ / min浮点值
z = 0.0f; //无用的任务

z = x / y; //这应该会产生溢出
I have a few floating point questions/issues I wish to ask. An answer or
discussion would be nice too :) Any links discussion the subject would be
nice too..

1. How do I generate an underflow exception for testing purposes?
I don''t think there is a certain way, but you could try...

Divide a small number by a big number, like

double a = 1e-200;
double b = 1e200;
double ab = a / b;
2. What is the fastest wat to suppress floating point exceptions?
The language doesn''t have any means to do that. They are platform-
specific, I believe.
Take the following situation as example.
float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

z = x / y; // this should generate a overflow exception
Hopefully.
I could use exception handling like so:
float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

try {
z = x / y; // this should generate a overflow exception
}
catch( EOverflow &eo ) {
//do anything
z = 1.0e38;
}
catch( EZeroDivide &ez ) {
//do anything
z = 1.0e38;
}
catch( EUnderflow &eu ) {
//do anything
z = 0.0;
}
I don''t think so. What''s EOverflow? What''s EZeroDivide? Those are
not Standard types.
Or is there a better method?
You''d have to ask in a newsgroup for your compiler.
3. This one is weird and may be my compiler itself. When i use the next
example (same as the first one) and compile it for debugging or release i
sometimes get an exception and sometimes i don''t. If it didn''t and i was
in debug mode, I notice the value of z was unchanged. No matter what the
"useless assignment"''s value was.

float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

z = x / y; // this should generate a overflow



再一次,这种语言实际上并没有任何捕获硬件的方法

像浮点一样的异常错误。也许你的

编译器的文档有一些信息......

V



Again, the language doesn''t really have any way of capturing hardware
exceptions like floating point errors. Perhaps the documentation for your
compiler has some information...

V


Victor Bazarov <五******** @ comAcast.net>写在

新闻:Nr **************** @ newsread1.dllstx09.us.to.v erio.net:
Victor Bazarov <v.********@comAcast.net> wrote in
news:Nr****************@newsread1.dllstx09.us.to.v erio.net:
Vinny写道:
我想问一些浮点问题/问题。回答
或讨论也会很好:)任何链接讨论主题
也会很好..

1.如何为测试目的生成下溢异常?
我不认为有某种方法,但你可以试试......

将一个小数字除以一个大数字,如

双a = 1e-200;
双b = 1e200;
双ab = a / b;
I have a few floating point questions/issues I wish to ask. An answer
or discussion would be nice too :) Any links discussion the subject
would be nice too..

1. How do I generate an underflow exception for testing purposes?
I don''t think there is a certain way, but you could try...

Divide a small number by a big number, like

double a = 1e-200;
double b = 1e200;
double ab = a / b;




我有点尝试过。没有帮助。



I kinda tried that. Didn''t help.

2.抑制浮点异常的最快值是什么?
2. What is the fastest wat to suppress floating point exceptions?


语言没有办法做到这一点。我相信它们是平台特定的。



The language doesn''t have any means to do that. They are platform-
specific, I believe.

以下列情况为例。
float x = 1.0e38,// max float value
y = 1.0e-45,// min float value
z = 0.0f; //无用的任务

z = x / y; //这应该会产生一个溢出异常
Take the following situation as example.
float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

z = x / y; // this should generate a overflow exception



希望。



Hopefully.

我可以像这样使用异常处理:
float x = 1.0 e38,// max float value
y = 1.0e-45,// min float value
z = 0.0f; //无用的任务

尝试{
z = x / y; //这应该会产生溢出
异常
}
catch(EOverflow& eo){
//做任何事情
z = 1.0e38;
}
catch(EZeroDivide& ez){
//做任何事情
z = 1.0e38;
}
catch(EUnderflow& eu){
//做任何事情
z = 0.0;
}
I could use exception handling like so:
float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

try {
z = x / y; // this should generate a overflow
exception
}
catch( EOverflow &eo ) {
//do anything
z = 1.0e38;
}
catch( EZeroDivide &ez ) {
//do anything
z = 1.0e38;
}
catch( EUnderflow &eu ) {
//do anything
z = 0.0;
}



我不这么认为。什么是EOverflow?什么是EZeroDivide?那些不是标准类型。



I don''t think so. What''s EOverflow? What''s EZeroDivide? Those are
not Standard types.




对不起。这是来自BCB6的测试。但是我假设在其他编译器上也可以捕获那些

异常,并且人们会得到这个想法:)但是假设它不是borland。必须有办法来接受这些接受吗?或者我错了吗?



Sorry. This was from testing in BCB6. But i assumed catching those
exceptions are possible on other compilers as well and people would get
the idea :) But assuming it wasn''t borland. There must be a way to catch
those acceptions? or am i wrong?

或者有更好的方法吗?



你必须在新闻组中询问你的编译器。



You''d have to ask in a newsgroup for your compiler.

3.这个很奇怪,可能是我的编译器本身。当我使用
下一个例子(与第一个相同)并编译它以进行调试或
发布时,我有时会得到一个例外,有时候我不会。如果它没有并且我处于调试模式,我注意到z的值是不变的。无论无用的任务的价值是多少。

浮动x = 1.0e38,//最大浮动值
y = 1.0e-45,//最小浮动值
z = 0.0f; //无用的任务

z = x / y; //这应该会产生溢出
3. This one is weird and may be my compiler itself. When i use the
next example (same as the first one) and compile it for debugging or
release i sometimes get an exception and sometimes i don''t. If it
didn''t and i was in debug mode, I notice the value of z was
unchanged. No matter what the "useless assignment"''s value was.

float x = 1.0e38, //max float value
y = 1.0e-45, //min float value
z = 0.0f; //useless assignment

z = x / y; // this should generate a overflow



同样,该语言实际上没有任何方法可以捕获像浮点错误这样的硬件异常。也许你的编译器的文档有一些信息...

V



Again, the language doesn''t really have any way of capturing hardware
exceptions like floating point errors. Perhaps the documentation for
your compiler has some information...

V




所以我想异常处理是特定于编译器的。我有点猜测你的写作是
。 *获得一些令人讨厌的装配经验的回忆

除以零错误*



So i guess the exception handling is compiler specific. I kinda guess
your write. *Getting flashbacks of some nasty assembly experiences with
division by zero errors*


>>
我不这么认为。什么是EOverflow?什么是EZeroDivide?那些不是标准类型。
I don''t think so. What''s EOverflow? What''s EZeroDivide? Those are
not Standard types.



抱歉。这是来自BCB6的测试。但是我假设在其他编译器上也可以捕获那些例外,人们会得到这个想法:)但是假设它不是borland。必须有办法捕捉那些接受吗?或者我错了吗?



Sorry. This was from testing in BCB6. But i assumed catching those
exceptions are possible on other compilers as well and people would get
the idea :) But assuming it wasn''t borland. There must be a way to catch
those acceptions? or am i wrong?




你错误的是假设这些例外存在于任何地方

而不是Borland。 C ++没有使用

浮点异常来定义任何标准的dealling方法。它甚至没有定义浮动

点异常存在。它是所有编译器特定的。也许你应该在Borland新闻组上询问




john



What you are wrong about is assuming that these exception exist anywhere
other than in Borland. C++ defines no standard means of dealling with
floating point exceptions at all. It does not even define that floating
point exceptions exist. It is all compiler specific. Perhaps you should ask
on a Borland newsgroup.

john


这篇关于一些浮点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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