From 2e192969b03bfc9c446cf34f55a44399070b0713 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Sun, 27 Feb 2011 18:13:32 +0000 Subject: [PATCH] changed: Big::FromString_ReadPartAfterComma() now we're reading the all part as integer and at the end we're dividing it by (base ^ how_many_digits_there_were) it's faster and should be a little accurate in some cases git-svn-id: svn://ttmath.org/publicrep/ttmath/trunk@342 e52654a7-88a9-db11-a3e9-0013d4bc506e --- CHANGELOG | 3 ++- ttmath/ttmathbig.h | 43 ++++++++++++------------------------------- 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 5f8fa2e..0a27869 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,4 @@ -Version 0.9.3 prerelease (2011.01.30): +Version 0.9.3 prerelease (2011.02.27): * added: Parser::InitCGamma() initializing coefficients used when calculating the gamma (or factorial) function this speed up the next calculations @@ -6,6 +6,7 @@ Version 0.9.3 prerelease (2011.01.30): these coefficients will be calculated when needed * added: option 'group_digits' to Conv struct you can set how many digits should be grouped + * changed: small optimizations in UInt::ToString() and Big::FromString() Version 0.9.2 (2010.09.23): diff --git a/ttmath/ttmathbig.h b/ttmath/ttmathbig.h index dc3e98c..9361a6e 100644 --- a/ttmath/ttmathbig.h +++ b/ttmath/ttmathbig.h @@ -5120,19 +5120,14 @@ private: uint FromString_ReadPartAfterComma( const char_type * & source, const Conv & conv, bool & value_read ) { sint character; - uint c = 0, index = 1; - Big sum, part, power, old_value, base_( conv.base ); + uint c = 0, power = 0; + UInt<1> power_; + Big sum, base_(conv.base); // we don't remove any white characters here - - // this is only to avoid getting a warning about an uninitialized object 'old_value' which GCC reports - // (in fact we will initialize it later when the condition 'testing' is fulfilled) - old_value.SetZero(); - - power.SetOne(); sum.SetZero(); - for( ; true ; ++source, ++index ) + for( ; sum.exponent.IsSign() || sum.exponent.IsZero() ; ++source ) { if( conv.group!=0 && *source==static_cast(conv.group) ) continue; @@ -5144,30 +5139,13 @@ private: value_read = true; - part = character; - - if( power.Mul( base_ ) ) - // there's no sens to add the next parts, but we can't report this - // as an error (this is only inaccuracy) - break; - - if( part.Div( power ) ) - break; - - // every 5 iteration we make a test whether the value will be changed or not - // (character must be different from zero to this test) - bool testing = (character != 0 && (index % 5) == 0); - - if( testing ) - old_value = sum; - // there actually shouldn't be a carry here - c += sum.Add( part ); + c += sum.Mul(base_); + c += sum.Add(character); + power += 1; - if( testing && old_value == sum ) - // after adding 'part' the value has not been changed - // there's no sense to add any next parts - break; + if( power == 0 ) + c += 1; } // we could break the parsing somewhere in the middle of the string, @@ -5175,6 +5153,9 @@ private: // we should set a correct value of 'source' now for( ; Misc::CharToDigit(*source, conv.base) != -1 ; ++source ); + power_ = power; + c += base_.Pow(power_); + c += sum.Div(base_); c += Add(sum); return (c==0)? 0 : 1;