[try Beta version]
Not logged in

 
Float Data Type

Jun 21, 2013 at 7:02pm
As i know float has 8 digit, how about if i want n-digit ?
for example:
in float, PI = 3.142857 -->8 digit
how about if want 10 digit? or 20 digit ?
wish somebody could solve this for me. i would be very thankful.
Jun 21, 2013 at 7:03pm
Double can use 16. If you want any larger than that you better make your own class of a "BigInt" type.
Jun 21, 2013 at 7:26pm
what is the content of the class ?can u tell me?
Jun 21, 2013 at 7:43pm
^^ There's some stuff on it around the 'net in case you'd want to learn about how it works and write it yourself, but you could try using source that someone else already wrote. Like for instance http://cpp-bigint.sourceforge.net/

I remember back when I learned this in a class at my University and a homework assignment was to write code for a BigInt and then put it to use.
Last edited on Jun 21, 2013 at 8:50pm
Jun 21, 2013 at 10:40pm
Just create a new class for your custom number to start try something like
1
2
3
4
5
6
class BigInt
{
public:
    BigInt( void );
    BigInt( what ever you want here string might be easiest? );
};


Example using string:

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
class BigInt
{
public:
    BigInt( void );
    BigInt( const std::string& );
    BigInt& operator+=( const BigInt& );
private:
    std::string value;
};

BigInt::BigInt( void ) : value( "0" ) {}
BigInt::BigInt( const std::string &value ) : value( value ) {}
BigInt& BigInt::operator+= ( const BigInt &rhs )
{
    std::string temp( rhs.value ) , result( "" );
    std::reverse( value.begin() , value.end() );
    std::reverse( temp.begin() , temp.end() );
    short sum( 0u ) , carry( 0u ) , remainder( 0u );

    while( value.size() != temp.size() ) value.size() > temp.size() ? temp += '0' : value += '0';

    for( auto i( 0u ); i < value.size(); ++i )
    {
        sum = ( value[ i ] - '0' ) + ( temp[ i ] - '0' ) + carry;
        remainder = sum % 10;
        carry = ( sum - remainder ) / 10;
        result += remainder + '0';
    }
    if( carry > 0 ) result += carry + '0';
    std::reverse( result.begin() , result.end() );
    value = result;

    return( *this );
}


The code may not be very neat or well written though so good luck =p
Topic archived. No new replies allowed.