Sybase NNTP forums - End Of Life (EOL)

The NNTP forums from Sybase - forums.sybase.com - are now closed.

All new questions should be directed to the appropriate forum at the SAP Community Network (SCN).

Individual products have links to the respective forums on SCN, or you can go to SCN and search for your product in the search box (upper right corner) to find your specific developer center.

Help: setFloat setting 4.9 to 4.89999989899

2 posts in JDBC Connect (product renamed to JConnect) Last posting was on 1997-04-15 17:24:33.0Z
hcliff Posted on 1997-04-07 19:22:43.0Z
Message-ID: <33494983.3142@spacelab.net>
Date: Mon, 07 Apr 1997 15:22:43 -0400
From: hcliff <hcliff@spacelab.net>
Reply-To: hcliff@spacelab.net
X-Mailer: Mozilla 3.01 [fr] (WinNT; I)
MIME-Version: 1.0
Subject: Help: setFloat setting 4.9 to 4.89999989899
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Newsgroups: sybase.public.jdbcconnect
Lines: 63
Path: forums-1-dub!forums-master.sybase.com!forums.powersoft.com
Xref: forums-1-dub sybase.public.jdbcconnect:1051
Article PK: 252809

I'm trying to use a CallableStatement to execute a stored
procedure with parameters. When using setFloat and inserting
CERTAIN values then there appears to be some type of conversion
problem. If for example I call setFloat and the value that I'm
passing is 4.25 then Sybase correctly stores the value, however
if the value is something like 4.1, 4.9, 4.3, etc., then
it is converted to something like 4.099999382998....

Any ideas? I am including both the Java code snippet which
does the update and the Sybase Stored Proc which is getting
called.

Cliff Helsel
chelsel@spacelab.net

*************************** CODE FOLLOWS *************************

//Code snippet from my Java Save Routine

private void save()
{
String tenorstr, ratestr;
CallableStatement cstmt;
float fRate, fTenor;

try {

cstmt=con.prepareCall("{ call yld_writeyield(?,?,?,?) }");
for(int i=0,j=0;i<maxrows-1;i++)
{
tenorstr=(String)grid.getCellValue(i,0,"cellString");
ratestr=(String)grid.getCellValue(i,1,"cellString");

fRate=Float.valueOf(ratestr).floatValue();
fTenor=Float.valueOf(tenorstr).floatValue();
System.out.println(fTenor+" "+fRate);
cstmt.setString(1,curValue);

// THESE TWO LINES DON'T WORK, ex, if fRate==4.9 the value stored
// in Sybase ='s 4.8999998239948, however
// if the value is fRate==4.25 then the value is properly stored.

cstmt.setFloat(2,fTenor);
cstmt.setFloat(3,fRate);

cstmt.setString(4,"4/4/97");
cstmt.execute();
}
cstmt.close();

} catch (SQLException s)
{
System.out.println(s);
}

}

//STORED PROCEDURE WHICH IS CALLED by Save() method

CREATE PROCEDURE yld_writeyield
@currency varchar(20),
@tenor udd_valstor,
@rate udd_valstor,
@date datetime

AS

IF EXISTS ( SELECT * FROM yields WHERE yld_currency = @currency AND
yld_tenor = @tenor )
UPDATE yields
SET yld_rate = @rate,
pag_today = @date
WHERE yld_currency = @currency
AND yld_tenor = @tenor
ELSE
INSERT yields
VALUES ( @currency, @tenor, @rate, @date )

// sp_help returns this info about User defined type 'udd_valstor' in
above proc.
Type_name Storage_type Length Prec Scale Nulls Default_name
Rule_name Identity
--------------- --------------- ------ ---- ----- ----- ---------------
--------------- --------
udd_valstor float 8 (null(null)0 (null)
(null) 0


David Clegg Posted on 1997-04-15 17:24:33.0Z
Message-ID: <3353B9D1.42E2093@sybase.com>
Date: Tue, 15 Apr 1997 10:24:33 -0700
From: David Clegg <davec@sybase.com>
X-Mailer: Mozilla 2.01 (X11; I; Linux 1.2.13 i586)
MIME-Version: 1.0
To: hcliff@spacelab.net
Subject: Re: Help: setFloat setting 4.9 to 4.89999989899
References: <33494983.3142@spacelab.net>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Newsgroups: sybase.public.jdbcconnect
Lines: 86
Path: forums-1-dub!forums-master.sybase.com!forums.powersoft.com
Xref: forums-1-dub sybase.public.jdbcconnect:1005
Article PK: 252759

Strange as it may seem the behavior you are seeing is correct.
The numbers you are looking at are the same to the precision
which "float" saves them, and the only reason that they look
different when printed out as strings is in the way that the
toString() method for the float is deciding to display stuff.
There IS NO exact float representation for 4.1 -

dave

>
> I'm trying to use a CallableStatement to execute a stored
> procedure with parameters. When using setFloat and inserting
> CERTAIN values then there appears to be some type of conversion
> problem. If for example I call setFloat and the value that I'm
> passing is 4.25 then Sybase correctly stores the value, however
> if the value is something like 4.1, 4.9, 4.3, etc., then
> it is converted to something like 4.099999382998....
>
> Any ideas? I am including both the Java code snippet which
> does the update and the Sybase Stored Proc which is getting
> called.
>
> Cliff Helsel
> chelsel@spacelab.net
>
> *************************** CODE FOLLOWS *************************
>
> //Code snippet from my Java Save Routine
>
> private void save()
> {
> String tenorstr, ratestr;
> CallableStatement cstmt;
> float fRate, fTenor;
>
> try {
>
> cstmt=con.prepareCall("{ call yld_writeyield(?,?,?,?) }");
> for(int i=0,j=0;i<maxrows-1;i++)
> {
> tenorstr=(String)grid.getCellValue(i,0,"cellString");
> ratestr=(String)grid.getCellValue(i,1,"cellString");
>
> fRate=Float.valueOf(ratestr).floatValue();
> fTenor=Float.valueOf(tenorstr).floatValue();
> System.out.println(fTenor+" "+fRate);
> cstmt.setString(1,curValue);
>
> // THESE TWO LINES DON'T WORK, ex, if fRate==4.9 the value stored
> // in Sybase ='s 4.8999998239948, however
> // if the value is fRate==4.25 then the value is properly stored.
>
> cstmt.setFloat(2,fTenor);
> cstmt.setFloat(3,fRate);
>
> cstmt.setString(4,"4/4/97");
> cstmt.execute();
> }
> cstmt.close();
>
> } catch (SQLException s)
> {
> System.out.println(s);
> }
>
> }
>
> //STORED PROCEDURE WHICH IS CALLED by Save() method
>
> CREATE PROCEDURE yld_writeyield
> @currency varchar(20),
> @tenor udd_valstor,
> @rate udd_valstor,
> @date datetime
>
> AS
>
> IF EXISTS ( SELECT * FROM yields WHERE yld_currency = @currency AND
> yld_tenor = @tenor )
> UPDATE yields
> SET yld_rate = @rate,
> pag_today = @date
> WHERE yld_currency = @currency
> AND yld_tenor = @tenor
> ELSE
> INSERT yields
> VALUES ( @currency, @tenor, @rate, @date )
>
> // sp_help returns this info about User defined type 'udd_valstor' in
> above proc.
> Type_name Storage_type Length Prec Scale Nulls Default_name
> Rule_name Identity
> --------------- --------------- ------ ---- ----- ----- ---------------
> --------------- --------
> udd_valstor float 8 (null(null)0 (null)
> (null) 0