944,038 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3935
  • C++ RSS
May 29th, 2007
0

C++ Double - 8th point decimals

Expand Post »
Hi,

I have a problem that happens very rarely. I have a calculation which ads (for example) -15000.00 to 15575.51 and saves the results to an oracle database. However, the reslut in the databse is 575.51000002

Both figures are loaded elsewhere from the database and either have 0 or 2 decimals as in the example above.

The code processes millions of transactions, but less than .01 % has this error. Any ideas?

Thanks,
Shawn

AddUpInputAK(
int iIndexInputAK,
int iAccumPos,
int iAccumNeg,
double* naSumsBW,
double* naSumsRW )
{
int iRet = SQC_C_Success; int iIndexSum; double nBetragBook; double nBetragBest; double nBetragKto; double nBetragRef; int iAK_Vorz; //// // //// if ( ALG_IsIntNull( iIndexInputAK ) ) {
goto L_Eof;
} if ( ALG_IsIntNull( iAccumPos ) && ALG_IsIntNull( iAccumNeg ) ) {
goto L_Eof;
} iIndexSum = 0; nBetragBook = m_naInpAbrKompBetrag[iIndexInputAK]; if ( ALG_IsDoubleNull( nBetragBook ) ) {
goto L_Eof;
} //// // Correct signage //// iAK_Vorz = 1; if ( m_saInpAbrKompVorz[iIndexInputAK].Left( 1 ) == "-" ) {
iAK_Vorz = -1; nBetragBook = nBetragBook * ( -1.0);
} if ( m_sPosArt == "KTO" ) { //// // Accounts //// if ( m_sKtoWhg == m_saInpAbrKompWhg[iIndexInputAK] ) {
nBetragKto = m_naInpAbrKompBetrag[iIndexInputAK];
if ( iAK_Vorz < 0 ) {
nBetragKto = nBetragKto * ( -1 );
} else {
nBetragKto = nBetragBook;
} ALG_SetDoubleNull( &nBetragKto ); } if ( ! ALG_IsDoubleNull( nBetragKto ) ) {
naSumsBW[iIndexSum] = naSumsBW[iIndexSum] + nBetragKto;
} } L_Eof: return( iRet ); }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShawnMuller is offline Offline
3 posts
since Oct 2006
May 29th, 2007
0

Re: C++ Double - 8th point decimals

I'd guess that it's due to how floating point values are stored imprecisely in memory, causing the weird results to come up on rare occasions.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
May 29th, 2007
0

Re: C++ Double - 8th point decimals

>The code processes millions of transactions, but less than .01 % has this error. Any ideas?
That's a typical floating-point rounding fudge. If it's a serious enough problem, you can try to use a more precise type such as long double, or remove floating-point altogether to maintain exact precision. Though I suspect that an imprecision at the eighth decimal place isn't that much of an issue unless you're trying to do something with vanilla C++ types that shouldn't be done with vanilla C++ types. If that's the case, you really need to get a high precision mathematics library.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 30th, 2007
0

Re: C++ Double - 8th point decimals

Hi, thanks for all the replies.

The problem arises later on when external systems want to do a reconciliiation and then fails. This external system uses a language which generally does not have such problems and as such it hasn't been programmed to handle such differences.

Thanks again

Shawn
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShawnMuller is offline Offline
3 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: really dont know what to do, c++?
Next Thread in C++ Forum Timeline: Assertion failure in loading





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC