-
Notifications
You must be signed in to change notification settings - Fork 0
/
BigFloat.h
executable file
·154 lines (137 loc) · 5.8 KB
/
BigFloat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// ##############################################################
// BigFloat.h
// BigFloat Implementation
//
// Created by Matt Gallagher on Sun Jan 06 2002.
// Copyright © 2002-2003 Matt Gallagher. All rights reserved.
// ##############################################################
#import <Foundation/Foundation.h>
//
// About BigFloat
//
// BigFloat is an arbitrary precision (fixed at compile-time) arbitrary radix floating
// point number format. The entire functionality of the class is implemented in a
// single file for simple inclusion in other projects.
//
// Precision is defined by BF_num_values. It defines how many unsigned longs are
// used to hold the number. Though in reality, only half of each long is used. This
// is so that when you multiply then together, there is room for the result (16 bits
// multiplied by 16 bits requires all 32 bits). If you really wanted to, you could
// change this class so that BF_num_values was chosen at class initialisation time
// (I didn't want to).
//
// Bad design choice: when I created this class, I created it "mutable". What I mean
// is that [object1 add:object:2] changes the value of object1. I thought it was a
// good idea at the time. Having used it, I now realise I was wrong. Sorry. It is really
// annoying when you return a BigFloat and the calling function mucks it up on you.
// Maybe you can learn from my mistake.
//
// Naturally, functionality has been catered to the needs of Magic Number Machine
// a little (especially the limitedString function).
//
// Basic constants defining the precision used by the class
#define BF_num_values 16 // was 8
#define BF_max_mantissa_length (BF_num_values * 16 + 3)
#define BF_max_exponent_length 32
#if (BF_num_values < 2)
#error BF_num_values must be at least 2
#endif
// Mode for trigonometric operations
typedef NS_ENUM(unsigned int, BFTrigMode)
{
BF_degrees,
BF_radians,
BF_gradians
};
@interface BigFloat : NSObject <NSCopying, NSCoding>
{
@protected
unsigned long bf_array[BF_num_values];
signed int bf_exponent;
unsigned short bf_user_point;
BOOL bf_is_negative;
unsigned short bf_radix;
unsigned short bf_value_precision;
unsigned int bf_value_limit;
unsigned int bf_exponent_precision;
BOOL bf_is_valid;
}
// Constructors
- (instancetype)init;
- (instancetype)initWithMantissa: (unsigned long long)mantissa
exponent: (short)exp
isNegative: (BOOL)flag
radix: (unsigned short)newRadix
userPointAt: (unsigned short)pointLocation;
- (instancetype)initWithString:(NSString *)newValue radix:(unsigned short)newRadix;
- (instancetype)initWithInt:(signed int)newValue radix:(unsigned short)newRadix;
- (instancetype)initWithDouble:(double)newValue radix:(unsigned short)newRadix;
- (instancetype)initPiWithRadix:(unsigned short)newRadix;
- (instancetype)initWithCoder:(NSCoder *)coder;
- (void)encodeWithCoder:(NSCoder *)coder;
- (id)copyWithZone:(NSZone*)zone;
+ (BigFloat*)bigFloatWithString:(NSString *)newValue radix:(unsigned short)newRadix;
+ (BigFloat*)bigFloatWithInt:(signed int)newValue radix:(unsigned short)newRadix;
+ (BigFloat*)bigFloatWithDouble:(double)newValue radix:(unsigned short)newRadix;
+ (BigFloat*)piWithRadix:(unsigned short)newRadix;
// Public Utility Functions and properties
@property (nonatomic, getter=getUserPoint) int userPoint;
@property (nonatomic, readonly) int mantissaLength;
@property (nonatomic, readonly) unsigned short radix;
@property (nonatomic, getter=isValid, readonly) BOOL valid;
@property (nonatomic, getter=isNegative, readonly) BOOL negative;
@property (nonatomic, readonly) BOOL hasExponent;
@property (nonatomic, getter=isZero, readonly) BOOL zero;
@property (nonatomic, readonly, copy) BigFloat *duplicate;
@property (nonatomic, readonly, copy) BigFloat *pi;
- (BOOL)appendDigit: (short)digit useComplement:(int)complement;
- (void)appendExpDigit:(short)digit;
- (void)deleteDigitUseComplement:(int)complement;
- (void)deleteExpDigit;
- (void)convertToRadix:(unsigned short)newRadix;
- (NSComparisonResult)compareWith:(BigFloat*)num;
- (void)assign:(BigFloat*)newValue;
- (void)abs;
- (void)negate;
// Arithmetic Functions
- (void)add:(BigFloat*)num;
- (void)subtract:(BigFloat*)num;
- (void)multiplyBy:(BigFloat*)num;
- (void)divideBy:(BigFloat*)num;
- (void)moduloBy:(BigFloat*)num;
// Extended Mathematics Functions
- (void)powerOfE;
- (void)ln;
- (void)raiseToIntPower: (NSInteger)n;
- (void)raiseToPower:(BigFloat*)num;
- (void)sqrt;
- (void)cbrt;
- (void)inverse;
- (void)logOfBase:(BigFloat *)base;
- (void)sinWithTrigMode:(BFTrigMode)mode inv:(BOOL)useInverse hyp:(BOOL)useHyp;
- (void)cosWithTrigMode:(BFTrigMode)mode inv:(BOOL)useInverse hyp:(BOOL)useHyp;
- (void)tanWithTrigMode:(BFTrigMode)mode inv:(BOOL)useInverse hyp:(BOOL)useHyp;
- (void)factorial;
- (void)sum;
- (void)nPr: (BigFloat*)r;
- (void)nCr: (BigFloat*)r;
- (void)exp3Up;
- (void)exp3Down:(int)displayDigits;
- (void)wholePart;
- (void)fractionalPart;
- (void)bitnotWithComplement:(int)complement;
- (void)andWith:(BigFloat*)num usingComplement:(int)complement;
- (void)orWith:(BigFloat*)num usingComplement:(int)complement;
- (void)xorWith:(BigFloat*)num usingComplement:(int)complement;
- (void)nandWith:(BigFloat*)num usingComplement:(int)complement;
- (void)norWith:(BigFloat*)num usingComplement:(int)complement;
- (void)xnorWith:(BigFloat*)num usingComplement:(int)complement;
// Conversion Functions
@property (nonatomic, readonly) double doubleValue;
@property (nonatomic, readonly, copy) NSString *mantissaString;
@property (nonatomic, readonly, copy) NSString *exponentString;
@property (nonatomic, readonly, copy) NSString *toString;
- (NSString*)toShortString:(int)precision;
- (void)limitedString:(unsigned int)lengthLimit fixedPlaces:(unsigned int)places fillLimit:(BOOL)fill complement:(unsigned int)complement mantissa:(NSString**)mantissaOut exponent:(NSString**)exponentOut;
- (void)debugDisplay;
@end