libcg / bfp

Beyond Floating Point - Posit C/C++ implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Documentation

davidsummers opened this issue · comments

If you could add some simple documentation on how to use this library that would be great.

What numbers would be best for representing close to abilities for float and double?

I'm very interested in learning to use Posits for calculations to try for more precision and accuracy.

I'm a newbie at numerical analysis and numerical methods.

What do the numbers mean for Posit ( 5, 1 )?

I thought 5 might mean the total number of bits in the Posit number and one would be the total number of bits for the exponent? Is this correct? If so, how does it determine the number of bits for the fraction and the regime?

So for instance, I would like to try it out with this calculation, the same as in one of the Posit reference articles:

#include "cstdio"
#include "iostream"

#include "posit.h"

#define PREC float

class Posit32 : public Posit
{
public:

Posit32( PREC value_ ) :
  Posit( 32, 3 )
{
  set( value_ );
}

~Posit32( )
{
}

Posit32 operator + ( const Posit32 &rhs_ )
{
Posit32 r( rhs_ );
r.add( *this );
return r;
}

Posit32 operator * ( const Posit32 &rhs_ )
{
Posit32 r( rhs_ );
r.mul( *this );
return r;
}
void PrintStream( std::ostream & os_ ) const
{
os_ << getFloat( );
}

protected:

private:
};

std::ostream & operator << ( std::ostream &os_, const Posit32 &p_ )
{
p_.PrintStream( os_ );
return os_;
}

void test( );

int main(int argc, char *argv[])
{
auto p = Posit( 5, 1 );

for (unsigned i = 0; i < (unsigned)(1 << p.nbits()); i++) {
    p.setBits(i);
    p.print();
}

test( );

return 0;

}

void test( )
{
Posit32 a [ 4 ] = { 3.2e7, 1, -1, 8.0e7 };
Posit32 b [ 4 ] = { 4.0e7, 1, -1, -1.6e7 };
Posit32 c = ( a[ 0 ] * b[ 0 ] ) +
( a[ 1 ] * b[ 1 ] ) +
( a[ 2 ] * b[ 2 ] ) +
( a[ 3 ] * b[ 3 ] );

std::cout << "Calculation Result: " << c << std::endl;
}

My Result comes out to be:
Calculation Result: -1.6e+07

The correct answer is 2.

What am I doing wrong? (Probably lots, I'm just a newbie)

Thanks

Sorry for the late answer, here goes:

Posit(5, 1) indeed means 5 bits total and 1 bit for es. From this document, it looks like es=2 is recommended for 32-bit posits and es=3 is recommended for 64-bit. Fraction and regime bit counts vary depending on the currently represented number.

I tried out your code, it looks correct, however I suspect that the result is wrong because rounding is not implemented yet in bfp.

I just tried your test code with master. The answer comes out as 0 using Posit32. I think this is correct because I get the same result using floats. I get 2 when using doubles.