确定小数是否可以存储为 int32

发布于 2024-08-30 23:09:34 字数 277 浏览 15 评论 0原文

我正在做一些自定义序列化,为了节省一些空间,我想将小数序列化为 int(如果可能的话)。性能是一个问题,因为我正在处理大量数据。我目前使用的方法是:

if ((value > Int32.MinValue) && (value < Int32.MaxValue) && ((valueAsInt = Decimal.ToInt32(value)) == value))
{
    return true;
}

这个可以改进吗?

I am doing some custom serializing, and in order to save some space, i want to serialize the decimals as int, if possible value wise. Performance is a concern, since i am dealing with a high volume of data. The current method i use is:

if ((value > Int32.MinValue) && (value < Int32.MaxValue) && ((valueAsInt = Decimal.ToInt32(value)) == value))
{
    return true;
}

Can this be improved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

别想她 2024-09-06 23:09:34

你有负值吗?我猜是的,因为您有 MinValue 检查,否则您可以跳过它。您甚至可以使用 unsigned int ,它允许您将更多的 double 值转换为 int 。

编辑:此外,如果您有更多正数,您可以交换前两个条件。这样,第一个最有可能失败,从而减少比较的总数。

Do you have any negative values? I'm guessing yes since you have the MinValue check, otherwise you can skip it. You could even use unsigned int which will allow you to convert more of your double values into ints.

Edit: Also, if you have more positive numbers, you can swap the first two conditions. That way the first one is the most likely to fail, decreasing the total number of comparisons.

孤檠 2024-09-06 23:09:34

您的无效标准是:

1) 是否大于 MaxValue?

2)它是否小于MinValue?

3)它是否包含分数成分?

听起来你已经涵盖了它们。我的实现是:

public bool IsConvertibleToInt(decimal value)
{
    if(value > int.MaxValue)
       return false;

    if(value < int.MinValue)
       return false;

    if(Math.Floor(value) < value && Math.Ceiling(value) > value)
       return false;

    return true;
}

Your invalidation criteria are:

1) Is it greater than MaxValue?

2) Is it smaller than MinValue?

3) Does it contain a fractional component?

It sounds like you have them covered. My implementation would be:

public bool IsConvertibleToInt(decimal value)
{
    if(value > int.MaxValue)
       return false;

    if(value < int.MinValue)
       return false;

    if(Math.Floor(value) < value && Math.Ceiling(value) > value)
       return false;

    return true;
}
揽清风入怀 2024-09-06 23:09:34

这个怎么样。我认为应该减少操作(至少减少比较次数):

    return (value == (Int32)value);

还要记住,如果 if 语句仅返回布尔值,则可以只返回比较。仅此一点就可能使其速度更快(除非编译器已经对此进行了优化)。如果您必须使用 if 语句,您可以类似地执行此操作:

    if (value == (Int32)value)
    {
        //Do stuff...
    return true;
    }
    else
    {
        //Do stuff...
        return false;
    }

编辑: 我意识到这实际上不起作用。我本以为 Int32 转换只会复制小数点的前 32 位,留下任何剩余位(并且不会引发异常),但是可惜,它并没有那样工作(更不用说这对于所有负值)。

How about this. I think it should take fewer operations (at least a fewer number of comparisons):

    return (value == (Int32)value);

Also remember, if an if statement simply returns a boolean, you can just return the comparison. That alone might make it faster (unless the compiler already optimizes for this). If you have to use the if statement, you can similarly do this:

    if (value == (Int32)value)
    {
        //Do stuff...
    return true;
    }
    else
    {
        //Do stuff...
        return false;
    }

EDIT: I realize this doesn't actually work. I was thinking the Int32 cast would just copy in the first 32 bits from the decimal, leaving behind any remaining bits (and not throw an exception), but alas, it didn't work that way (not to mention it would be wrong for all negative values).

§对你不离不弃 2024-09-06 23:09:34

这取决于您有多少位小数或真正关心的小数位。如果您可以说我只关心最多 3 位小数,那么 int32 中可以存储的最大数字是 int.MaxValue / 1000。如果您只处理正数,那么您可以通过使用 uint 获得更高的数字。在任何情况下,做到这一点的方法是始终为小数保留空间,并使用 * 1000 对它们进行编码,使用 / 1000 将它们解码为十进制。

It depends on how many decimal places you have or really care about. If you could say that I only care about up to 3 decimal places then the largest number you can store in int32 is int.MaxValue / 1000. If you are only working with positive numbers then you can get a higher number by using uint. In any case the way to do it is to consistently reserve space for the decimal and use * 1000 to encode them and / 1000 to decode them to / from decimal.

山川志 2024-09-06 23:09:34

不需要“valueAsInt =”。我相信 (Decimal.ToInt32(value) == value)) 可以通过少一项赋值获得相同的结果。您是否使用 valueAsInt 作为某种输出参数?

No need for "valueAsInt =". I believe (Decimal.ToInt32(value) == value)) gets you the same result with one less assignment. Are you using valueAsInt as some sort of output parameter?

冬天旳寂寞 2024-09-06 23:09:34

难道你不能做这样的事情:

if(Decimal.ToInt32(value) == value)
{
     return true;
}

不是.net专家,但我认为这应该是它所需要的。此外,您的两个比较运算符应该是“或等于”,因为最小/最大值也是有效的。

编辑:正如评论中指出的,这会引发异常。您可以尝试捕获异常并返回 false,但此时自己进行最小/最大测试可能会快得多。

Wouldn't you be able to just do something like:

if(Decimal.ToInt32(value) == value)
{
     return true;
}

Not an expert on .net, but I think that should be all it'd require. Also, your two comparisons operators should be 'or equal' since the min/max values are also valid.

Edit: As pointed out in the comment, this would throw an exception. You could try catching the exception and returning false, but at that point it likely would be much faster to do the min/max testing yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文