cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with decimal data type

robert_kratschmann
Participant
1,456

As the power function is in SQL Anywhere 17.0.10.5866 defined only for the double data type, I wrote a new power function only for integers called power_int:

create or replace function power_int( @int decimal(127,0), @j decimal (127,0) )
returns decimal(127,0)
as begin
declare @i decimal (127,0)
declare @a decimal (127,0)
if @j=0.0 set @int=1.0
set @i=1.0
set @a=@int
while @i<=@j-1.0
begin
set @int=@int*@a
set @i=@i+1.0
end
return @int
end;

The function generates for smaller results correct values. If the result has more the 33 digits it becomes wrong:

select power_int (16,27)
--324518553658426726783156020576200 --wrong result
--324518553658426726783156020576256 --correct

select power_int (2,107)
--162259276829213363391578010288100 --wrong result
--162259276829213363391578010288128 --correct

While power_int (16,26) and (2,106) gave the correct result. In ASE 16.0.x the function works fine if instead of decimal(127,0) decimal(38,0) will be used.

Many thanks

Robert

Accepted Solutions (1)

Accepted Solutions (1)

jack_schueler
Product and Topic Expert
Product and Topic Expert

set option PUBLIC.precision = 120; and retry

robert_kratschmann
Participant
0 Kudos

Works fine, never heard about this option.

Many thanks

jack_schueler
Product and Topic Expert
Product and Topic Expert
0 Kudos

Default precision and scale are PUBLIC database options that can be set. Check the documentation for more information.

Answers (0)