通常,该异常被除以零时抛出。唯一的问题是当我在分母中记录变量的值时,该值不为零。代码:
布鲁克
#import "breuk.h"
@implementation breuk
@synthesize tellerBreuk;
@synthesize noemerBreuk;
@synthesize ggd;
@synthesize quotientBreuk;
- (id)initWithTeller:(int)mijnTeller andNoemer:(int)mijnNoemer{
self = [super init];
if(self){
int u = mijnTeller;
int v = mijnNoemer;
int temp;
while (v != 0){
temp = u % v;
u = v;
v = temp;
}
ggd = u;
self.tellerBreuk = mijnTeller/ggd;
self.noemerBreuk = mijnNoemer/ggd;
NSLog(@"%i de ggd is %i", self.noemerBreuk, ggd);
//self.quotientBreuk = (float)mijnTeller/mijnNoemer;
}
return self;
}
@end
最佳答案
查看评论:
if(self){
int u = mijnTeller; // For example u == 0
int v = mijnNoemer; // and v == 0
int temp;
while (v != 0){ // you don't come in while loop
temp = u % v;
u = v;
v = temp;
}
ggd = u; // ggd is then == 0
self.tellerBreuk = mijnTeller/ggd; // Error: division by zero
self.noemerBreuk = mijnNoemer/ggd;
NSLog(@"%i de ggd is %i", self.noemerBreuk, ggd);
}
关于ios - 引发算术异常,但是为什么呢?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19620034/