Squeak Class Documentation category index | class index  
 
Float
  category: Kernel-Numbers
  superclass: Number
  subclasses:

My instances represent IEEE-754 floating-point double-precision numbers. They have about 16 digits of accuracy and their range is between plus and minus 10^307. Some valid examples are:

8.0 13.3 0.3 2.5e6 1.27e-30 1.27e-31 -12.987654e12

Mainly: no embedded blanks, little e for tens power, and a digit on both sides of the decimal point. It is actually possible to specify a radix for Squeak Float constants. This is great for teaching about numbers, but may be confusing to the average reader:

3r20.2 --> 6.66666666666667
8r20.2 --> 16.25

If you don't have access to the definition of IEEE-754, you can figure out what is going on by printing various simple values in Float hex. It may help you to know that the basic format is...
sign 1 bit
exponent 11 bits with bias of 1023 (16r3FF) to produce an exponent
in the range -1023 .. +1024
- 16r000:
significand = 0: Float zero
significand ~= 0: Denormalized number (exp = -1024, no hidden '1' bit)
- 16r7FF:
significand = 0: Infinity
significand ~= 0: Not A Number (NaN) representation
mantissa 53 bits, but only 52 are stored (20 in the first word, 32 in the second). This is because a normalized mantissa, by definition, has a 1 to the right of its floating point, and IEEE-754 omits this redundant bit to gain an extra bit of precision instead. People talk about the mantissa without its leading one as the FRACTION, and with its leading 1 as the SIGNFICAND.

The single-precision format is...
sign 1 bit
exponent 8 bits, with bias of 127, to represent -126 to +127
- 0x0 and 0xFF reserved for Float zero (mantissa is ignored)
- 16r7F reserved for Float underflow/overflow (mantissa is ignored)
mantissa 24 bits, but only 23 are stored
This format is used in FloatArray (qv), and much can be learned from the conversion routines, Float asIEEE32BitWord, and Float class fromIEEE32Bit:.

Thanks to Rich Harmon for asking many questions and to Tim Olson, Bruce Cohen, Rick Zaccone and others for the answers that I have collected here.

instance methods
  arithmetic
  *
+
-
/
abs
negated
reciprocal

  comparing
  <
<=
=
>
>=
closeTo:
hash
~=

  converting
  adaptToFraction:andSend:
adaptToInteger:andSend:
asApproximateFraction
asFloat
asFraction
asIEEE32BitWord
asTrueFraction
degreesToRadians
isInf
radiansToDegrees

  copying
  deepCopy
shallowCopy
veryDeepCopyWith:

  mathematical functions
  arcCos
arcSin
arcTan
arcTan:
cos
degreeCos
degreeSin
exp
floorLog:
ln
log
raisedTo:
reciprocalFloorLog:
reciprocalLogBase2
safeArcCos
sin
sqrt
tan
timesTwoPower:

  printing
  absByteEncode:base:
absPrintExactlyOn:base:
absPrintOn:base:
byteEncode:base:
hex
printOn:base:

  private
  absPrintOn:base:digitCount:

  testing
  hasContentsInExplorer
isFloat
isInfinite
isLiteral
isNaN
isPowerOfTwo
isZero
sign

  truncation and round off
  exponent
fractionPart
integerPart
reduce
rounded
significand
significandAsInteger
truncated

class methods
  class initialization
  initialize

  constants
  e
halfPi
infinity
nan
negativeZero
pi

  instance creation
  fromIEEE32Bit:
readFrom:

  plugin generation
  ccg:emitLoadFor:from:on:
ccg:generateCoerceToOopFrom:on:
ccg:generateCoerceToValueFrom:on:
ccg:prolog:expr:index:
ccgCanConvertFrom:
ccgDeclareCForVar:

instance methods
  arithmetic top  
 

*

Primitive. Answer the result of multiplying the receiver by aNumber.
Fail if the argument is not a Float. Essential. See Object documentation
whatIsAPrimitive.


 

+

Primitive. Answer the sum of the receiver and aNumber. Essential.
Fail if the argument is not a Float. See Object documentation
whatIsAPrimitive.


 

-

Primitive. Answer the difference between the receiver and aNumber.
Fail if the argument is not a Float. Essential. See Object documentation
whatIsAPrimitive.


 

/

Primitive. Answer the result of dividing receiver by aNumber.
Fail if the argument is not a Float. Essential. See Object documentation
whatIsAPrimitive.


 

abs

This is faster than using Number abs.


 

negated

Answer a Number that is the negation of the receiver.


 

reciprocal

Answer 1 divided by the receiver. Create an error notification if the
receiver is 0.


  comparing top  
 

<

Primitive. Compare the receiver with the argument and return true
if the receiver is less than the argument. Otherwise return false.
Fail if the argument is not a Float. Essential. See Object documentation
whatIsAPrimitive.


 

<=

Primitive. Compare the receiver with the argument and return true
if the receiver is less than or equal to the argument. Otherwise return
false. Fail if the argument is not a Float. Optional. See Object
documentation whatIsAPrimitive.


 

=

Primitive. Compare the receiver with the argument and return true
if the receiver is equal to the argument. Otherwise return false.
Fail if the argument is not a Float. Essential. See Object documentation
whatIsAPrimitive.


 

>

Primitive. Compare the receiver with the argument and return true
if the receiver is greater than the argument. Otherwise return false.
Fail if the argument is not a Float. Essential. See Object documentation
whatIsAPrimitive.


 

>=

Primitive. Compare the receiver with the argument and return true
if the receiver is greater than or equal to the argument. Otherwise return
false. Fail if the argument is not a Float. Optional. See Object documentation
whatIsAPrimitive.


 

closeTo:

are these two numbers close?


 

hash

Hash is reimplemented because = is implemented. Both words of the float are used; 8 bits are removed from each end to clear most of the exponent regardless of the byte ordering. (The bitAnd:'s ensure that the intermediate results do not become a large integer.) Slower than the original version in the ratios 12:5 to 2:1 depending on values. (DNS, 11 May, 1997)


 

~=

Primitive. Compare the receiver with the argument and return true
if the receiver is not equal to the argument. Otherwise return false.
Fail if the argument is not a Float. Optional. See Object documentation
whatIsAPrimitive.


  converting top  
 

adaptToFraction:andSend:

If I am involved in arithmetic with a Fraction, convert it to a Float.


 

adaptToInteger:andSend:

If I am involved in arithmetic with an Integer, convert it to a Float.


 

asApproximateFraction

Answer a Fraction approximating the receiver. This conversion uses the
continued fraction method to approximate a floating point number.


 

asFloat

Answer the receiver itself.


 

asFraction


 

asIEEE32BitWord

Convert the receiver into a 32 bit Integer value representing the same number in IEEE 32 bit format. Used for conversion in FloatArrays only.


 

asTrueFraction

Answer a fraction that EXACTLY represents self,
a double precision IEEE floating point number.
Floats are stored in the same form on all platforms.
(Does not handle gradual underflow or NANs.)
By David N. Smith with significant performance
improvements by Luciano Esteban Notarfrancesco.
(Version of 11April97)


 

degreesToRadians

Answer the receiver in radians. Assumes the receiver is in degrees.


 

isInf

simple, byte-order independent test for +/- Infinity


 

radiansToDegrees

Answer the receiver in degrees. Assumes the receiver is in radians.


  copying top  
 

deepCopy

Answer a copy of the receiver with its own copy of each instance
variable.


 

shallowCopy

Answer a copy of the receiver which shares the receiver's instance variables.


 

veryDeepCopyWith:

Return self. Do not record me.


  mathematical functions top  
 

arcCos

Answer the angle in radians.


 

arcSin

Answer the angle in radians.


 

arcTan

Answer the angle in radians.
Optional. See Object documentation whatIsAPrimitive.


 

arcTan:

Answer the angle in radians.
Optional. See Object documentation whatIsAPrimitive.


 

cos

Answer the cosine of the receiver taken as an angle in radians.


 

degreeCos

Answer the cosine of the receiver taken as an angle in degrees.


 

degreeSin

Answer the sine of the receiver taken as an angle in degrees.


 

exp

Answer E raised to the receiver power.
Optional. See Object documentation whatIsAPrimitive.


 

floorLog:

Answer the floor of the log base radix of the receiver.


 

ln

Answer the natural logarithm of the receiver.
Optional. See Object documentation whatIsAPrimitive.


 

log

Answer the base 10 logarithm of the receiver.


 

raisedTo:

Answer the receiver raised to aNumber.


 

reciprocalFloorLog:

Quick computation of (self log: radix) floor, when self < 1.0.
Avoids infinite recursion problems with denormalized numbers


 

reciprocalLogBase2

optimized for self = 10, for use in conversion for printing


 

safeArcCos

Answer the angle in radians.


 

sin

Answer the sine of the receiver taken as an angle in radians.
Optional. See Object documentation whatIsAPrimitive.


 

sqrt

Answer the square root of the receiver.
Optional. See Object documentation whatIsAPrimitive.


 

tan

Answer the tangent of the receiver taken as an angle in radians.


 

timesTwoPower:

Primitive. Answer with the receiver multiplied by 2.0 raised
to the power of the argument.
Optional. See Object documentation whatIsAPrimitive.


  printing top  
 

absByteEncode:base:

Print my value on a stream in the given base. Assumes that my value is strictly
positive; negative numbers, zero, and NaNs have already been handled elsewhere.
Based upon the algorithm outlined in:
Robert G. Burger and R. Kent Dybvig
Printing Floating Point Numbers Quickly and Accurately
ACM SIGPLAN 1996 Conference on Programming Language Design and Implementation
June 1996.
This version performs all calculations with Floats instead of LargeIntegers, and loses
about 3 lsbs of accuracy compared to an exact conversion.


 

absPrintExactlyOn:base:

Print my value on a stream in the given base. Assumes that my value is strictly
positive; negative numbers, zero, and NaNs have already been handled elsewhere.
Based upon the algorithm outlined in:
Robert G. Burger and R. Kent Dybvig
Printing Floating Point Numbers Quickly and Accurately
ACM SIGPLAN 1996 Conference on Programming Language Design and Implementation
June 1996.
This version guarantees that the printed representation exactly represents my value
by using exact integer arithmetic.


 

absPrintOn:base:

Print my value on a stream in the given base. Assumes that my value is strictly
positive; negative numbers, zero, and NaNs have already been handled elsewhere.
Based upon the algorithm outlined in:
Robert G. Burger and R. Kent Dybvig
Printing Floating Point Numbers Quickly and Accurately
ACM SIGPLAN 1996 Conference on Programming Language Design and Implementation
June 1996.
This version performs all calculations with Floats instead of LargeIntegers, and loses
about 3 lsbs of accuracy compared to an exact conversion.


 

byteEncode:base:

Handle sign, zero, and NaNs; all other values passed to absPrintOn:base:


 

hex

If ya really want to know...


 

printOn:base:

Handle sign, zero, and NaNs; all other values passed to absPrintOn:base:


  private top  
 

absPrintOn:base:digitCount:

Print me in the given base, using digitCount significant figures.


  testing top  
 

hasContentsInExplorer


 

isFloat

Overridden to return true in Float, natch


 

isInfinite

Return true if the receiver is positive or negative infinity.


 

isLiteral

Answer whether the receiver has a literal text form recognized by the
compiler.


 

isNaN

simple, byte-order independent test for Not-a-Number


 

isPowerOfTwo

Return true if the receiver is an integral power of two.
Floats never return true here.


 

isZero


 

sign

Answer 1 if the receiver is greater than 0, -1 if less than 0, else 0.
Handle IEEE-754 negative-zero by reporting a sign of -1


  truncation and round off top  
 

exponent

Primitive. Consider the receiver to be represented as a power of two
multiplied by a mantissa (between one and two). Answer with the
SmallInteger to whose power two is raised. Optional. See Object
documentation whatIsAPrimitive.


 

fractionPart

Primitive. Answer a Float whose value is the difference between the
receiver and the receiver's asInteger value. Optional. See Object
documentation whatIsAPrimitive.


 

integerPart

Answer a Float whose value is the receiver's truncated value.


 

reduce

If self is close to an integer, return that integer


 

rounded

Answer the integer nearest the receiver.


 

significand


 

significandAsInteger


 

truncated

Answer with a SmallInteger equal to the value of the receiver without
its fractional part. The primitive fails if the truncated value cannot be
represented as a SmallInteger. In that case, the code below will compute
a LargeInteger truncated value.
Essential. See Object documentation whatIsAPrimitive.


class methods
  class initialization top  
 

initialize

Float initialize


  constants top  
 

e

Answer the constant, E.


 

halfPi


 

infinity

Answer the value used to represent an infinite magnitude


 

nan

Answer the canonical value used to represent Not-A-Number


 

negativeZero


 

pi

Answer the constant, Pi.


  instance creation top  
 

fromIEEE32Bit:

Convert the given 32 bit word (which is supposed to be a positive 32bit value) from a 32bit IEEE floating point representation into an actual Squeak float object (being 64bit wide). Should only be used for conversion in FloatArrays or likewise objects.


 

readFrom:

Answer a new Float as described on the stream, aStream.


  plugin generation top  
 

ccg:emitLoadFor:from:on:


 

ccg:generateCoerceToOopFrom:on:


 

ccg:generateCoerceToValueFrom:on:


 

ccg:prolog:expr:index:


 

ccgCanConvertFrom:


 

ccgDeclareCForVar: