Strange problems on decimals - dangerous bug !!

Strange problems on decimals - dangerous bug !!

Postby Marco Turco » Fri Jan 16, 2015 5:30 pm

Hi,
there is a really strange problem with decimal using last FWH and XHB versions.
I don't know if it is FW or xHarbour related anyway with my old FWH versions (2012) all runs well.

It's a bit difficult to make a self-contained sample, anyway the problem appear in this case

nDecimals is a numeric value I have read from my database and its value is 3

if I make str(87.877,10,nDecimals) it return 88 instead of 87.877 !!!
if I make str(87.877,10,3) it return 87.877 that is correct

It's a really crazy and Dangerous situation. At the moment I have renstalled the previous FWH/XHB version to go in production.

Any ideas ?
Best Regards,

Marco Turco
SOFTWARE XP LLP
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Re: Strange problems on decimals - dangerous bug !!

Postby James Bott » Fri Jan 16, 2015 6:24 pm

Hmm, have you checked to make sure that nDecimals is actually 3 and not zero?

MsgInfo( nDecimals == 3 )
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Strange problems on decimals - dangerous bug !!

Postby Marco Turco » Fri Jan 16, 2015 6:30 pm

Hi James,
I tried and it return .t.

The problem is xHarbour related !!

If I use FWH14.12 with my old xHarbour 1.2.1 rev 9656 all runs fine,
if I I use FWH 14.12 with xHarbour 1.2.3 rev build 20141106 the problem appears.

The xHarbour provided with FWH14.12 is buggy.
Best Regards,

Marco Turco
SOFTWARE XP LLP
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Re: Strange problems on decimals - dangerous bug !!

Postby James Bott » Fri Jan 16, 2015 7:02 pm

Yes, it would seem that then str() function is the problem. I can't imagine why that needed modification after all these years.
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Strange problems on decimals - dangerous bug !!

Postby Marco Turco » Fri Jan 16, 2015 9:25 pm

I'm not sure it is str() related, I suppose it is floating point related because it appears just using numbers loaded from a database not with numers manually assigned to a variable.
Antonio, pls. any ideas ?
Best Regards,

Marco Turco
SOFTWARE XP LLP
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Re: Strange problems on decimals - dangerous bug !!

Postby James Bott » Fri Jan 16, 2015 10:36 pm

I am having a hard time understanding why this is happening if nDecimal == 3.

str() should only be returning an integer if the third value is either zero or not passed. But you checked the value of nDecimal and it is not either zero or nil.
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Strange problems on decimals - dangerous bug !!

Postby Antonio Linares » Fri Jan 16, 2015 10:52 pm

Marco,

This example is properly working here using both Harbour and xHarbour

Code: Select all  Expand view  RUN
function Main()

   local nDecimals := 3

   MsgInfo( str(87.877,10,3) )

   MsgInfo( str(87.877,10,nDecimals) )

return nil


I get 87.877 in all cases
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Strange problems on decimals - dangerous bug !!

Postby James Bott » Fri Jan 16, 2015 11:32 pm

Marco,

It's a bit difficult to make a self-contained sample,


This makes me think that there is something else in your code that is triggering the problem.

Antonio's sample code is only 5 lines of code, so an example is simple. If you are getting it in your code, then it must be something else that is going on.

First, can you run Antonio's example with your suspect xHarbour version and tell us the result (and tell us the version number). Then try compiling your code again with that version and recheck it. If you are still getting the error then there is something else causing the error. It could still be differences in versions of xHarbour, but not in the str() function (as you mentioned).
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Strange problems on decimals - dangerous bug !!

Postby Marco Turco » Sat Jan 17, 2015 8:52 am

Hi Antonio,
your sample work well but it is not my case.
In my case the nDecimal value is loaded from a sqlite database. the field is "nDecimal smallint (2)" .
I have also generated the sqlite library using last xharbour version but the problem is always in place.

I will try to make a self contained sample to show you the error.
Best Regards,

Marco Turco
SOFTWARE XP LLP
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Re: Strange problems on decimals - dangerous bug !!

Postby Antonio Linares » Sat Jan 17, 2015 9:05 am

Marco,

use nDecimal this way:

Int( nDecimal )

Harbour may be waiting for an integer and you are providing a decimal number
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Strange problems on decimals - dangerous bug !!

Postby nageswaragunupudi » Sat Jan 17, 2015 10:11 am

One of the advantages of SqLite is that we can store any datatype in a column of any datatype. This is like storing into and retrieving from an array. This advantage can also be dangerous pitfall at times.

We are not sure at times what we accessed is a string "3" or double 3. or integer 3.
We need to doubly sure of the datatype and if necessary convert into the correct datatype before use.

Please doubly ensure that the value of nDecimal is a Harbour Integer before using it in the function Str(...). Please read the value into a memory variable, check valtype and then use the value.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10656
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Strange problems on decimals - dangerous bug !!

Postby James Bott » Sat Jan 17, 2015 5:21 pm

Marco,

Gee, you left out a very important bit of information, that you are using SQLite as the database.

Nages,

Is xHarbour capable of dealing with number types other that floating? When nDecimal is loaded from a SQL database with fieldtype "integer" what is valtype() going to return? "Integer?" I would have guessed that all numeric datatypes were converted to floating.
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Strange problems on decimals - dangerous bug !!

Postby Marco Turco » Sat Jan 17, 2015 7:02 pm

Hi, some more infos:
1. the valtype of the value read from the sqlite db is "N" so numeric that is correct
2. I also make this test: MsgInfo( str(87.877,10,int(nDecimals)) ) and Always return 88 (nDecimal had 3 as value)

This problem didn't exist with xHarbour 1.2.1 rev 9656 but appear with xHarbour 1.2.3 rev build 20141106

I'm working to a self-contained sample...

To Antonio: in the meantime do you think I can use FWH14.12 with xHarbour 1.2.1 rev 9656 ?
The str function runs well in this configuration.
Best Regards,

Marco Turco
SOFTWARE XP LLP
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Re: Strange problems on decimals - dangerous bug !!

Postby nageswaragunupudi » Sun Jan 18, 2015 12:06 am

in the meantime do you think I can use FWH14.12 with xHarbour 1.2.1 rev 9656 ?

You can use.

This is the version I am using.
xHarbour 1.2.3 Intl. (SimpLex) (Build 20141106)

Everything is working correctly for me. I tried to simulate the same situation using SqLite3 also.
We await your self contained sample.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10656
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Strange problems on decimals - dangerous bug !!

Postby Marco Turco » Sun Jan 18, 2015 7:09 pm

Hi Rao,
self contained sample available on http://109.228.12.120/softwaredistribut ... a/test.rar

Test.prg is a very simply self-contained that show the problem reading numeric data from sqlite db.

#include "fivewin.ch"

procedure start()

oSQLite := TSQLiteServer():New( "test.sql" )
If oSQLite:lError
msgStop( "error" )
RETURN
EndIf

cQuery:="SELECT * from t_tables;"
oQry:=oSQLite:Query(cQuery)
nDecimals:=oQry:aData[1,7]
oQry:End()
oSQLite:End()

msginfo(ndecimals) && return 3
msginfo(valtype(ndecimals)) && return N

msginfo(str(88.456,10,3)) && return 88.456
msginfo(str(88.456,10,nDecimals)) && RETURN 88 !!!
return
Best Regards,

Marco Turco
SOFTWARE XP LLP
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot], wmormar and 96 guests