Alaska Software Inc. - Problem with sums
Username: Password:
AuthorTopic: Problem with sums
Paulo PinheiroProblem with sums
on Tue, 10 Mar 2009 17:15:13 +0000
Hello All,

I know this is not a new problem, and probably not completly a Xbase++
problem,  but i believe an effort should be done by Alaska in order to
correct it.

I include a small example (of a real problem) that doesn't work correctly.
The goal is to check if a sum of values (aValues) is equal to a given total
(nTotal).

If we uncomment the line <nSum := Round( nSum, 2 )> this works.


PROCEDURE Main()

  local nSum
  local aValues := { 2.02, 2.21, 1.31 }
  local nTotal := 5.54

  nSum := 0.00
  AEval( aValues, { |x| nSum += x } )
  **** nSum := Round( nSum, 2 )

  if nSum # nTotal
    ? "BAD", nSum, nTotal
  else
    ? "OK"
  endif

  Inkey(0)

RETURN
James Loughner Re: Problem with sums
on Tue, 10 Mar 2009 13:35:23 -0400
This is due to rounding errors in binary math and a finite length of the
binary numbers. You must always round if you want an exact comparison.

This has been discussed many many times on the board.

Jim

Paulo Pinheiro wrote:
> Hello All,
> 
> I know this is not a new problem, and probably not completly a Xbase++
> problem,  but i believe an effort should be done by Alaska in order to
> correct it.
> 
> I include a small example (of a real problem) that doesn't work correctly.
> The goal is to check if a sum of values (aValues) is equal to a given total
> (nTotal).
> 
> If we uncomment the line <nSum := Round( nSum, 2 )> this works.
> 
> 
> PROCEDURE Main()
> 
>   local nSum
>   local aValues := { 2.02, 2.21, 1.31 }
>   local nTotal := 5.54
> 
>   nSum := 0.00
>   AEval( aValues, { |x| nSum += x } )
>   **** nSum := Round( nSum, 2 )
> 
>   if nSum # nTotal
>     ? "BAD", nSum, nTotal
>   else
>     ? "OK"
>   endif
> 
>   Inkey(0)
> 
> RETURN
> 
> 
> 
> 
>
Thomas Braun
Re: Problem with sums
on Fri, 13 Mar 2009 08:50:13 +0100
James Loughner wrote:

> This is due to rounding errors in binary math and a finite length of the
> binary numbers. You must always round if you want an exact comparison.
> 
> This has been discussed many many times on the board.

I'd like to add a "there is nothing Alaska possibly could do about this"


Thomas
Rodd Graham Re: Problem with sums
on Fri, 13 Mar 2009 10:36:48 +0000
Hello Thomas,

>> This is due to rounding errors in binary math and a finite length of
>> the binary numbers. You must always round if you want an exact
>> comparison.
>> 
>> This has been discussed many many times on the board.
>> 
> I'd like to add a "there is nothing Alaska possibly could do about
> this"
> 

Besides creating a FAQ or switching to a BCD math library 

Regards,

Rodd Graham, Consultant
Graham Automation Systems, LLC
Jorge LRe: Problem with sums
on Fri, 13 Mar 2009 08:12:02 -0300
Hello
In my opinion you need to make this

Function MyRound( nNum, nDec )

      Return ( Val( StrTran( Str( nNum, 12, nDec ), ',', '.' )  ), nDec ) 
/// strtran() to prevent the regional settings
Val() ingnore ","

or work with enteries numbers and convert to decimal to the representation 
on the screen


Regards

"Paulo Pinheiro" <paulo.pinheiro@modulac.pt> escribió en el mensaje de 
noticias news:24515de8$7a4a54d1$a74@news.alaska-software.com...
> Hello All,
>
> I know this is not a new problem, and probably not completly a Xbase++
> problem,  but i believe an effort should be done by Alaska in order to
> correct it.
>
> I include a small example (of a real problem) that doesn't work correctly.
> The goal is to check if a sum of values (aValues) is equal to a given 
> total
> (nTotal).
>
> If we uncomment the line <nSum := Round( nSum, 2 )> this works.
>
>
> PROCEDURE Main()
>
>  local nSum
>  local aValues := { 2.02, 2.21, 1.31 }
>  local nTotal := 5.54
>
>  nSum := 0.00
>  AEval( aValues, { |x| nSum += x } )
>  **** nSum := Round( nSum, 2 )
>
>  if nSum # nTotal
>    ? "BAD", nSum, nTotal
>  else
>    ? "OK"
>  endif
>
>  Inkey(0)
>
> RETURN
>
>
>
>
>
>