I have a few weird, unexplained issues

Discussion in 'iOS Programming' started by adildacoolset, Dec 28, 2012.

  1. adildacoolset macrumors 65816

    Joined:
    Sep 5, 2011
    Location:
    Lusaka, Zambia(If you know where it is)
    #1
    Background: I'm making a simple program that solves quadratic equations and completes the squares. Now, I have a few issues:

    1. For some reason, when I do something like this (not actual code):
    Code:
    double a = 5/2;
    NSLog(@"%f", a);
    
    The output will be 2.000000, instead of 2.5. And this happens to any operation that has decimal points. It just rounds it down to an integer, depute being a type of
    Code:
    double
    . This gives invalid answers, and I need help. Please.

    2. Since it solves quadratic equations, let's say is the answer is a whole number(probably 5). Since the answer variable is of type
    Code:
    double
    , it will output 5.000000. How do I make it do just 5?

    3. I made it obtain the values from three separate UITextFields. As a result, the value was a string and so I had to set up a scanner to scan for the integers. Here is the code:
    Code:
    NSCharacterSet* charSet = [NSCharacterSet decimalDigitCharacterSet];
        
        
        NSScanner* aScanner = [NSScanner scannerWithString:self.aValue.text];
        NSScanner* bScanner = [NSScanner scannerWithString:self.bValue.text];
        NSScanner* cScanner = [NSScanner scannerWithString:self.cValue.text];
        
        [aScanner scanUpToCharactersFromSet:charSet intoString:nil];
        [bScanner scanUpToCharactersFromSet:charSet intoString:nil];
        [cScanner scanUpToCharactersFromSet:charSet intoString:nil];
        
        [aScanner scanInt:&aVal];
        [bScanner scanInt:&bVal];
        [cScanner scanInt:&cVal];
    
    This code is fair enough, and works fine for positive values. As you can imagine, if the user enters a negative value, it will ignore it. How can I adjust this to scan for the sign also, so it can also work for negative values?



    Thank you for your time. I have attached the entire project file of you want to take a look.
     

    Attached Files:

  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    1. Notice that you are dividing one whole number by another. The return value from that will be a whole number. You need to cast at least one of those values to a double.

    Code:
    double foo = (double) 5 / (double) 2;

    2. You could use the remainder() function to discover if the number is evenly divisible by 1. Like so, along with two ways of not printing the trailing zeros. Play with it.

    Code:
    if ( remainder(foo,1) == 0.0) {
      NSLog(@"number is: %d", (NSInteger)foo); // Notice the casting.
      NSLog(@"number is: %9.0f", foo); // // No casting, but a fixed length. 9 characters before the period, and zero following.
    }
    else
      NSLog(@"number is: %f", foo);

    3. First you read the documentation for NSString to see what it can do. In this case search the page for the word double and low & behold you'll find the doubleValue method. It will pass back positive and negative values.

    Code:
    double foo = [mytext.text doubleValue];
     
  3. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    1 - xStep's explanation is correct, but a simpler / shorter solution would be just appending a .0 to the end of each number, which will cause it to be a float or double instead of a int or long.

    2 - Rather than using remainder() like xStep said, you could also use modulus, which is the same thing... it looks like a%b... That is, the remainder when a is divided by b.

    3 - I don't know of anything simpler than xStep provided.
     
  4. adildacoolset thread starter macrumors 65816

    Joined:
    Sep 5, 2011
    Location:
    Lusaka, Zambia(If you know where it is)
    #4
    Thanks a lot man!
     
  5. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #5
    1. I assumed adildacoolset was using an int or NSInteger value instead of numbers as in the sample, so the cast would be necessary.

    2. I first tried the simple % mod method, but it doesn't work with float values, only integers. I realized I had to use a function and chose remainder over fmod.
     

Share This Page