comp.lang.ada
 help / color / mirror / Atom feed
* Decimal number of interger?
@ 2001-10-24 17:46 a98mican
  2001-10-24 19:54 ` tmoran
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: a98mican @ 2001-10-24 17:46 UTC (permalink / raw)


Hi!
How do I determine weather the result from a function has a decimal part
or not?
I want to use the log function to se weather the width of the terrain is
(2^x)+1. In order to do this I want to use the following:
log2(width-1) = "float without decimal part"


Thanks in advance!
/Michael Andersson




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Decimal number of interger?
  2001-10-24 17:46 Decimal number of interger? a98mican
@ 2001-10-24 19:54 ` tmoran
  2001-10-24 23:35   ` Jeffrey Carter
  2001-10-25  1:38 ` DuckE
  2001-10-29 15:36 ` Tucker Taft
  2 siblings, 1 reply; 5+ messages in thread
From: tmoran @ 2001-10-24 19:54 UTC (permalink / raw)


>How do I determine weather the result from a function has a decimal part
>or not?
  x : float := function_value(y);
begin
  if float(integer(x)) = x then -- decimal part of x is zero

>I want to use the log function to se weather the width of the terrain is
>(2^x)+1. In order to do this I want to use the following:
>log2(width-1) = "float without decimal part"
  test : interfaces.unsigned_32 := interfaces.unsigned_32(width-1);
begin
  if (test and (test-1)) = 0 then -- test (ie, width-1) is a power of 2.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Decimal number of interger?
  2001-10-24 19:54 ` tmoran
@ 2001-10-24 23:35   ` Jeffrey Carter
  0 siblings, 0 replies; 5+ messages in thread
From: Jeffrey Carter @ 2001-10-24 23:35 UTC (permalink / raw)


tmoran@acm.org wrote:
> 
> >How do I determine weather the result from a function has a decimal part
> >or not?
>   x : float := function_value(y);
> begin
>   if float(integer(x)) = x then -- decimal part of x is zero
> 
> >I want to use the log function to se weather the width of the terrain is
> >(2^x)+1. In order to do this I want to use the following:
> >log2(width-1) = "float without decimal part"
>   test : interfaces.unsigned_32 := interfaces.unsigned_32(width-1);
> begin
>   if (test and (test-1)) = 0 then -- test (ie, width-1) is a power of 2.

This will probably work for the values expected from such an
application, but for the general case in which Width - 1 may not fit in
any integer type, the floating-point attribute 'Floor (or 'Truncation,
if the value may be negative) yields a floating-point value with no
fractional part.

-- 
Jeffrey Carter



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Decimal number of interger?
  2001-10-24 17:46 Decimal number of interger? a98mican
  2001-10-24 19:54 ` tmoran
@ 2001-10-25  1:38 ` DuckE
  2001-10-29 15:36 ` Tucker Taft
  2 siblings, 0 replies; 5+ messages in thread
From: DuckE @ 2001-10-25  1:38 UTC (permalink / raw)


I would use something like:

  IF Abs( log2(width-1) - Float'Truncation( log2(width - 1) ) ) <
someMinDelta THEN
    ...

where "someMinDelta" gives an indication of how close is close enough.

SteveD

<a98mican@ida.his.se> wrote in message news:3BD70DE5.C2D2B6FD@ida.his.se...
> Hi!
> How do I determine weather the result from a function has a decimal part
> or not?
> I want to use the log function to se weather the width of the terrain is
> (2^x)+1. In order to do this I want to use the following:
> log2(width-1) = "float without decimal part"
>
>
> Thanks in advance!
> /Michael Andersson
>





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Decimal number of interger?
  2001-10-24 17:46 Decimal number of interger? a98mican
  2001-10-24 19:54 ` tmoran
  2001-10-25  1:38 ` DuckE
@ 2001-10-29 15:36 ` Tucker Taft
  2 siblings, 0 replies; 5+ messages in thread
From: Tucker Taft @ 2001-10-29 15:36 UTC (permalink / raw)


a98mican@ida.his.se wrote:
> 
> Hi!
> How do I determine weather the result from a function has a decimal part
> or not?
> I want to use the log function to se weather the width of the terrain is
> (2^x)+1. In order to do this I want to use the following:
> log2(width-1) = "float without decimal part"
> 
> Thanks in advance!
> /Michael Andersson

The attribute function S'Remainder(x, y) can be used to get the
"fractional" part, as follows:
    if Float'Remainder(X, 1.0) = 0.0 then 
        -- X has no fractional part
    else
        -- X has a fractional part
    end if;

On the other hand, you are using two relatively expensive floating
point operations to determine something that might be done more cheaply
in some other way.  E.g., getting the exponent (presuming the
Machine_Radix is 2, which it almost certainly is unless you are
on a mainframe) using Float'Exponent() and then using Float'Compose()
to build a new value that is 2^exponent.  Alternatively, Float'Fraction
might be the perfect function, because Float'Fraction(width-1) will be
exactly 0.50 in the case of interest (again, presuming Machine_Radix
is 2).
-- 
-Tucker Taft   stt@avercom.net   http://www.avercom.net
Chief Technology Officer, AverCom Corporation (A Titan Company) 
Bedford, MA  USA (AverCom was formerly the Commercial Division of AverStar:
http://www.averstar.com/~stt)



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2001-10-29 15:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-24 17:46 Decimal number of interger? a98mican
2001-10-24 19:54 ` tmoran
2001-10-24 23:35   ` Jeffrey Carter
2001-10-25  1:38 ` DuckE
2001-10-29 15:36 ` Tucker Taft

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox