I have a few weird, unexplained issues - MacRumors Forums
Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 28, 2012, 10:39 AM   #1
adildacoolset
macrumors 65816
 
Join Date: Sep 2011
Location: Lusaka, Zambia(If you know where it is)
I have a few weird, unexplained issues

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
File Type: zip iPhoneQuadEqSolver.zip (78.2 KB, 3 views)
__________________
Last edited by adildacoolset; Tomorrow at 09:42 AM. Reason: grammar error
adildacoolset is offline   0 Reply With Quote
Old Dec 28, 2012, 11:45 AM   #2
xStep
macrumors 68000
 
Join Date: Jan 2003
Location: Lost in Minneapolis
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];
__________________
My App: CameraTime - Time lapse photography for novice and advanced users.
xStep is offline   0 Reply With Quote
Old Dec 28, 2012, 11:58 AM   #3
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
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.
ArtOfWarfare is offline   0 Reply With Quote
Old Dec 28, 2012, 12:04 PM   #4
adildacoolset
Thread Starter
macrumors 65816
 
Join Date: Sep 2011
Location: Lusaka, Zambia(If you know where it is)
Quote:
Originally Posted by xStep View Post
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];
Thanks a lot man!
__________________
Last edited by adildacoolset; Tomorrow at 09:42 AM. Reason: grammar error
adildacoolset is offline   0 Reply With Quote
Old Dec 28, 2012, 03:00 PM   #5
xStep
macrumors 68000
 
Join Date: Jan 2003
Location: Lost in Minneapolis
Quote:
Originally Posted by ArtOfWarfare View Post
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.
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.
__________________
My App: CameraTime - Time lapse photography for novice and advanced users.
xStep is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Tags
double, float, quadratic

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Weird Mavericks Issues, with Dropbox, and other issues mashinhead OS X Mavericks (10.9) 4 Jan 12, 2014 05:23 PM
Unexplained network activity meegosh2013 OS X 10.8 Mountain Lion 1 May 20, 2013 10:12 AM
General: Who else is having unexplained battery issues? tymaster50 Jailbreaks and iOS Hacks 25 Apr 16, 2013 04:04 AM
Unexplained Data Consumption? americanclassic iPhone 8 Jan 6, 2013 04:55 PM
Macbook Pro Retina - Weird Weird issues MishalGunner MacBook Pro 14 Oct 18, 2012 12:02 PM

Forum Jump

All times are GMT -5. The time now is 09:42 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC