[comp.lang.smalltalk] subclasses of Float in Smalltalk/V

fchan@watvlsi.waterloo.edu (Francis Chan) (06/20/89)

In article <56225@linus.UUCP> sdl@linus.UUCP
(Steven D. Litvintchouk) writes:
>
>I currently own a copy of Smalltalk/V for the Mac.
>
>I am trying to create some subclasses of Float.  For example, subclass
>Degree, which would be constrained to have values in the range 0.0 ..
>360.0.  (For example, adding an instance of Degree to another instance
>of Degree would return the sum mod 360.0.)
>
>The problem is:  How can you create instances of class Degree?  I
>tried:
>
>	Degree fromInteger: 50
>
>But fromInteger is apparently a primitive which returns a Float.
>
>I also tried  
>	Degree new
>
>But Smalltalk/V considers the expression  (Float new) to be an error.
>
>I guess the problem is how to implement a conversion method for class
>Float called "asDegree," which will return an instance of class Degree
>equal to the Float number mod 360.0.  I see no way to accomplish this
>in Smalltalk/V.
>
>
>Any ideas?
>
>
>Steven Litvintchouk
>MITRE Corporation
>Burlington Road
>Bedford, MA  01730

I ran into a similar problem trying to create a subclass for Integer.
In the end I gave up doing trying to make the new class a subclass of
Integer simply because I couldn't readily modify the primitives that
handle the creation of new instances.

Finally, I made a new class under Number instead of under Integer that
used an instance variable to store the value. This is somewhat unsafe
since this value can be easily changed thus violating the concept of a
number (an object whose value is a constant). It's ugly but it works.

Once you have an instance variable it is fairly easy to get an
instance of Degree to show its value instead of `Degree' by
overriding the printOn: method. In this case:

printOn: aStream
  value printOn: aStream

will do the job where value is the instance variable holding the
degree value of the instance.

If anybody can tell me how Float or Integer class instances are stored,
modified, etc.. I would appreciate it a lot. This is using ST V/286 and
not the Mac version but perhaps a general treatise may be useful as
well.

Francis Chan
SWEN
University of Waterloo

trr@rayssd.ray.com (Terry R. Raymond) (06/23/89)

I tried to do it and this is what I found.  Apparently, the primitives
that perform the actual operations check the class of the object.  They
appear to test "is it a Float" not "is it kindof a Float" which would
permit subclasses.

This is what I did.

I created a class Degree as a variableByteSubclass of Float, just like
Float.  I created a class method "new:".

new: anInteger

^(Float fromInteger: anInteger) become: (self basicNew).

The method "become:" is in Behavior, it changes the class of the receiver
to the class of the argument.  The method "basicNew" is also in Behavior,
this method is never overridden by a subclass.  I then evaluated the 
following expressions.

(Degree new: 50) + 4.0      This produces a floating point exception error.

4.0 + (Degree new: 50)      This produces a stack overflow.

The stack over flow was produced because the "+" method trys to
convert the Degree instance to a float, i.e. it does not consider Degree
to be class Float.


I suggest that you look at the class Fraction and implement your class
in a similar way.
-- 
Terry Raymond
Raytheon Submarine Signal Division; Portsmouth RI; (401)-847-8000 x5597
------------------------------------------------------------------------------
Internet: trr@rayssd.ray.com    (trr%rayssd.ray.com@a.cs.uiuc.edu)
UUCP: {decuac,gatech,mimsy,mirror,necntc,sun,uiucdcs,ukma}!rayssd!trr

trr@rayssd.ray.com (Terry R. Raymond) (06/23/89)

I incorrectly stated that the "become:" method is in Behavior it is not,
it is in Object.
-- 
Terry Raymond
Raytheon Submarine Signal Division; Portsmouth RI; (401)-847-8000 x5597
------------------------------------------------------------------------------
Internet: trr@rayssd.ray.com    (trr%rayssd.ray.com@a.cs.uiuc.edu)
UUCP: {decuac,gatech,mimsy,mirror,necntc,sun,uiucdcs,ukma}!rayssd!trr

sdl@linus.UUCP (Steven D. Litvintchouk) (07/23/89)

I currently own a copy of Smalltalk/V for the Mac.

I am trying to create some subclasses of Float.  For example, subclass
Degree, which would be constrained to have values in the range 0.0 ..
360.0.  (For example, adding an instance of Degree to another instance
of Degree would return the sum mod 360.0.)

The problem is:  How can you create instances of class Degree?  I
tried:

	Degree fromInteger: 50

But fromInteger is apparently a primitive which returns a Float.

I also tried  
	Degree new

But Smalltalk/V considers the expression  (Float new) to be an error.

I guess the problem is how to implement a conversion method for class
Float called "asDegree," which will return an instance of class Degree
equal to the Float number mod 360.0.  I see no way to accomplish this
in Smalltalk/V.


Any ideas?


Steven Litvintchouk
MITRE Corporation
Burlington Road
Bedford, MA  01730

Fone:  (617)271-7753
ARPA:  sdl@mitre-bedford.arpa
UUCP:  ...{att,decvax,genrad,ll-xn,philabs,utzoo}!linus!sdl

	"Those who will be able to conquer software will be able to
	 conquer the world."  -- Tadahiro Sekimoto, president, NEC Corp.