Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

drf1229

macrumors regular
Original poster
Jun 22, 2009
237
0
I am making an app that finds the roots of a quadratic equation. It looks like this:

ViewController.m-

//
// ViewController.m
// PercentFinder
//
// Created by Danny Flax on 6/21/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"
#import <UIKit/UIKit.h>
#import <stdio.h>

@implementation ViewController

-(IBAction)Solve(UIButton*)sender{


NSString*as=[A text];
int a=[as intValue];
NSString*bs=[B text];
int b=[bs intValue];
NSString*cs=[C text];
int c=[cs intValue];
int d=((-1*b)+sqrt((b*b)-4*(a*c)))/(2*a);
int e=((-1*b)-sqrt((b*b)-4*(a*c)))/(2*a);



[Answer setText:[NSString stringWithFormat:mad:"%d and %d", d,e]];






}


@end

For some odd reason, it returns 0 for the values of d and e no matter what I type. I'm just a beginner, so there probably is some obvious problem that I missed, and I would greatly appreciate it if somebody could help me.
 
First, I think you need to involve some floats in there somewhere. Otherwise, things like sqrt()s on ints are just going to give you unexpected results.

Second, I suggest enclosing your snippet in code tags.
 
It worked! Thanks for your help. But I have one more problem. because the answer it a float, it shows up as -0.5000000000. How do I just make it -0.5?
 
and how do I enclose snippets in tags?
Put [ CODE ]...[ /CODE ] tags around your snippet, removing the spaces. These tags are easily accessible via the # icon in the toolbar.

But I have one more problem. because the answer it a float, it shows up as -0.5000000000. How do I just make it -0.5?
You want to look into formatting the output. Since stringWithFormat works very much like printf, you should be able to do "%.1f"
 
Thank you so much! Is there any way to tell when the answer is an integer and when its a long decimal?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.