[comp.sys.mac.programmer] Converting COMP to EXTENDED to STRING

pascal@altitude.CAM.ORG (Pascal Gosselin) (07/23/90)

Hi,

    I am presently trying to write a FUNCTION in MPW Pascal 3.1 that will
input a 'real' of type COMP or INTEGER and output the equivalent value
into a STRING.

I've got it set up something like this at the moment:

FUNCTION Number2Str(var ExtendedNumber:Extended):DecStr;

Var
   TheNumber:DecStr;

Begin
     DecimalFormat.Style:=FixedDecimal;
     DecimalFormat.Digits:=2;
     Num2Str(DecimalFormat,ExtendedNumber,TheNumber);
     Number2Str:=TheNumber;
End;


Now, everytime that I attempt to call this function with any REAL type
(integer,comp,etc...), I get an error 125 'Invalid parameter passed to
function' (I forget the exact wording) (this is during compilation).

The chapter on SANE in the MPW Pascal manual states that "any function
with a parameter declared as Extended may input any other real data
type" (again, approximate wording, I don't have the manual next to me
right now).

I can get the function to work if I make it specific like:
FUNCTION Number2Str(var CompNumber:COMP):Decstr;  etc.....

But that defeats the point, I want to make the routine generic.

Why is it that SANE has built-on functions to convert almost anything
EXCEPT from COMP to EXTENDED (or am I missing something???) ?

I'm really stuck on this one, any info would be appreciated.


-- 
+--------------------------------------------------------------------+
| Pascal Gosselin          |    Internet: pascal@altitude.CAM.ORG    |
| Gest-Mac Inc. Apple VAR  |    (514) 939-1127     CIS: 72757,1570   |
+--------------------------------------------------------------------+

aries@rhi.hi.is (Reynir Hugason) (07/23/90)

>FUNCTION Number2Str(var ExtendedNumber:Extended):DecStr;
                     ^^^

Your headache lies in the VAR part of your function declaration. The
Pascal compiler can easily transform an INTEGER or a COMP to an
Extended, but transforming an EXTENDED to an INTEGER is a bit harder
than that. If that's what you want to do you'll have to use SANE's
Num2Integer or Num2Comp.

---
Mimir (aries@rhi.hi.is) - Aries, Inc.

pascal@altitude.CAM.ORG (Pascal Gosselin) (07/24/90)

aries@rhi.hi.is (Reynir Hugason) writes:


>>FUNCTION Number2Str(var ExtendedNumber:Extended):DecStr;
>                     ^^^

>Your headache lies in the VAR part of your function declaration. The
>Pascal compiler can easily transform an INTEGER or a COMP to an
>Extended, but transforming an EXTENDED to an INTEGER is a bit harder
>than that. If that's what you want to do you'll have to use SANE's
>Num2Integer or Num2Comp.

>---
>Mimir (aries@rhi.hi.is) - Aries, Inc.

No, I am trying to convert COMP to EXTENDED and INTEGER TO EXTENDED.

I then take the EXTENDED and convert it into a STRING.  I have the function
working right now, but in the following formats....

FUNCTION Comp2Str(var CompNumber:Comp):DecStr;
....  add code here for conversion... etc....

and to covert from Integer to the string....

FUNCTION Integer2Str(var IntegerNumber:Integer):DecStr;
...  again, code to do the conversion....


I find it bothersome to have to code a function for EVERY type of
REAL (integer,real,longint,comp,extended) since the MPW Pascal 3.0 manual
states the following:

Page 326, Appendix G The SANE Library

"Remember that any function with a formal parameter of any real types
can be passed a value of any realinteger type. Floating-point value
parameters in MPW3.0 Pascal will accept expressions and variables of
the following types: Integer,longit,real(single),double,comp or extended.
We abbreviate the list with the term numeric argument in the explantory
text below."


Now, I figure that this means that you can pass just about anything
to a function of the form FUNCTION Convert(var x:extended):real; ... Right?

I AM returning a string from the function, but I can't see why the compiler
would complain of an "Invalid parameter substitution" whenever my
program passes an Integer or Comp value to it....

Still searching for an answer....


-- 
+--------------------------------------------------------------------+
| Pascal Gosselin          |    Internet: pascal@altitude.CAM.ORG    |
| Gest-Mac Inc. Apple VAR  |    (514) 939-1127     CIS: 72757,1570   |
+--------------------------------------------------------------------+

kaufman@Neon.Stanford.EDU (Marc T. Kaufman) (07/24/90)

In article <1990Jul23.221417.24424@altitude.CAM.ORG> pascal@altitude.CAM.ORG (Pascal Gosselin) writes:
>aries@rhi.hi.is (Reynir Hugason) writes:

-->FUNCTION Number2Str(var ExtendedNumber:Extended):DecStr;

->Your headache lies in the VAR part of your function declaration. The
->Pascal compiler can easily transform an INTEGER or a COMP to an
->Extended, but transforming an EXTENDED to an INTEGER is a bit harder
->than that. If that's what you want to do you'll have to use SANE's
->Num2Integer or Num2Comp.

>No, I am trying to convert COMP to EXTENDED and INTEGER TO EXTENDED.

The problem is that the "VAR" part means that the *ADDRESS* of the Extended
is passed to the Function, so that it may be modified as a result.  If this
is not what you intend, and I don't think it is, try a prototype like:

   FUNCTION Number2Str(ExtendedNumber:Extended):DecStr;

Not the lack of "var".  In this case the parameter is passed by *VALUE*, and
the compiler doesn't have to worry about how to store an Extended into an
integer parameter passed by address.

Marc Kaufman (kaufman@Neon.stanford.edu)