summaryrefslogtreecommitdiff
path: root/MLKInteger.m
diff options
context:
space:
mode:
authorMatthias Andreas Benkard <matthias@benkard.de>2008-08-04 18:18:41 +0200
committerMatthias Andreas Benkard <matthias@benkard.de>2008-08-04 18:18:41 +0200
commit90e6023292e2bfe927bd633bac42fc355bb9f4d1 (patch)
tree14664b9e9d98f43c83b78547ea7c554fef8de9d1 /MLKInteger.m
parent383e3e833a7fbb8b1560ba861b76e8be96542c6f (diff)
Add support for fixnums.
Diffstat (limited to 'MLKInteger.m')
-rw-r--r--MLKInteger.m39
1 files changed, 36 insertions, 3 deletions
diff --git a/MLKInteger.m b/MLKInteger.m
index 2837b5b..43af24d 100644
--- a/MLKInteger.m
+++ b/MLKInteger.m
@@ -39,6 +39,19 @@
return self;
}
+-(MLKInteger *) initWithIntptr_t:(intptr_t)intptr_t_value
+{
+ self = [super init];
+ mpz_init_set_si (value, intptr_t_value);
+ return self;
+}
+
+-(MLKInteger *) initWithFixnum:(id)fixnum
+{
+ self = [self initWithIntptr_t:MLKIntWithFixnum(fixnum)];
+ return self;
+}
+
-(MLKInteger *) initWithString:(NSString *)string
negative:(BOOL)negative
base:(unsigned int)base
@@ -51,21 +64,31 @@
+(MLKInteger *) integerWithMPZ:(mpz_t)mpz
{
- return LAUTORELEASE ([[MLKInteger alloc] initWithMPZ:mpz]);
+ return AUTORELEASE ([[MLKInteger alloc] initWithMPZ:mpz]);
}
+(MLKInteger *) integerWithString:(NSString *)string
negative:(BOOL)negative
base:(unsigned int)base
{
- return LAUTORELEASE ([[MLKInteger alloc] initWithString:string
+ return AUTORELEASE ([[MLKInteger alloc] initWithString:string
negative:negative
base:base]);
}
+(MLKInteger *) integerWithInt:(int)intValue
{
- return LAUTORELEASE ([[MLKInteger alloc] initWithInt:intValue]);
+ return AUTORELEASE ([[MLKInteger alloc] initWithInt:intValue]);
+}
+
++(MLKInteger *) integerWithIntptr_t:(intptr_t)intptr_t_value
+{
+ return AUTORELEASE ([[MLKInteger alloc] initWithIntptr_t:intptr_t_value]);
+}
+
++(MLKInteger *) integerWithFixnum:(id)fixnum
+{
+ return AUTORELEASE ([[MLKInteger alloc] initWithFixnum:fixnum]);
}
@@ -140,6 +163,16 @@ DEFINE_MPZ_TWOARG_INTONLY_OPERATION (lcm:, mpz_lcm)
return obj;
}
+-(BOOL) fitsIntoFixnum
+{
+ return (mpz_sizeinbase (self->value, 2) <= (sizeof (id)) * 8 - 2);
+}
+
+-(id) fixnumValue
+{
+ return MLKFixnumWithInt ([self intValue]);
+}
+
-(int) intValue
{
return mpz_get_si (value);