Funky Math

Discussion in 'iOS Programming' started by larswik, Nov 23, 2011.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    I seem to have all these odd problems that I am struggling to find the answer for, this one seems so basic. I have a Table view that loads when the user clicks a button. I then iterate through the dollar amounts stoered in the mutableArray and add up the amounts for a total, but the math is off?

    I think it has something to do with the dequeueReusableCellWithIdentifier since the final 2 that are supposed to be added to the total are skipped. I had a problem many months ago when values when off the screen they returned as 0, but I solved that. But in this instance I an just dealing with the array and the values in it's indexes?

    Code:
    -(void)addUpTheValues{ // calculates the values in the text fields
        runningGrandTotal = 0.0;
        
        for (int i = 0; i < totalPayments.count; i++) {
            NSString *TempTwoString = [totalPayments objectAtIndex:i];
            runningGrandTotal +=   [TempTwoString floatValue];
            
            NSLog(@"Temp %d : %@", i, TempTwoString);
            NSLog(@"The Running Grand Total is %.2f", runningGrandTotal);
        }
        grandTotal.text = [NSString stringWithFormat:@"%.2f",runningGrandTotal];
       
    }
    
    Here is the print out from the NSLog. It adds everything up and then stops at 2300.00 at "Temp 8" and on? Thje values are there but it is not adding them in?
     
  2. szymczyk macrumors regular

    Joined:
    Mar 5, 2006
    #2
    You put a $ character before the last two entries: Temp 8 and Temp 9. The float value in those cases is 0, which is why the total isn't increasing. Remove the $ character from the last two entries and things should work.
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    Using a string to represent a number used in a calculation is a bad idea. Keep the number. Calculate with the number. To display a number, convert it to a string. The primary representation is always the number.
     
  4. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #4
    OMG! I was looking at that for about 3 hours last night and the answer was right in front of me. Now I feel dumb and thanks for the second set of eyes that caught that. I guess that was a good thing while I am flushing out my code. I should build in make some method that will check to see that only valid characters are in a textField to avoid that.

    Chown33- my numbers are stored in an NSMutableArray and from what I understand I can not store primitive data types in that type of array. These arrays are also stored in a Property List, which I also think need to be objects and not primitive data types. I must first wrap them in an object before I can store them. I have gotten use to using NSString as my go to object for this. I don't see a big difference between something like NSString and NSNumber to wrap my int's or floats in?

    I understand what you are saying about "Calculate with numbers". so instead of this line of code

    Code:
    runningGrandTotal +=   [TempTwoString floatValue];
    It should be something like

    Code:
    float someNumber =   [TempTwoString floatValue];
    runningGrandTotal += someNumber;
    Thanks again for the assist!
     
  5. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #5
    The conversion of an int or float to and from an NSString is very expensive. The conversion of an int or double to and from an NSNumber is almost trivial.
     
  6. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #6
    Hummm.... I will adopt that and read up on the NSNumber class. I am curious why it is more expensive in terms of compute time with NSString then NSNumber? I will read up on it tonight and thanks for pointing that out Jim
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    An NSNumber is also certain not to contain '$'.
     
  8. jiminaus, Nov 23, 2011
    Last edited: Nov 23, 2011

    jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #8
    I did a quick'n'dirty benchmark.


    With ints:

    Conversion of 10,000,000 ints to and from NSNumbers took 0 seconds
    Conversion of 10,000,000 ints to and from NSStrings took 11 seconds

    Conversion of 100,000,000 ints to and from NSNumbers took 4 seconds
    Conversion of 100,000,000 ints to and from NSStrings took 113 seconds


    And with doubles:

    Conversion of 10,000,000 doubles to and from NSNumbers took 2 seconds
    Conversion of 10,000,000 doubles to and from NSStrings took 14 seconds

    Conversion of 100,000,000 doubles to and from NSNumbers took 17 seconds
    Conversion of 100,000,000 doubles to and from NSStrings took 146 seconds
     
  9. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #9
    Interesting finds... I wasn't aware converting to one type of NSObject would be any better than converting to any other, for performance reasons.
     
  10. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #10
    Wow, some benchmarks. I guess when you are designing complex things and games it is good to use the correct object for the task at hand.

    Chown33 - I looked at the doc's and thought that you could only have integers in an NSNumbers but it said this
    Char is a character and not an integer value? So I guess NSNumber can hold more the numbers.
     
  11. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #11
    Char is a number between 0 and 127 I believe.
     
  12. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #12
    It's always goot to use the correct object for the task at hand. Granted though that our CPUs and RAM are big enough to cope with sloppy programming.


    -128 to 127 or 0 to 255 depending on if it's signed or not.
     

Share This Page