Skip to content

Commit

Permalink
Big integer division added.
Browse files Browse the repository at this point in the history
  • Loading branch information
joubertnel committed Jan 5, 2011
1 parent d6f24c1 commit a03f7fb
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 38 deletions.
2 changes: 1 addition & 1 deletion BigInteger.h
Expand Up @@ -63,7 +63,7 @@ typedef enum {
- (BigInteger *)add:(BigInteger *)anotherNumber;
- (BigInteger *)subtract:(BigInteger *)anotherNumber;
- (BigInteger *)multiply:(BigInteger *)anotherNumber;

- (BigInteger *)divide:(BigInteger *)divisor;


@end
14 changes: 14 additions & 0 deletions BigInteger.m
Expand Up @@ -187,6 +187,20 @@ - (BigInteger *)multiply:(BigInteger *)anotherNumber
return [BigInteger bigIntegerWithMPZ:result_mpz];
}

- (BigInteger *)divide:(BigInteger *)divisor
{
mpz_t result_mpz;
mpz_t this_mpz;
mpz_t other_mpz;

mpz_init(result_mpz);
[self.mpzVal getValue:&this_mpz];
[divisor.mpzVal getValue:&other_mpz];

mpz_div(result_mpz, this_mpz, other_mpz);
return [BigInteger bigIntegerWithMPZ:result_mpz];
}




Expand Down
36 changes: 18 additions & 18 deletions Cacao.xcodeproj/joubert.perspectivev3
Expand Up @@ -216,7 +216,7 @@
<key>_historyCapacity</key>
<integer>0</integer>
<key>bookmark</key>
<string>BEF450F912D41E52007E42FB</string>
<string>BEF4521D12D4289B007E42FB</string>
<key>history</key>
<array>
<string>BEF4506E12D419E3007E42FB</string>
Expand Down Expand Up @@ -316,6 +316,7 @@
<string>BE3BC17A1290972900ED6ADB</string>
<string>BE93F79012BBE395007A4826</string>
<string>BE021FE7128FAECE00636124</string>
<string>BE3BC17B1290973500ED6ADB</string>
<string>32C88DFF0371C24200C91783</string>
<string>0867D69AFE84028FC02AAC07</string>
<string>1C37FBAC04509CD000000102</string>
Expand All @@ -324,9 +325,8 @@
<key>PBXSmartGroupTreeModuleOutlineStateSelectionKey</key>
<array>
<array>
<integer>49</integer>
<integer>41</integer>
<integer>11</integer>
<integer>3</integer>
<integer>1</integer>
<integer>0</integer>
</array>
</array>
Expand Down Expand Up @@ -366,19 +366,19 @@
<key>PBXProjectModuleGUID</key>
<string>BE2652C3128DBF470088DC0A</string>
<key>PBXProjectModuleLabel</key>
<string>CacaoBigInteger.m</string>
<string>TestMath.m</string>
<key>PBXSplitModuleInNavigatorKey</key>
<dict>
<key>Split0</key>
<dict>
<key>PBXProjectModuleGUID</key>
<string>BE2652C4128DBF470088DC0A</string>
<key>PBXProjectModuleLabel</key>
<string>CacaoBigInteger.m</string>
<string>TestMath.m</string>
<key>_historyCapacity</key>
<integer>0</integer>
<key>bookmark</key>
<string>BEF450F812D41E52007E42FB</string>
<string>BEF4521C12D4289B007E42FB</string>
<key>history</key>
<array>
<string>BE2E8750129259F20048AE63</string>
Expand All @@ -405,15 +405,13 @@
<string>BEA9FD4312C7D9BE001875C1</string>
<string>BEA9018012C83567001875C1</string>
<string>BE47178E12CB7FBE006F6BB3</string>
<string>BE47194F12CBFAA5006F6BB3</string>
<string>BE471D9212CD95A1006F6BB3</string>
<string>BE471D9312CD95A1006F6BB3</string>
<string>BE471DA412CD9861006F6BB3</string>
<string>BE471DEE12CD9D1C006F6BB3</string>
<string>BE471DF012CD9D1C006F6BB3</string>
<string>BE471DF312CD9D1C006F6BB3</string>
<string>BE471DF412CD9D1C006F6BB3</string>
<string>BE471DF512CD9D1C006F6BB3</string>
<string>BE471DF612CD9D1C006F6BB3</string>
<string>BE471DF712CD9D1C006F6BB3</string>
<string>BE471DF812CD9D1C006F6BB3</string>
Expand All @@ -437,7 +435,6 @@
<string>BEF44DC512D3E432007E42FB</string>
<string>BEF44DDC12D3E535007E42FB</string>
<string>BEF44E2B12D3EAA7007E42FB</string>
<string>BEF44E2C12D3EAA7007E42FB</string>
<string>BEF44E4312D3EE56007E42FB</string>
<string>BEF44E5E12D3F229007E42FB</string>
<string>BEF44E5F12D3F229007E42FB</string>
Expand All @@ -447,18 +444,21 @@
<string>BEF4507E12D41B0D007E42FB</string>
<string>BEF4507F12D41B0D007E42FB</string>
<string>BEF4508012D41B0D007E42FB</string>
<string>BEF4508112D41B0D007E42FB</string>
<string>BEF4508212D41B0D007E42FB</string>
<string>BEF450AE12D41C22007E42FB</string>
<string>BEF450C612D41C7F007E42FB</string>
<string>BEF450EB12D41E00007E42FB</string>
<string>BEF450EC12D41E00007E42FB</string>
<string>BEF450F212D41E52007E42FB</string>
<string>BEF450F312D41E52007E42FB</string>
<string>BEF450F412D41E52007E42FB</string>
<string>BEF450F512D41E52007E42FB</string>
<string>BEF450F612D41E52007E42FB</string>
<string>BEF450F712D41E52007E42FB</string>
<string>BEF450FE12D41FAD007E42FB</string>
<string>BEF450FF12D41FAD007E42FB</string>
<string>BEF4510012D41FAD007E42FB</string>
<string>BEF4510112D41FAD007E42FB</string>
<string>BEF4510A12D42017007E42FB</string>
<string>BEF4510B12D42017007E42FB</string>
<string>BEF4511C12D4206F007E42FB</string>
<string>BEF4520212D4274F007E42FB</string>
<string>BEF4521912D4289B007E42FB</string>
<string>BEF4521A12D4289B007E42FB</string>
<string>BEF4521B12D4289B007E42FB</string>
</array>
</dict>
<key>SplitCount</key>
Expand Down
29 changes: 17 additions & 12 deletions CacaoCore.m
Expand Up @@ -11,6 +11,7 @@
#import "CacaoSymbol.h"
#import "CacaoFn.h"
#import "BigInteger.h"
#import "NSArray+Functional.h"

static NSString * GLOBAL_NAMESPACE = @"cacao";
static NSString * SYMBOL_NAME_YES = @"YES";
Expand Down Expand Up @@ -59,6 +60,19 @@ + (NSDictionary *)functions
return answer;
} params:nil restArg:multiplyArgSym];

CacaoSymbol * divideOpSym = [CacaoSymbol symbolWithName:@"/" inNamespace:GLOBAL_NAMESPACE];
NSString * divideArgName = @"numbers";
CacaoSymbol * divideArgSym = [CacaoSymbol symbolWithName:divideArgName inNamespace:nil];
CacaoFn * divideFn = [CacaoFn fnWithDispatchFunction:^(NSDictionary * argsAndVals) {
BigInteger * firstNumber;
CacaoVector * numbers = [argsAndVals objectForKey:divideArgSym];
NSArray * remainingNumbers = [[numbers elements] popFirstInto:&firstNumber];
BigInteger * answer = firstNumber;
for (BigInteger * number in remainingNumbers)
answer = [answer divide:number];
return answer;
} params:nil restArg:divideArgSym];


CacaoSymbol * lessThanSymbol = [CacaoSymbol symbolWithName:@"<" inNamespace:GLOBAL_NAMESPACE];
NSString * lessThanArgName = @"numbers";
Expand Down Expand Up @@ -110,16 +124,8 @@ + (NSDictionary *)functions
} params:mapArgs restArg:nil];


//
// CacaoSymbol * divideOpSym = [CacaoSymbol symbolWithName:@"/" inNamespace:GLOBAL_NAMESPACE];
// CacaoFn * divideFn = [CacaoFn fnWithDispatchFunction:^(NSDictionary * argsAndVals) {
// NSNumber * firstNumber;
// NSArray * remainingNumbers = [params popFirstInto:&firstNumber];
// int answer = [firstNumber intValue];
// for (NSNumber * number in remainingNumbers)
// answer /= [number intValue];
// return [NSNumber numberWithInt:answer];
// }];



NSDictionary * globalMappings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], yesSymbol,
Expand All @@ -130,8 +136,7 @@ + (NSDictionary *)functions
lessThanFn, lessThanSymbol,
rangeFn, rangeSymbol,
pmapFn, mapSymbol,

// divideFn, divideOpSym,
divideFn, divideOpSym,
nil];
return globalMappings;
return nil;
Expand Down
3 changes: 2 additions & 1 deletion CacaoLispReader.h
Expand Up @@ -31,7 +31,8 @@
#import <Cocoa/Cocoa.h>
#import "PushbackReader.h"

#define symbolPat @"[:]?([\\D&&[^/]].*/)?([\\D&&[^/]][^/]*)"
//#define symbolPat @"[:]?([\\D&&[^/]].*/)?([\\D&&[^/]][^/]*)"
#define symbolPat @"[:]*?([\\D]*/)?([\\D][^/]*)"
#define intPat @"([-+]?)(?:(0)|([1-9][0-9]*)|0[xX]([0-9A-Fa-f]+)|0([0-7]+)|([1-9][0-9]?)[rR]([0-9A-Za-z]+)|0[0-9]+)\\.?"
#define ratioPat @"([-+]?[0-9]+)/([0-9]+)"
#define floatPat @"[-+]?[0-9]+(\\.[0-9]+)?([eE][-+]?[0-9]+)?[M]?"
Expand Down
14 changes: 8 additions & 6 deletions TestMath.m
Expand Up @@ -97,19 +97,21 @@ - (void)testIntegerMultiplication
STAssertTrue([result boolValue], nil);
}

- (void)testIntegerDivision
{
NSString * test = @"(= 19 (/ 5681 299|";
NSNumber * result = (NSNumber *)[CacaoEnvironment evalText:test inEnvironment:env];
STAssertTrue([result boolValue], nil);
}


- (void)testNegativeIntegers
{
NSString * test = @"(= -45 (- 50 95|";
NSNumber * result = (NSNumber *)[CacaoEnvironment evalText:test inEnvironment:env];
STAssertTrue([result boolValue], nil);
}

//- (void)testIntegerDivision
//{
// CacaoAST * ast = [CacaoAST astWithText:@"<= 10 </ 20 2]"];
// NSNumber * result = (NSNumber *)[CacaoEnvironment eval:ast.tree inEnvironment:env];
// STAssertTrue([result boolValue], nil);
//}


@end

0 comments on commit a03f7fb

Please sign in to comment.