Is there a bug?

145 views
Skip to first unread message

marcelo.linhaverde

unread,
Jul 30, 2025, 1:15:51 PMJul 30
to Harbour Developers
Hello. Good afternoon.

For years, I've encountered an unusual situation when comparing variables with zero. I build a simple example with specific values:

function main()
local a
local e

a:={-405.97,135.00,135.00,135.97}
e:=0
aeval( a,{|i| e+=i } )

? transform( e,"99999.99")

if e<0
? "Negative"
endif
if e>0
? "Positive"
endif
if e==0
? "Zero"
endif

return( nIL )

The code above displays:

0.00
Negative


The order of the values in the array affects the final result. Seems crazy!

My build: Harbour 3.2.0dev (r2410180556)

Can anyone help?

José M. C. Quintas

unread,
Jul 30, 2025, 2:47:54 PMJul 30
to harbou...@googlegroups.com

operation with float point.

confirm using anything like this 

? e / 1.00000000000000000000000000000000000000

possible workaround on operation and/or result

e := val(str(e,2))

May be you have same problem using double on mysql, but not on currency or decimal.

José M. C. Quintas

--
You received this message because you are subscribed to the Google Groups "Harbour Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-deve...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/harbour-devel/f88c1ee2-efa3-4973-9f33-1b5457dc70fcn%40googlegroups.com.

Mario H. Sabado

unread,
Jul 31, 2025, 12:54:14 AMJul 31
to harbou...@googlegroups.com

Hi,

Displaying the result of e from your sample with higher decimal precision.

? transform(e,"999.999999999999999999999")  
 -0.000000000000028421709

You may need to use Round() function to achieve the result you expected.

Regards,
Mario

















--

Jose Carlos da Rocha Rocha

unread,
Jul 31, 2025, 12:55:14 AMJul 31
to Harbour Developers
Hi friends,

ZERO is not a positive on negative number. ZERO is ZERO.

In my very old Harbour Compiler Alpha build 45.0 (Flex) Copyright 1999-2005, http://www.harbour-project.org/

I tested this same code and the result was the same. 0 and Negative.

Try this code and be shocked

function main()
local a
local e

a:={-405.97,135.00,135.00,135.97}
e:=0
aeval( a,{|i| e+=i } )

? transform( e,"99999.999999999999999999999999")


if e<0
   ? "Negative"
endif
if e>0
   ? "Positive"
endif
if e=0.00

   ? "Zero"
endif

return( nIL )

Marek Długosz

unread,
Jul 31, 2025, 2:21:36 AMJul 31
to harbou...@googlegroups.com
I use conversion to whole numbers by *100 and divide after calcuations
or keep error of rounding in check:
e:=0
aeval({1/3,1/3,1/3},{lxle:=x-(x:=round(x+e,2)),qout(tran(x,"9.99"))})

31 lip 2025 06:55:13 Jose Carlos da Rocha Rocha <5vol...@gmail.com>:

signature.asc

frank van nuffel

unread,
Jul 31, 2025, 2:37:22 AMJul 31
to Harbour Developers
Hi,

(i haven't tested) but what about comparing to 0.00 instead of to 0?

regards, lohen

Op woensdag 30 juli 2025 om 19:15:51 UTC+2 schreef marcelo.linhaverde:

Alex Strickland

unread,
Jul 31, 2025, 3:36:43 AMJul 31
to harbou...@googlegroups.com

Hi

Numbers are stored as floating point (double, float) in Harbour (and Clippre) but these issues affect all languages that use them. You will find many many articles explaining the problem. Rounding is all you can do.

All programmers should know this but I managed to get a computer science degree without being made aware of it :)

--

Regards

Alex

marcelo.linhaverde

unread,
Aug 4, 2025, 12:42:18 PMAug 4
to Harbour Developers
Hi. This workaround works very well.

#xcommand DEFAULT <v1> TO <x1> [, <vn> TO <xn> ] => ;
                                IF <v1> == NIL ; <v1> := <x1> ; END ;
                                [; IF <vn> == NIL ; <vn> := <xn> ; END ]

function IsNegativo( n,nDec )
   local j

DEFAULT nDec TO   set( _SET_DECIMALS)

   j := -1* ( 10^(-nDec ) )

return( n <= j )

function IsPositivo( n, nDec )
local j

DEFAULT nDec TO   set( _SET_DECIMALS)

j :=  10^(-nDec )

return( n >= j )

function IsZero( n,nDec )

DEFAULT nDec TO   set( _SET_DECIMALS)

   return( !( IsNegativo( n, nDec ) .or. IsPositivo( n, nDec ) ) )

But, I have a huge code.

 Is there a away do a "translate" IP statetent ou replace if() function? Sugestions?

Thanks for all anwsers.

Marcelo G Tavares

José M. C. Quintas

unread,
Aug 4, 2025, 1:55:36 PMAug 4
to harbou...@googlegroups.com

AEval( a, { | i | e := Val(Str( e + i, 15,2)) }

or at the end

e := Val( Str( e, 15, 2 ))

On this way, you do not need extra functions.

José M. C. Quintas

Reply all
Reply to author
Forward
0 new messages