- Inherits from:
- Object
- Conforms to:
- DComparable, DParsable, DSimpleArithmetic, DTextable
- Declared in:
- DBigFraction.h
Object
|
+---DBigFraction
Class Description
The big fraction class implements a number of methods for working with
arbitrary large fraction numbers, also called rational numbers.
The class uses the excellent gmp library for the actual calculations.
- Example:
#include <stdio.h>
#include "ofc/DBigFraction.h"
int main(int argc, char *argv[])
{
DBigFraction *fra1 = [DBigFraction alloc];
DBigFraction *fra2 = [DBigFraction new ];
DBigFraction *fra3 = [DBigFraction new ];
DText *str;
[fra1 init :"6/4" :10]; // Init fraction with 6/4 (base 10)
[fra2 fraction :8 :10]; // Set fraction with 8/10
[fra2 mul :fra1]; // fra2 = fra2 * fra1
[fra3 sub :fra2 :fra1]; // fra3 = fra2 - fra1
printf("Fraction (as double):%f\n", [fra3 get]); // Get fraction as double
str = [fra3 get :10]; // Get fraction as string, base 10
printf("Fraction (as string):%s\n", [str cstring]);
if ([fra1 compare :fra2] == 0) // Compare numbers
printf("Fraction1 is equal to fraction2\n");
else if ([fra1 compare :fra2] < 0)
printf("Fraction1 is smaller than fraction2\n");
else
printf("Fraction1 is greater than fraction2\n");
[str free]; // Cleanup
[fra3 free];
[fra2 free];
[fra1 free];
return 0;
}
- Last modified:
- 19-Jul-2008 (DBigFraction.h)
Instance Variables
- private void *_value
- the big fraction value
- Constructors
- - (DBigFraction *) init
- Initialise to fraction zero
- Returns:
- the object
- - (DBigFraction *) init :(double) value
- Initialise the fraction with a double
- Parameters:
- value - the double value
- Returns:
- the object
- - (DBigFraction *) init :(const char *) cstring :(int) base
- Initialise the fraction with a text string
- Parameters:
- cstring - the cstring with the fraction
base - the base for the fraction
- Returns:
- the object
- Copy constructor
- - shallowCopy
- Do a shallow copy of the big fraction
- Returns:
- the object
- Deconstructor
- - free
- Free a big fraction
- Returns:
- the object
- Set methods
- - (DBigFraction *) clear
- Set the big fraction to zero
- Returns:
- the object
- - (DBigFraction *) fraction :(int) num :(int) denom
- Set the big fraction with (small) numerator and denominator
- Parameters:
- num - the numerator
denom - the denominator
- Returns:
- the object
- - (DBigFraction *) move :(DBigFraction *) other
- Move an other object in this object
- Parameters:
- other - the other object
- Returns:
- this object
- - (DBigFraction *) set :(double) value
- Set the big fraction with a double
- Parameters:
- value - the double value
- Returns:
- the object
- - (BOOL) set :(const char *) cstring :(int) base
- Set the big fraction with a text string
- Parameters:
- cstring - the cstring with the fraction
base - the base of the number (2..36)
- Returns:
- success
- Get methods
- - (long) denominator
- Get the denominator (as small integer)
- Returns:
- the denominator
- - (double) get
- Get the big fraction as a double
- Returns:
- the double
- - (DText *) get :(int) base
- Get the big fraction as a text string
- Parameters:
- base - the base for the text string (2..36)
- Returns:
- a (new) text string (or nil)
- - (long) numerator
- Get the numerator (as small integer)
- Returns:
- the numerator
- - (double) toDouble
- Convert to a double
- Returns:
- the double
- Methods on big fraction numbers
- - (DBigFraction *) abs
- Absolute the big fraction
- Returns:
- the object
- - (DBigFraction *) add :(DBigFraction *) other
- Add with an other big fraction
- Parameters:
- other - the big fraction
- Returns:
- the object
- - (DBigFraction *) add :(DBigFraction *) src1 :(DBigFraction *) src2
- Add two big fractions and store the result in the object
- Parameters:
- src1 - the first big fraction
src2 - the second big fraction
- Returns:
- the object
- - (DBigFraction *) div :(DBigFraction *) other
- Divide with an other big fraction
- Parameters:
- other - the divisor big fraction
- Returns:
- the object
- - (DBigFraction *) div :(DBigFraction *) src1 :(DBigFraction *) src2
- Divide two big fractions and store the result in the object
- Parameters:
- src1 - the dividend big fraction
src2 - the divisor big fraction
- Returns:
- the object
- - (DBigFraction *) invert
- Invert the big fraction
- Returns:
- the object
- - (DBigFraction *) mul :(DBigFraction *) other
- Multiply with an other big fraction
- Parameters:
- other - the big fraction
- Returns:
- the object
- - (DBigFraction *) mul :(DBigFraction *) src1 :(DBigFraction *) src2
- Multiply two big fractions and store the result in the object
- Parameters:
- src1 - the first big fraction
src2 - the second big fraction
- Returns:
- the object
- - (DBigFraction *) negate
- Negate the big fraction
- Returns:
- the object
- - (DBigFraction *) norm
- Normalize the big fraction
- Returns:
- the object
- - (DBigFraction *) sub :(DBigFraction *) other
- Subract with an other big fraction
- Parameters:
- other - the big fraction
- Returns:
- the object
- - (DBigFraction *) sub :(DBigFraction *) src1 :(DBigFraction *) src2
- Subract two big fractions and store the result in the object
- Parameters:
- src1 - the first big fraction
src2 - the second big fraction
- Returns:
- the object
- DComparable protocol implementation
- - (int) compare :(DBigFraction *) other
- Compare with an other big fraction
- Parameters:
- other - the other big fraction
- Returns:
- the compare result ( <0, 0, 0> )
- Textable protocol implementation
- - (DText *) toText
- Convert to a decimal text string
- Returns:
- a (new) text string with the big fraction (or nil)
- DParsable protocol implementation
- - (int) fromString :(char **) cstr
- Parse a string for a big fraction. Format: [space]*[+-]number/number
and number is one of:
decimal: [1..9][0..9]*
hexadecimal: 0x[0..9a..f]*
octal: 0[0..7]*
- Parameters:
- cstr - the string to be parsed (moved to the first non-parsable char)
- Returns:
- the result (0,ERANGE,ENODATA)
generated 06-Sep-2008 by ObjcDoc 3.0.0