summaryrefslogtreecommitdiff
path: root/src/number.cpp
diff options
context:
space:
mode:
authorMike Buland <mike@xagasoft.com>2013-05-09 15:44:34 -0600
committerMike Buland <mike@xagasoft.com>2013-05-09 15:44:34 -0600
commit0e5e01b4d0d5f3f872d97c95bd57fd057e4fd5a1 (patch)
tree1e6a657c57ceeda4d699f1f0b55ad033e3cacf7b /src/number.cpp
parentce68e816bd82612c14f3492e8bc969da9bfab06c (diff)
downloadclic-0e5e01b4d0d5f3f872d97c95bd57fd057e4fd5a1.tar.gz
clic-0e5e01b4d0d5f3f872d97c95bd57fd057e4fd5a1.tar.bz2
clic-0e5e01b4d0d5f3f872d97c95bd57fd057e4fd5a1.tar.xz
clic-0e5e01b4d0d5f3f872d97c95bd57fd057e4fd5a1.zip
Added Number::toRadix & Number::set( int32_t ).
Both very handy. I'll add other numeric setters later, it was very easy.
Diffstat (limited to 'src/number.cpp')
-rw-r--r--src/number.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/number.cpp b/src/number.cpp
index eb4d01d..cab7f46 100644
--- a/src/number.cpp
+++ b/src/number.cpp
@@ -358,6 +358,17 @@ void Number::set( const Number &sNum )
358 iRadix = sNum.iRadix; 358 iRadix = sNum.iRadix;
359} 359}
360 360
361void Number::set( int32_t iNum )
362{
363 aFrac.zero();
364 aInt.clear();
365 while( iNum > 0 )
366 {
367 aInt.append( iNum%iRadix );
368 iNum /= iRadix;
369 }
370}
371
361void Number::divide( const Number &rhs, Number &q, Number &r ) const 372void Number::divide( const Number &rhs, Number &q, Number &r ) const
362{ 373{
363 if( rhs.iScale > 0 ) 374 if( rhs.iScale > 0 )
@@ -598,6 +609,25 @@ int32_t Number::toInt32() const
598 return ret; 609 return ret;
599} 610}
600 611
612Number Number::toRadix( int iNewRadix, int iNewScale ) const
613{
614 if( iNewScale < 0 )
615 iNewScale = iScale;
616 Number ret( iNewScale, iNewRadix );
617
618 Number me( 0, iRadix );
619 me = *this;
620 Number n( 0, iRadix );
621 n.set( iNewRadix );
622 while( !me.isZero() )
623 {
624 ret.aInt.append( (me%n).toInt32() );
625 me = me / n;
626 }
627
628 return ret;
629}
630
601Number Number::add( const Number &rhs, bool bSub ) const 631Number Number::add( const Number &rhs, bool bSub ) const
602{ 632{
603 Number ret( Bu::buMax(iScale,rhs.iScale), iRadix ); 633 Number ret( Bu::buMax(iScale,rhs.iScale), iRadix );