Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 6 of 6 total
Thread Why is Trunc(1.8888,2) rounding the number to 1.89?
Fri, Mar 14 2014 5:54 PMPermanent Link

Barry

Why is:

select Trunc(1.8888,2)

producing: 1.89?

Shouldn't it be 1.88? It looks like it is rounding the number to 2 decimal places as if I had executed  Round(1.8888,2) .


Also I noticed there is no Frac() function. I know I can write a function to do it, but I thought there should be a Frac() since it is the opposite of Trunc().

Maybe Tim doesn't give a Frac?

(Sorry Galactica fans, I just had to throw it in, even though it is misspelled I couldn't resist. I hope I don't get tossed from the forum!).

Barry
Sat, Mar 15 2014 1:09 AMPermanent Link

Adam Brett

Orixa Systems

No idea about the reason for the first question ... likely it is the ISO standard, as that is the usual reason (Smile

Like the second question.
Wed, Mar 19 2014 10:18 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Barry,

<< select Trunc(1.8888,2)
>
> producing: 1.89?

Shouldn't it be 1.88? It looks like it is rounding the number to 2 decimal
places as if I had executed  Round(1.8888,2) . >>

It's a bug.

<< Also I noticed there is no Frac() function. I know I can write a function
to do it, but I thought there should be a Frac() since it is the opposite of
Trunc(). >>

It's just not something that is normally included in the base function set
in SQL.

Tim Young
Elevate Software
www.elevatesoft.com
Tue, Mar 25 2014 12:01 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Barry,

Actually, this incident report's fix broke this incident report's regression
test:

http://www.elevatesoft.com/incident?action=viewrep&category=edb&release=2.11&incident=3682

So, it looks like I've got a floating-point approximation issue here that
isn't going to get solved completely unless I figure out a different way of
truncating floats.  The other alternative is to CAST your floats to DECIMAL
before calling the TRUNC() function (4 decimal places or less).

Tim Young
Elevate Software
www.elevatesoft.com
Tue, Mar 25 2014 5:04 PMPermanent Link

Barry

Tim,

How about:

EXECUTE IMMEDIATE 'CREATE FUNCTION "MyTrunc"  (IN "aNum" FLOAT, IN "aNumDec" INTEGER)
RETURNS FLOAT
BEGIN
 declare _Fact   Integer Default 0;
 declare _Result Float   default NULL;

 set _Fact = Power(10, aNumDec);
 set _Result = Trunc(aNum * _Fact) / _Fact;
 Return _Result;
END
VERSION 1.00';

Barry
Wed, Apr 2 2014 7:03 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Barry,

<< How about: >>

What about it ?

BTW, as you already know, I fixed the issue by using an epsilon to fix the
approximation issues.

Tim Young
Elevate Software
www.elevatesoft.com
Image