A Glance at Numbers
Damien Cassou, Stéphane Ducasse and Luc Fabresse
http://stephane.ducasse.free.fr
Numbers
- SmallInteger, Integer,
- 4, 2r100 (4 in base 2), 3r11 (4 in base 3)
- Automatic coercion
1 + 2.3
-> 3.3
1 class
-> SmallInteger
1 class maxVal class
-> SmallInteger
(1 class maxVal + 1) class
-> LargePositiveInteger
- Fraction, Float
- 3/4, 2.4e7
(1/3) + (2/3)
-> 1
1000 factorial / 999 factorial
-> 1000
2/3 + 1
-> (5/3)
Numbers
Small Integers are Real Objects
- Small ints are real objects: instance of class
SmallInteger
- No need for boxing or unboxing
- Operations on small integers are plain messages (no exception to the rule)
Small integers are optimized
But small integers are heavily optimized
- Small integers encoded on 30 bits
- The pointer itself is the small integer
Automatic Coercion
What is the largest small integer?
What is the smallest large integer?
Fun With Large Numbers
factorial
is not optimized
Fraction
Fraction
denominator
, numerator
, ....
- Can handle large numbers
Messages Sent to Objects
- Operations on numbers are plain messages
- No exceptions
- Numbers are objects
- Mathematical operations are messages sent to objects
Message Limits
- There is no notion of precedence
- Use parenthesis to enforce precedence
Points
- A point has an x and y
- Points are created using the message
@
or x:y:
Alternative
Class written in a functional way: Most Point
methods are returning new points
Rectangles
- Geometric and expected operations
areasOutside:
, expandBy:
, insetBy:
, intersect:
Margins
- Delta to apply to a rectangle
- Useful for GUI
- Encodes 4 numbers, expressed in 3 different ways
- a number (same space all around)
- two numbers (same left/right and top/bottom)
- four numbers
- Used to compute the extended (or inner) rectangle
Summary
- Numbers are real objects
- Automatic coercion
- Number can be infinitely large
- Abstractions are built on top:
Point
, Rectangle
, Margin
/