【问题标题】:Comparison of constant 10000000000 with expression of type 'NSInteger' (aka 'int') is always false常量 10000000000 与“NSInteger”(又名“int”)类型的表达式的比较总是错误的
【发布时间】:2016-08-16 09:29:55
【问题描述】:

我试图从算盘游戏中获得十亿和百万值,在 iphone 5s 和 iphone 6 上它给我正确的值,但在 iphone 4s 和 iphone 5 上它给我负格式的值,我尝试了数据类型 long long , long 但仍然没有给我正确的值 这是我的代码,它在 iphone 5s 和 6 上运行良好,但给出警告并给出减号格式的数字,任何人都可以帮助我吗 这是我的代码图片

NSString *digitLabelText;
if([self digit] == 0)
{
    digitLabelText= [NSString stringWithFormat:@"%ld", (long)[self digit]];
}
else
{
    if([self placeValue] == ONE || [self placeValue] == TEN ||
       [self placeValue] == ONE_HUNDRED || [self placeValue] == ONE_THOUSAND)
    {
        digitLabelText= [NSString stringWithFormat:@"%ld", (long)[self digit]];
    }
    else if([self placeValue] == TEN_THOUSAND)
    {
        digitLabelText= [NSString stringWithFormat:@"%ld,000",(long)[self digit]/ONE_THOUSAND];
    }
    else if([self placeValue] == HUNDRED_THOUSAND)
    {
        digitLabelText= [NSString stringWithFormat:@"%ld,000", (long)[self digit]/ONE_THOUSAND];
    }
    else if([self placeValue] == ONE_MILLION)
    {
        digitLabelText= [NSString stringWithFormat:@"%ld mil", (long)[self digit]/ONE_MILLION];
    }
    else if([self placeValue] == TEN_MILLION)
    {
        digitLabelText= [NSString stringWithFormat:@"%ld mil", (long)[self digit]/ONE_MILLION];
    }
    else if([self placeValue] == HUNDRED_MILLION)
    {
        digitLabelText= [NSString stringWithFormat:@"%ld mil", (long)[self digit]/ONE_MILLION];
    }
    else if([self placeValue] == ONE_BILLION)
    {
        //issue come from this in iphone 4s ,5 digitLabelText= [NSString  stringWithFormat:@"%ld bil", (long)[self digit]/ONE_BILLION];
    }
    else if([self placeValue] == TEN_BILLION)
    {
        digitLabelText= [NSString stringWithFormat:@"%ld bil", (long)[self digit]/ONE_BILLION];///ONE_BILLION];
    }
    else if([self placeValue] == HUNDRED_BILLION)
    {
        digitLabelText= [NSString stringWithFormat:@"%ld bil", (long)[self digit]/ONE_BILLION];///ONE_BILLION];
    }
    else if([self placeValue] == ONE_TRILLION)
    {
        digitLabelText= [NSString stringWithFormat:@"%lld tril", (long)[self digit]/ONE_TRILLION];///ONE_TRILLION];
    }
}
digitLabel.text = digitLabelText;

请看我的代码 http://i.stack.imgur.com/U24oA.png

【问题讨论】:

  • 请在 cmets 中包含您的代码。外部链接可能会中断。
  • 查找“NSInteger 最大值”以了解原因。
  • 另外,这似乎是一个 XY 问题。您问题的实际解决方案似乎是对数。

标签: ios objective-c


【解决方案1】:

NSInteger 的大小取决于平台。

iPhone 5s 和更高版本是 64 位,因此您可以拥有的最大值是 2^63 - 1(大约 9,22 * 10^18)。

iPhone 4s 和 iPhone 5 是 32 位的,因此您可以拥有的最大值是 2^31 - 1 = 2 147 483 647,它始终小于 10 000 000 000

您可以使用int64_t 代替 NSInteger 在所有平台上拥有 64 位整数并解决您的问题。

【讨论】:

  • @Asmita 您是否尝试将placeValue 的类型更改为int64_t 并重新编译?你还有同样的警告吗?
  • 警告现在谢谢,但给数字减号就像 500000000 给我 -167890874 这样的数字
  • @Asmita 那么你应该还有其他部分代码使用NSInteger 而不是ìnt64_t`。你也必须更换它们。
  • 我将其替换为 int64_t 其他地方也仍然无法正常工作@Raphael
【解决方案2】:

让我们考虑一下算盘的工作原理,以及如何利用它:

什么是算盘?

算盘是一种计算工具,其工作原理如下:它有一系列杆,每个杆都有一系列珠子,每个杆的值等于珠子的数量乘以十的幂次方杆的位置,或b * 10p,或bEp

如何利用它来发挥你的优势?

与其存储您当前的大量数字,不如考虑将您的算盘每根杆的值存储为类似于typedef struct : uint8_t { uint8_t value : 4, exponent : 4; } rod_value_t; 的内容。

在这种情况下,rod_value_t 可以存储最多为1E16 的数字,该数字在 64 位整数的范围内。

现在,每个杆只存储一个数字,因此每个杆都需要一个数字,但是由于您直接使用指数和数字,并且(显然)在任何一种情况下都存储每个杆,这很漂亮节省大量空间。

如果您想存储比1E16 更大的数字,您可以稍微不同地划分结构,例如typedef struct : uint16_t { uint16_t value : 4, exponent : 12; } rod_value_t;,这将允许您存储具有适当数量的棒,基本上是任何数字你可以想象(还有很多你不能)。

【讨论】:

  • 是的,你说得对,我使用了一个第三方,使用了所有的珠子、柱子、beadbehaviour 在 iphone 6 和 5s 上都可以正常工作,但在 iphone 4s 和 5 上出现错误,根据 Raphaël 所说,使用了 int64_t也不行?你能帮我吗,这对我很有好处@Williham Totland
  • @Asmita 远离大整数大小,而是存储每列的指数和值。
  • 抱歉没有找到你@Williham Totland
  • @asmita 不像您那样存储 10000000 作为数字位置和 5 作为列值,而是存储 7 作为指数和 5 作为值。
  • 抱歉还是没看懂,因为这个游戏我太烦了,如果你有时间可以看看我的演示游戏,请帮帮我@Williham Totland
猜你喜欢
  • 1970-01-01
  • 2017-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-28
  • 1970-01-01
  • 2019-10-04
  • 2020-11-26
相关资源
最近更新 更多