summaryrefslogtreecommitdiff
path: root/src/number.h
blob: 0816a17c36f63e0ba58174c501b27bb56df19555 (plain)
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
#ifndef NUMBER_H
#define NUMBER_H

#include <bu/string.h>
#include "packedintarray.h"

class Number
{
public:
    Number( int iScale=0, int iRadix=10 );
    Number( const Bu::String &sData, int iScale=0, int iRadix=10 );
    virtual ~Number();

    Number &operator=( const Bu::String &sNum );
    Number &operator=( const Number &rhs );
    
    Number operator+( const Number &rhs ) const;
    Number operator-( const Number &rhs ) const;
    Number operator*( const Number &rhs ) const;
    Number operator/( const Number &rhs ) const;
    Number operator%( const Number &rhs ) const;
    Number operator-() const;

    bool operator==( const Number &rhs ) const;
    bool operator!=( const Number &rhs ) const;
    bool operator>( const Number &rhs ) const;
    bool operator<( const Number &rhs ) const;
    bool operator>=( const Number &rhs ) const;
    bool operator<=( const Number &rhs ) const;

    void set( const Bu::String &sNum );
    void set( const Number &sNum );
    
    void divide( const Number &rhs, Number &q, Number &r ) const;
    bool isZero() const;

    operator Bu::String() const
    {
        return aInt.toString();
    }

    Bu::String toString() const;

    int digit( int iIdx ) const;

    int getScale() const { return iScale; }
    void setScale( int iNewScale );

    int32_t toInt32() const;

private:
    Number add( const Number &rhs, bool bSub ) const;

private:
    int iRadix;
    int iScale;
    bool bPositive;
    PackedIntArray aInt;
    PackedIntArray aFrac;
};

Bu::Formatter &operator<<( Bu::Formatter &f, const Number &n );

#endif