如何从所有双打中枚举? [英] How does one enumerate through all doubles?

查看:64
本文介绍了如何从所有双打中枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,我想验证它是否适用于所有双打.由于我不是信任的排序者,因此我希望它能在所有双打中运行并测试我的方法是否有效.我将double转换为另一种数据类型,然后将其转换回.

不幸的是,这不是像遍历Int32那样琐碎的任务.如果有人这样做或知道某个代码在哪里,请告诉我.


谢谢
Ken

I have some code and I want to verify that it works for all doubles. Since I am not the trusting sort I would like to have it run through all the doubles and test if my method works. I''m converting the double to another datatype and then converting it back.

Unfortunately this is not a trivial task like going through Int32. If anyone has done this or knows where some code is that does this please let me know.


Thanks
Ken

推荐答案

您要做的就是用三个左右的值进行测试.最好的情况,最坏的情况,以及几个随机选择的中间值.

此外,当您说出所有双打中的数字时,您是说所有可能的值吗?该测试将需要相当长的时间才能运行.
All you have to do is test with three or so values. Best case, worst case, and a couple of randomly chosen middle-of-the-road values.

Besides, when you say enumerate through all the doubles, do you mean every possible value? That test will take quite a while to run.


我建​​议使用以下代码增加一个long值,然后将其转换为double值:

I''d recommend incrementing a long and then converting it to a double, using the below code:

double dbl;
 
for (long lng = long.MinValue; lng < long.MaxValue; ++lng)
{
    dbl = BitConverter.Int64BitsToDouble(lng);
 
    /* Test the double here */
}
 
// Also test for long.MaxValue as well.
dbl = BitConverter.Int64BitsToDouble(long.MaxValue);


如果您要加倍并增加某个数字,它将避免潜在的下溢.

请注意,这样做会获得一些特殊的"case"值,例如infinity和NaN等.您可能希望对double使用静态方法来消除这些情况,或者只是将值限制在Min/Max之间.像这样...

尽情享受您的〜18,446,744,073,709,551,616测试用例. :D


It''ll keep you from potentially underflowing if you were to just take a double and increment it by some number.

Be aware, you''ll get some special ''case'' values from doing this, like infinity and NaN and such. You''ll probably want to use the static methods on double to remove those cases or just constrain the value between Min/Max. Something like that...

Have fun with your ~18,446,744,073,709,551,616 test cases. :D


这篇关于如何从所有双打中枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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