-1

I am trying to get billion and million values from Abacus game , on iphone 5s and on iphone 6 its give me values correctly ,but on iphone 4s and on iphone 5 its give me values in minus format , i was try out with data type long long , long but still not give me correct value here is my code which works fine on iphone 5s and 6 but give warning and give number in minus format ,can anyone help me in this please here is image of my code

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;

see my code please https://i.stack.imgur.com/U24oA.png

Fabio Berger
  • 1,921
  • 2
  • 24
  • 29
Asmita
  • 477
  • 8
  • 20

2 Answers2

3

The size of NSInteger is platform dependent.

iPhone 5s and superior are 64 bit so the maximum value you can have is 2^63 - 1 (roughly 9,22 * 10^18).

iPhone 4s and iPhone 5 are 32 bit so the maximum value you can have is 2^31 - 1 = 2 147 483 647 which is always smaller than 10 000 000 000.

You can use int64_t instead of NSInteger to have 64 bit integer on all platform and solve your problem.

Raphaël
  • 3,646
  • 27
  • 28
  • @Asmita Did you try to change the type of `placeValue` to `int64_t` and recompile ? Do you still have the same warning ? – Raphaël Aug 16 '16 at 10:01
  • warning goes now thanks but giving number in minus like on 500000000 giving me -167890874 like this number – Asmita Aug 16 '16 at 10:05
  • @Asmita Then you should still have other portion of codes that uses `NSInteger` instead of ìnt64_t`. You have to replace them as well. – Raphaël Aug 16 '16 at 10:07
  • i replace it to int64_t other place also still not working @Raphael – Asmita Aug 16 '16 at 10:09
0

Let's consider for a moment how an abacus works, and how you can leverage that:

What is an abacus?

An abacus is a calculation tool that works as follows: It has a series of rods, each with a series of beads, each rod having a value equal to its number of beads multiplied by ten to the power of the position of the rod, or b * 10p, or bEp.

How can this be used to your advantage?

Instead of storing the huge numbers you currently are, consider storing the value of each rod of your abacus as something along the lines of typedef struct : uint8_t { uint8_t value : 4, exponent : 4; } rod_value_t;.

A rod_value_t can in this case store a number up to 1E16, which is on the order of the range of a 64 bit integer.

Now, each rod only stores a single digit, so you'll need one of these for each rod, but since you are using the exponents and digits directly, and (apprently) storing each rod in either case, this is pretty massive space saving all the same.

If you want to store larger numbers than 1E16, you could divide the struct up a little differently, such as for instance typedef struct : uint16_t { uint16_t value : 4, exponent : 12; } rod_value_t;, which will allow you to store, with the appropriate number of rods, basically any number you can conceive of (and quite a few you can't).

Williham Totland
  • 28,471
  • 6
  • 52
  • 68
  • yes you right , i used one third party using that done all beads, columns, beadbehaviour all works fine on iphone 6 and 5s but giving error on iphone 4s and on 5 as per Raphaël say used int64_t its also not work? can you help me in this , it will great for me @Williham Totland – Asmita Aug 18 '16 at 18:42
  • @Asmita Step away from large integer sizes alltogether and store the exponent and vauee for each column instead. – Williham Totland Aug 18 '16 at 18:45
  • sorry not getting you @Williham Totland – Asmita Aug 18 '16 at 18:46
  • @asmita Instead of storing, like you do, 10000000 for the number place and 5 for the column value, store 7 for the exponent and 5 for the value. – Williham Totland Aug 18 '16 at 18:47
  • sorry still not understood i am too irritated beacuse this game , if you have time can you take look on my demo game please its help me lot @Williham Totland – Asmita Aug 18 '16 at 18:53
  • @asmita I'm sorry, I just don't know how to make it clearer. – Williham Totland Aug 18 '16 at 18:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121288/discussion-between-asmita-and-williham-totland). – Asmita Aug 18 '16 at 18:55