[comp.lang.pascal] Realint qestion

s32935k@taltta.hut.fi (Carl Torsten Stenholm) (05/12/89)

I was looking at a bit of software posted on the net and there was a function
looking like this (I hope I don't break any copyrights now...)

function realint (x: real): real;
begin
  realint := x - (x - trunc (x));
end;

As far as I can see this must be the same as 

function realint (x: real): real;
begin
  realint :=  nt := trunc (x); 
end;

Well now the only difference between this function and putting "trunc (x)" 
directly in the code is that trunc returns an integer (I  think). In that
case -- is there any situation where you must have an real returned instead
of an integer and thus forced to use this function ?


                                  Ctrl-C.Stenholm
                                  32935k@taltta.hut.fi


    BTW -- A while a go someone (I just can't find his mail adress, I've looked
    everywhere) posted a TPU file called TRI_D.TPU. I think this was compiled
    using turbo using ver 4.0. I can't get it to work on my 5.0, is there any
    chance of getting it posted compiled with 5.0 -- or does anybody know who
    posted it.

diamond@diamond.csl.sony.junet (Norman Diamond) (05/14/89)

In article <21930@santra.UUCP> s32935k@taltta.hut.fi (Carl Torsten Stenholm) writes:

>I was looking at a bit of software posted on the net and there was a function
>looking like this (I hope I don't break any copyrights now...)

>function realint (x: real): real;
>begin
>  realint := x - (x - trunc (x));
>end;

>As far as I can see this must be the same as 

>function realint (x: real): real;

(* inserting on your behalf *)
var nt : integer;

>begin
>  realint :=  nt := trunc (x); 
>end;

>Well now the only difference between this function and putting "trunc (x)" 
>directly in the code is that trunc returns an integer (I  think). In that
>case -- is there any situation where you must have an real returned instead
>of an integer and thus forced to use this function ?

Suppose MAXINT is 2147whatever (about 2 billion), and x is -2.35e+3000 ?

--
Norman Diamond, Sony Computer Science Lab (diamond%csl.sony.co.jp@relay.cs.net)
  The above opinions are my own.   |  Why are programmers criticized for
  If they're also your opinions,   |  re-implementing the wheel, when car
  you're infringing my copyright.  |  manufacturers are praised for it?

damm@freja.diku.dk (Kristian Damm Jensen) (05/15/89)

diamond@diamond.csl.sony.junet (Norman Diamond) writes:

>In article <21930@santra.UUCP> s32935k@taltta.hut.fi (Carl Torsten Stenholm) writes:

>>I was looking at a bit of software posted on the net and there was a function
>>looking like this (I hope I don't break any copyrights now...)

>>function realint (x: real): real;
>>begin
>>  realint := x - (x - trunc (x));
>>end;

<< stuf deleted >>

>>Well now the only difference between this function and putting "trunc (x)" 
>>directly in the code is that trunc returns an integer (I  think). In that
>>case -- is there any situation where you must have an real returned instead
>>of an integer and thus forced to use this function ?

>Suppose MAXINT is 2147whatever (about 2 billion), and x is -2.35e+3000 ?

In that case you would REALLY be in trouble since the code contains a
call trunc (x).

The only likely reason to use the above mentioned function that I can 
think of is as a parameter for a function that needs a "realint" value.
I that case you can't just use the standard trunc-function since the 
restriction on types are much stronger on parameters than on plain assignment.
In an assignment the types just has to be compatible (for the full 
description of type compatibility see the Users Manual And Report 
p. 159 (3. ed.))- in parameters they have BE the same type.

diamond@diamond.csl.sony.junet (Norman Diamond) (05/16/89)

In article <21930@santra.UUCP> s32935k@taltta.hut.fi (Carl Torsten Stenholm) wrote:

>>>function realint (x: real): real;
>>>begin
>>>  realint := x - (x - trunc (x));
>>>end;

>>>Well now the only difference between this function and putting "trunc (x)" 
>>>directly in the code is that trunc returns an integer (I think).

I replied:

>>Suppose MAXINT is 2147whatever (about 2 billion), and x is -2.35e+3000 ?
In article <4664@freja.diku.dk> damm@freja.diku.dk (Kristian Damm Jensen) writes:

>In that case you would REALLY be in trouble since the code contains a
>call trunc (x).

Oh yeah, sorry.

>I that case you can't just use the standard trunc-function since the 
>restriction on types are much stronger on parameters than on plain assignment.
>In an assignment the types just has to be compatible (for the full 
>description of type compatibility see the Users Manual And Report 
>p. 159 (3. ed.))- in parameters they have BE the same type.

In fact, this is not correct.  Realint takes a *value* parameter, and
trunc effectively does the same.  For a *value* parameter, the actual
must be assignment-compatible to the formal, since it really is only
a kind of assignment.  Full compatibility is only required for *var*
parameters.  Please read the standard again, shortly before or after
the section you cited.

There is a minor difference, since trunc *returns* an integer.  The
integer can be converted back to real automatically, while the result
of realint cannot be converted back to integer automatically (it
would need a call to -- you guessed it -- trunc).  However, the user
of realint really wants a real from it, right?  So she could just call
trunc instead of realint.  He doesn't need realint.

In the C group a while back, there were discussions of SLMs (silly
little macros).  I guess realint might be this group's first SLF.
Doubtless there will be more SLFs and SLPs to follow.

--
Norman Diamond, Sony Computer Science Lab (diamond%csl.sony.co.jp@relay.cs.net)
  The above opinions are my own.   |  Why are programmers criticized for
  If they're also your opinions,   |  re-implementing the wheel, when car
  you're infringing my copyright.  |  manufacturers are praised for it?