|
Class: Float
Object
|
+--Magnitude
|
+--ArithmeticValue
|
+--Number
|
+--LimitedPrecisionReal
|
+--Float
- Package:
- stx:libbasic
- Category:
- Magnitude-Numbers
- Version:
- rev:
1.458
date: 2023/09/12 12:59:29
- user: cg
- file: Float.st directory: libbasic
- module: stx stc-classLibrary: libbasic
Floats represent rational numbers with limited precision.
They use the C-compiler's 'double' format,
which is usually the 8byte IEEE double float format.
Floats give you 64 bit floats.
In contrast to ShortFloats (32bit) and LongFloats (>=64bit).
WARNING:
The layout of Float instances is known by the runtime system and the compiler;
you may not add instance variables here.
Also, subclassing is complicated by the fact, that the VM creates floats/shortFloats,
and does some float-checks by an identity compare with the Float-class.
(i.e. your subclasses instances may not be recognized as float-like objects,
thus mixed mode arithmetic will always coerce them, effectively slowing things down).
Notice, that Floats are defined as Byte-array to prevent the garbage collector
from going into the value ... otherwise I needed a special case in many places.
Also notice, that ST/X Floats are what Doubles are in ST-80. The reason for doing this
was to be compatible to both Digitalk, Squeak AND ParcPlace smalltalk implementations
(ParcPlace uses a 4-byte Float and an 8-byte Double class, in contrast to
Digitalk and Squeak, which have an 8-byte Float class).
Thus, by providing an 8-byte Float class, code would not loose precicion
(although some memory is wasted when porting from VW).
Notice that ST/X provides an alias called Double, and an extra ShortFloat class, which has 4-byte
instances.
Mixed mode arithmetic:
float op float -> float
float op fix -> float
float op fraction -> float
float op integer -> float
float op shortFloat -> float
float op longFloat -> longFloat
float op complex -> complex
Representation:
64bit double precision IEEE floats
52 bit mantissa + 1 hidden bit providing 53 bits of precision,
11 bit exponent,
15 decimal digits (approx.)
Range and Precision of Storage Formats: see LimitedPrecisionReal >> documentation
[aliases:]
FloatD (ANSI)
Float64
copyrightCOPYRIGHT (c) 1988 by Claus Gittinger
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
errorHandlingFloating point error handling and signalling depends on the system's
(actually: the C-runtime systems) ability to handle floating point errors.
Most systems report errors by raising an OS floatingPoint exception,
which is mapped to a fpExceptionInterrupt in ST/X.
However, some systems do return NaN as result.
Currently, ST/X does not care specially for these systems - it maybe added
easily, if there is sufficient customer interest, though.
Try:
|f1 f2|
f1 := 1.0.
f2 := 0.0.
f1 / f2
or:
2 arcSin
accessing
-
defaultPrintFormat
-
'.15' by default, I will print up to 15 digits
-
defaultPrintFormat: aFormatString
-
the formatString must be of the form '.n'.
-
defaultPrintPrecision
-
the default number of digits when printing
-
defaultPrintfPrecision
-
the default number of digits when printing with printf's %f format.
Notice, that the C-language standard states that this should be 6;
however, we can adjust it on a per-class basis.
binary storage
-
readBinaryIEEEDoubleFrom: aStream
-
read a float from the binary stream, aStream,
interpreting the next bytes as an IEEE formatted 8-byte float.
The bytes are read in the native byte order (i.e.lsb on intel)
-
readBinaryIEEEDoubleFrom: aStream MSB: msbFirst
-
read a float from the binary stream, aStream,
interpreting the next bytes as an IEEE formatted 8-byte float.
The bytes are read in the specified byte order
-
readBinaryIEEEDoubleFrom: aStream into: aBasicNewFloat
-
read the receiver's value from the binary stream, aStream,
interpreting the next bytes as an IEEE formatted 8-byte float.
The bytes are read in the native byte order (i.e.lsb on intel)
-
readBinaryIEEEDoubleFrom: aStream into: aFloat MSB: msb
-
read the receiver's value from the binary stream, aStream,
interpreting the next bytes as an IEEE formatted 8-byte float.
If msb is true, the stream bytes are most-significant-first.
-
storeBinaryIEEEDouble: aFloat on: aStream
-
store aFloat as an IEEE formatted 8-byte float
onto the binary stream, aStream.
The bytes are written in the native byte order (i.e.lsb on intel)
-
storeBinaryIEEEDouble: aFloat on: aStream MSB: msb
-
store aFloat as an IEEE formatted 8-byte float
onto the binary stream, aStream.
If msb is true, the stream bytes are written most-significant-first.
class initialization
-
initialize
-
print up to 15 valid digits
Usage example(s):
Pi := nil.
Epsilon := nil.
self initialize
|
coercing & converting
-
coerce: aNumber
-
convert the argument aNumber into an instance of the receiver's class and return it.
-
generality
-
return the generality value - see ArithmeticValue>>retry:coercing:
constants
-
NaN
-
return the constant NaN (not a Number)
Usage example(s):
NaN := nil
Float NaN
Float NaN negated
Float NaN + 0.0
Float NaN + Float NaN
0.0 + Float NaN
it's a singleton:
Float NaN == Float NaN
|
-
e
-
return the constant e as Float
Usage example(s):
don't expect this many valid digits on all machines;
|
Usage example(s):
The actual precision is very CPU specific.
|
-
halfPi
-
return the constant pi/2 as Float
-
halfPiNegative
-
return the constant -pi/2 as Float
-
infinity
-
return the constant infinity
Usage example(s):
PositiveInfinity := nil
Float infinity
Float infinity negated
Float infinity + 0.0
Float infinity + Float NaN
0.0 + Float infinity
Float NaN + Float infinity
it's a singleton:
Float infinity == Float infinity
|
-
ln10
-
return ln(10), the natural logarithm of 10;
will return something like 2.30258509299405
Usage example(s):
Float ln10 -> 2.30258509299405
ShortFloat ln10 -> 2.302585
QDouble ln10 -> 2.302585093
|
-
ln2
-
return ln(2) the natural logarithm of 2
-
maxSmallInteger
-
return the max. smallInteger value as a float.
Notice: on a 32bit machine, the full SmallInteger range can be represented
as a float, whereas on a 64 bit machine, you'll loose some precision,
because floats only have 53 bits of precision.
-
negativeInfinity
-
return a float which represents positive infinity (for my instances).
Warning: do not compare equal against infinities;
instead, check using isFinite or isInfinite
Usage example(s):
NegativeInfinity := nil.
Float negativeInfinity == Float negativeInfinity
|
-
negativeZero
-
notice that IEEE format does have a negative zero
-
phi
-
return the constant phi as Float
-
pi
-
return the constant pi as Float
Usage example(s):
Float pi -> 3.14159265358979
ShortFloat pi -> 3.141593
LongFloat pi -> 3.141592653589793238
QDouble pi -> 3.1415926535897932384626433832795028841971693993751058209749446
QuadFloat pi -> 3.1415926535897932384626433832795027
OctaFloat pi -> 3.14159265358979323846264338327950288419716939937510582097494459230781639
LargeFloat pi -> 3.1415926535897932384626433832795028841971693993751058209749445923078164
|
-
piNegative
-
return the constant -pi as Float
Usage example(s):
Float pi -> 3.14159265358979
Float piNegative -> -3.14159265358979
|
-
sqrt2
-
don't expect this many valid digits on all machines;
-
sqrt3
-
don't expect this many valid digits on all machines;
-
sqrt5
-
don't expect this many valid digits on all machines;
-
twoPi
-
return the constant 2*pi as Float
-
twoPiNegative
-
return the constant 2*pi as Float
-
unity
-
return the neutral element for multiplication (1.0) as Float
-
zero
-
return the neutral element for addition (0.0) as Float
instance creation
-
basicNew
-
return a new float - here we return 0.0
- floats are usually NOT created this way ...
Its implemented here to allow things like binary store & load
of floats. (but even this support will go away eventually, its not
a good idea to store the bits of a float - the reader might have a
totally different representation - so floats will eventually be
binary stored in a device independent format.
-
basicNew: size
-
(comment from inherited method)
return an instance of myself with anInteger indexed variables.
If the receiver-class has no indexed instvars, this is only allowed
if the argument, anInteger is zero.
** Do not redefine this method in any class **
-
fastFromString: aString at: startIndex
-
return the next Float from the string starting at startIndex.
No spaces are skipped.
Raises an exception, if the startIndex is not valid.
Returns garbage if the argument string is not a valid double float number.
This is a specially tuned entry (using a low-level C-call to atof).
It has been added to allow high speed string decomposition
into numbers, especially for mass-data (reading millions of floats).
This is roughly 5 times faster than the full-blown inherited fromString:
Usage example(s):
Float fastFromString:'-123.45' at:1
Float fastFromString:'123.45' at:1
Float fastFromString:'123.45' at:2
Float fastFromString:'123.45' at:3
Float fastFromString:'123.45' at:4
Float fastFromString:'123.45' at:5
Float fastFromString:'123.45' at:6
Float fastFromString:'123.45E4' at:1
Float fastFromString:'hello123.45E4' at:6
Float fastFromString:'12345' at:1
Float fastFromString:'12345' at:2
Float fastFromString:'12345' at:3
Float fastFromString:'12345' at:4
Float fastFromString:'12345' at:5
Float fastFromString:'12345' at:6 -> error
Float fastFromString:'12345' at:0 -> error
Float fastFromString:'hello123.45E4' at:1 -> 0
Float fastFromString:'1.7976931348623157e+308'
Float fastFromString:'nan'
Float fastFromString:'Nan'
Float fastFromString:'Inf'
Float fastFromString:'+Inf'
Float fastFromString:'-Inf'
|
Usage example(s):
(Time toRun:[
1000000 timesRepeat:[
Float fastFromString:'123.45' at:1
]
]) / 1000000 -> 164ns
|
-
fromIEEE32Bit: anInteger
-
creates a double, given the four native float bytes as an integer
Usage example(s):
ShortFloat fromIEEE32Bit:((ShortFloat pi digitBytesMSB:true) asIntegerMSB:true)
|
-
fromIEEE64Bit: anInteger
-
creates a double, given the eight native double bytes as an integer
Usage example(s):
Float fromIEEE64Bit:(#[16r0 16r0 16r0 16r0 16r0 16r0 16rF0 16r3F] asIntegerMSB:false).
Float fromIEEE64Bit:((Float pi digitBytesMSB:true) asIntegerMSB:true)
Float fromIEEE64Bit:((0.0 digitBytesMSB:true) asIntegerMSB:true)
Float fromIEEE64Bit:0
Float fromIEEE64Bit:1
Float fromIEEE64Bit:2
Float fromIEEE64Bit:((Float NaN digitBytesMSB:true) asIntegerMSB:true)
Float fromIEEE64Bit:((Float NaN negated digitBytesMSB:true) asIntegerMSB:true)
Float fromIEEE64Bit:((Float infinity digitBytesMSB:true) asIntegerMSB:true)
Float fromIEEE64Bit:((Float infinity negated digitBytesMSB:true) asIntegerMSB:true)
|
-
fromInteger: anInteger
-
return a new float, given an integer value
Usage example(s):
Float fromInteger:123 -> 123.0
Float fromInteger:(100 factorial) -> 9.33262154439442e+157
(100 factorial) asFloat -> 9.33262154439442e+157 -- floats are inexact
100 factorial - ((100 factorial) asFloat asInteger) -- floats are inexact
|
-
fromNumber: aNumber
-
return aNumber coerced to Float
-
fromVAXFloatBytes: b1 b2: b2 b3: b3 b4: b4
-
creates a double, given the four vax F-format float bytes to an ieee double.
For NaNs and Infinity, nil is returned.
To get an idea of how big the floating-point zoo really is,
see: http://www.quadibloc.com/comp/cp0201.htm
misc
-
getFPUControl
-
BORLAND only: get the fpu control word.
Usage example(s):
-
getRoundingMode
-
get the rounding mode;
Notice: currently, this is a static settings, which is bad.
in the future (the next VM version), this will be a thread local setting
Usage example(s):
-
setFPUControl
-
BORLAND only: set the fpu control word for 64 bit precision for long doubles here
Usage example(s):
-
setRoundingMode: modeSymbolOrInteger
-
set the rounding mode;
Notice: currently, this is a static settings, which is bad.
in the future (the next VM version), this will be a thread local setting
Usage example(s):
self getRoundingMode
self setRoundingMode:#TONEAREST
self getRoundingMode
|
queries
-
_fmax
-
Float _fmax
-
_fmin
-
the smallest non-zero value which can be represented by instances of this class;
should actually be sent to the instance,
because of IEEEFloat, which has instance-specific representation
Usage example(s):
-
eBias
-
Answer the exponent's bias;
that is the offset of the zero exponent when stored
Usage example(s):
1<<(1.0 numBitsInExponent-1)-1
1.0 numBitsInExponent 11
1.0 eBias 1023
1.0 emin -1022
1.0 emax 1023
1.0 fmin 2.2250738585072E-308
1.0 fmax 1.79769313486232E+308
1.0 asShortFloat numBitsInExponent 8
1.0 asShortFloat eBias 127
1.0 asShortFloat emin -126
1.0 asShortFloat emax 127
1.0 asShortFloat fmin 1.175494e-38
1.0 asShortFloat fmax 3.402823e+38
1.0 asLongFloat numBitsInExponent 15
1.0 asLongFloat eBias 16383
1.0 asLongFloat emin -16382
1.0 asLongFloat emax 16383
1.0 asLongFloat fmin 3.362103143112093506E-4932
1.0 asLongFloat fmax 1.189731495357231765E+4932
|
-
emax
-
The largest exponent value allowed by instances of this class.
Usage example(s):
1.0 fmax -> 1.79769313486232E+308
1.0 fmin -> 2.2250738585072E-308
1.0 emin -> -1022
1.0 emax -> 1023
|
-
emin
-
The smallest exponent value allowed by (normalized) instances of this class.
Usage example(s):
1.0 fmax -> 1.79769313486232E+308
1.0 fmin -> 2.2250738585072E-308
1.0 emin -> -1022
1.0 emax -> 1023
1.0 asIEEEFloat emin -> -1022
1.0 asIEEEFloat emax -> 1023
1.0 asIEEEFloat fmax -> 1.79769313486232E+308
1.0 fmin
|
-
epsilon
-
return the maximum relative spacing of instances of mySelf
(i.e. the value-delta of the least significant bit of a 1.0).
according to ISO C standard;
Ada, C, C++ and Python language constants;
Mathematica, MATLAB and Octave; and various textbooks
see https://en.wikipedia.org/wiki/Machine_epsilon
Usage example(s):
(1.0 nextFloat - 1.0) -> 2.22044604925031e-16
ShortFloat epsilon -> 1.192093e-07
Float epsilon -> 2.22044604925031E-16
LongFloat epsilon -> 1.084202172485504434E-19
QuadFloat epsilon -> 1.92592994e-34
OctaFloat epsilon -> 9.0556790788e-72
QDouble epsilon -> 7.7787690973e-62
LargeFloat epsilon -> 1.24460305557223e-60
(1.0 asLargeFloatPrecision:200) epsilon -> 1.24460305557223e-60
(1.0 asLargeFloatPrecision:1000) epsilon -> 1.86652723700644e-301
|
-
exponentCharacter
-
return the character used to print between mantissa an exponent.
Also used by the scanner when reading numbers.
-
fmax
-
the largest finite value which can be represented by instances of this class;
should actually be sent to the instance,
because of IEEEFloat, which has instance-specific representation
Usage example(s):
-
fmaxDenormalized
-
the largest denormalized value which can be represented
by instances of this class.
Should actually be sent to the instance,
because of IEEEFloat, which has instance-specific representation
Usage example(s):
Float fmin -> 2.2250738585072e-308
Float fminDenormalized -> 4.94065645841247e-324
Float fmaxDenormalized -> 2.2250738585072e-308
-- actually Float fminDenormalized above that
-- but printed with rounding on the last bit
|
-
fmin
-
the smallest normalized non-zero value which can be represented by
instances of this class;
should actually be sent to the instance,
because of IEEEFloat, which has instance-specific representation
Usage example(s):
Float fmin -> 2.2250738585072e-308
Float fminDenormalized -> 4.94065645841247e-324
|
-
fminDenormalized
-
the smallest non-zero value which can be represented
by instances of this class;
should actually be sent to the instance,
because of IEEEFloat, which has instance-specific representation
Usage example(s):
Float fmin -> 2.2250738585072e-308
Float fminDenormalized -> 4.94065645841247e-324
|
-
isBuiltInClass
-
return true if this class is known by the run-time-system.
Here, true is returned for myself, false for subclasses.
-
numBitsInExponent
-
answer the number of bits in the exponent
This is an IEEE float, where 11 bits are available:
seeeeeee eeeemmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm
Usage example(s):
-
numBitsInMantissa
-
answer the number of bits in the mantissa (the significant).
The hidden bit is not counted here.
This is an IEEE double (binary64),
where 52 bits are available:
seeeeeee eeeemmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm
Usage example(s):
1.0 numBitsInExponent 11
1.0 numBitsInMantissa 52
1.0 precision 53
1.0 decimalPrecision 16
1.0 eBias 1023
1.0 emin -1022
1.0 emax 1023
1.0 fmin 2.2250738585072E-308
1.0 fmax 1.79769313486232E+308
|
-
precision
-
answer the precision (the number of bits in the mantissa) of a Float (in bits)
This is an IEEE double, where only the fraction from the normalized mantissa is stored
and so there is a hidden bit and the mantissa is actually represented
by 53 binary digits (although only 52 are needed in the binary representation)
the hidden bit is included here
Usage example(s):
self numBitsInMantissa + 1
self precision
|
-
radix
-
answer the radix of a Float's exponent
This is an IEEE float, which is represented as binary
arithmetic
-
* aNumber
-
return the product of the receiver and the argument.
-
+ aNumber
-
return the sum of the receiver and the argument, aNumber
-
- aNumber
-
return the difference of the receiver and the argument, aNumber
-
/ aNumber
-
return the quotient of the receiver and the argument, aNumber
-
abs
-
return the absolute value of the receiver
reimplemented here for speed
Usage example(s):
-
negated
-
return the receiver negated
Usage example(s):
0 negated
0.0 negated
0.0 asShortFloat negated
0.0 asLongFloat negated
0.0 asQDouble negated
|
-
quotientFromFloat: aFloat
-
return the quotient of aFloat and the receiver
-
rem: aNumber
-
return the floating point remainder of the receiver and the argument, aNumber
Usage example(s):
5.0 rem:2 -> 1.0
5.0 rem:2.0 -> 1.0
5.0 rem:(2.0 asLongFloat) -> 1.0
5.0 rem:(2.0 asShortFloat) -> 1.0
5.0 rem:(3/2) -> 0.5
-5.0 rem:12 -> -5.0
-5.0 quo:12 -> 0
-5.0 \\ 12 -> 7.0
-5.0 // 12 -> -1
|
-
uncheckedDivide: aNumber
-
return the quotient of the receiver and the argument, aNumber.
Do not check for divide by zero (return NaN or Infinity).
This operation is provided for emulators of other languages/semantics,
where no exception is raised for these results (i.e. Java).
It is only defined if the argument's type is the same as the receiver's.
Usage example(s):
0.0 uncheckedDivide:0.0
1.0 uncheckedDivide:0.0
-1.0 uncheckedDivide:0.0
|
coercing & converting
-
asDouble
-
ST80 compatibility: return a double with the receiver's value.
In ST/X, floats are the equivalent to ST80 doubles
-
asFloat
-
return a Float with same value - that's me
-
asInteger
-
return an integer with same value - might truncate
Usage example(s):
12345.0 asInteger
1e15 asInteger
SmallInteger maxVal 4611686018427387903
SmallInteger maxVal asFloat 4.61168601842739e+18
SmallInteger maxVal asFloat asInteger 4611686018427387904
SmallInteger maxVal asFloat perform:#asInteger 4611686018427387904
Float NaN asInteger
Float infinity asInteger
Float infinity negated asInteger
|
-
asLongFloat
-
return a LongFloat with same value as receiver
Usage example(s):
123 asFloat asLongFloat 123.0
123.0 asLongFloat 123.0
0.1 asLongFloat 0.1000000000000000056
0.3 asLongFloat 0.2999999999999999889
0.3 asLongFloat roundTo:0.0000001
0.25 asLongFloat 0.25
|
-
asOctaFloat
( an extension from the stx:libbasic2 package )
-
return an octaFloat with same value as receiver
Usage example(s):
123 asFloat asOctaFloat
0 asFloat asOctaFloat
0.0 asOctaFloat
|
-
asQDouble
( an extension from the stx:libbasic2 package )
-
return a QDouble with my value
Usage example(s):
-
asQuadFloat
( an extension from the stx:libbasic2 package )
-
return a quadFloat with same value as receiver
Usage example(s):
123 asFloat asQuadFloat
0 asFloat asQuadFloat
0.0 asQuadFloat
|
-
asShortFloat
-
return a ShortFloat with same value as receiver.
Does NOT raise an error if the receiver exceeds the float range.
Usage example(s):
1e300 asShortFloatChecked -> infiniteResultError
-1e300 asShortFloatChecked -> infiniteResultError
1e300 asShortFloat -> INF
-1e300 asShortFloat -> -INF
1e-300 asShortFloat -> 0.0
-1e-300 asShortFloat -> -0.0
|
-
asShortFloatChecked
-
return a ShortFloat with same value as receiver.
Raises an error if the receiver exceeds the float range or is non-finite.
If the error is proceeded, you'll get a NaN or Inf.
Usage example(s):
1e300 asShortFloatChecked -> infiniteResultError
-1e300 asShortFloatChecked -> infiniteResultError
1e-300 asShortFloatChecked -> domainError
-1e-300 asShortFloatChecked -> domainError
|
-
asTrueFraction
-
Answer a fraction or integer that EXACTLY represents the receiver,
which is a double precision IEEE floating point number.
Notice: this may generate fractions which huge numerator/denominator.
By David N. Smith with significant performance
improvements by Luciano Esteban Notarfrancesco.
(Version of 11April97)
Subnormal, INF&NaN handling and further tuning by cg
Usage example(s):
0.3 asTrueFraction (5404319552844595/18014398509481984) - as you can see, Float is not able to represent this exactly
1.25 asTrueFraction (5/4) - but this one (it is a sum of powers of two)
0.25 asTrueFraction (1/4)
-0.25 asTrueFraction (-1/4)
3e37 asTrueFraction 30000000000000002158062836758597337088
2e37 asTrueFraction 19999999999999999077525316404242284544
1e37 asTrueFraction 9999999999999999538762658202121142272
1e30 asTrueFraction 1000000000000000019884624838656
(2.0 ** 100) asTrueFraction 1267650600228229401496703205376
(2.0 ** 50) asTrueFraction 1125899906842624
Float NaN asTrueFraction nan
Float infinity asTrueFraction inf
Float negativeInfinity asTrueFraction -inf
Float fmin asTrueFraction (1/44942328371557897693232629769725618340449424473557664318357520289433168951375240783177119330601884005280028469967848339414697442203604155623211857659868531094441973356216371319075554900311523529863270738021251442209537670585615720368478277635206809290837627671146574559986811484619929076208839082406056034304)
Float fminDenormalized asTrueFraction (1/202402253307310618352495346718917307049556649764142118356901358027430339567995346891960383701437124495187077864316811911389808737385793476867013399940738509921517424276566361364466907742093216341239767678472745068562007483424692698618103355649159556340810056512358769552333414615230502532186327508646006263307707741093494784)
Float fmaxDenormalized asTrueFraction (4503599627370495/202402253307310618352495346718917307049556649764142118356901358027430339567995346891960383701437124495187077864316811911389808737385793476867013399940738509921517424276566361364466907742093216341239767678472745068562007483424692698618103355649159556340810056512358769552333414615230502532186327508646006263307707741093494784)
|
-
generality
-
return the generality value - see ArithmeticValue>>retry:coercing:
comparing
-
< aNumber
-
return true, if the argument is greater
-
<= aNumber
-
return true, if the argument is greater or equal
-
= aNumber
-
return true, if the argument represents the same numeric value
as the receiver, false otherwise
-
> aNumber
-
return true, if the argument is less
-
>= aNumber
-
return true, if the argument is less or equal
-
hash
-
return a number for hashing; redefined, since floats compare
by numeric value (i.e. 3.0 = 3), therefore 3.0 hash must be the same
as 3 hash.
Usage example(s):
mhmh take some of my value-bits to hash on
|
Usage example(s):
3 hash
3.0 hash
3.1 hash
3.14159 hash
31.4159 hash
3.141591 hash
1.234567890123456 hash
1.234567890123457 hash
Set withAll:#(3 3.0 99 99.0 3.1415)
|
-
isAlmostEqualTo: aNumber nEpsilon: nE
-
return true, if the argument, aNumber represents almost the same numeric value
as the receiver, false otherwise.
nE is the number of minimal float distances, that the numbers may differ and
still be considered equal.
That is an integer, which counts the delta-steps in the mantissa.
Notice, that the absolute value of the epsilon depends on the receiver
(i.e. with a receiver of 10^17, a mantissa-step is much larger than with 10^-17 as receiver)
For background information why floats need this
read: http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
Usage example(s):
67329.234 isAlmostEqualTo:67329.23400000001 nEpsilon:1
1.0 isAlmostEqualTo:1.0001 nEpsilon:1
1.0 isAlmostEqualTo:-1.0 nEpsilon:1
1 isAlmostEqualTo:1.000000000000001 nEpsilon:1
1 isAlmostEqualTo:1.000000000000001 nEpsilon:10
1.0 isAlmostEqualTo:1 nEpsilon:1
1.0 isAlmostEqualTo:1 asFraction nEpsilon:1
0.0 isAlmostEqualTo:0 nEpsilon:1
0.0 isAlmostEqualTo:self epsilon nEpsilon:1
|
-
~= aNumber
-
return true, if the arguments value are not equal
copying
-
deepCopy
-
return a deep copy of myself
- because storing into floats is not recommended/allowed, its ok to return the receiver
-
deepCopyUsing: aDictionary postCopySelector: postCopySelector
-
return a deep copy of myself
- because storing into floats is not recommended/allowed, its ok to return the receiver
-
shallowCopy
-
return a shallow copy of the receiver
-
simpleDeepCopy
-
return a deep copy of the receiver
- because storing into floats is not recommended/allowed, its ok to return the receiver
double dispatching
-
lessFromLongFloat: aNumber
-
aLongFloat does not know how to compare to the receiver -
Return true if aLongFloat < self.
retry the operation by coercing to higher generality
mathematical functions
-
cbrt
-
return the cubic root of myself.
Usage example(s):
8 cbrt
-8 cbrt
8.0 cbrt
-8.0 cbrt
8.0 asShortFloat cbrt
-8.0 asShortFloat cbrt
8.0 raisedTo:(1/3)
-8.0 raisedTo:(1/3)
|
-
erf
-
return the error function of myself.
Usage example(s):
1 erf 0.842700792949715
1.0 erf 0.842700792949715
1.0 asShortFloat erf 0.842700792949715
4 erf
-4 erf
|
-
exp
-
return e raised to the power of the receiver
Usage example(s):
-
gamma
-
return the gamma function of myself.
Usage example(s):
4 gamma
4 gamma
10 gamma
-4 gamma
|
-
ldexp: exp
-
multiply the receiver by an integral power of 2.
I.e. return self * (2 ^ exp).
This is also the operation to reconstruct the original float from its
mantissa and exponent: (f mantissa ldexp:f exponent) = f
Usage example(s):
1.0 mantissa ldexp:1.0 exponent
-1.0 mantissa ldexp:-1.0 exponent
1.0 ldexp:16 -> 65536.0
1.0 ldexp:100 -> 1.26765060022823E+30
1 * (2 raisedToInteger:100) -> 1267650600228229401496703205376
1.0 ldexp:200 -> 1.60693804425899E+60
|
-
ln
-
return the natural logarithm of myself.
Raises an exception, if the receiver is less or equal to zero.
-
log10
-
return the base-10 logarithm of the receiver.
Raises an exception, if the receiver is less or equal to zero.
-
raisedTo: aNumber
-
return self raised to the power of aNumber
-
reciprocalLogBase2
-
optimized for self = 10, for use in conversion for printing
-
sqrt
-
return the square root of myself.
Raises an exception, if the receiver is less than zero.
Usage example(s):
10 sqrt -> 3.16227766016838
-10 sqrt -> error
Number trapImaginary:[ -10 sqrt ] -> (0+3.16227766016838i)
|
-
timesTenPower: anInteger
-
Return the receiver multiplied by 10 raised to the power of the argument.
i.e. self * (10 ** anInteger)
Usage example(s):
3.4 timesTenPower:0 -> 3.4
3.4 timesTenPower:1 -> 34.0
3.4 timesTenPower:10 -> 34000000000.0
3.4 timesTenPower:20 -> 3.4e+20
3.4 timesTenPower:21 -> 3.4e+21
3.4 timesTenPower:-1 -> 0.34
3.4 timesTenPower:-2 -> 0.034
3.4 timesTenPower:-10 -> 3.4e-10
3.4 timesTenPower:-20 -> 3.4e-20
3.4 timesTenPower:-21 -> 3.4e-21
|
printing & storing
-
printOn: aStream
-
append a printed representation of the receiver to
the argument, aStream.
I use #printString instead of #printOn: as basic print mechanism.
-
printString
-
return a printed representation of the receiver;
if not specified otherwise (by setting DefaultPrintFormat),
15 valid digits are printed.
LimitedPrecisonReal and its subclasses use #printString instead of
#printOn: as basic print mechanism.
Usage example(s):
Float pi printString. => '3.14159265358979'
1.0 printString => '1.0'
1.234 printString => '1.234'
1e10 printString => '10000000000.0'
1e-60 printString => '1e-60'
1.2e3 printString => '1200.0'
1.2e30 printString => '1.2e+30'
(1.0 uncheckedDivide:0) printString
(0.0 uncheckedDivide:0) printString
DecimalPointCharacter := $,.
1.234 printString.
1.0 printString.
1e10 printString.
1.2e3 printString.
1.2e30 printString.
(1.0 uncheckedDivide:0) printString.
(0.0 uncheckedDivide:0) printString.
DecimalPointCharacter := $.
|
-
printStringWithFormat: format
-
return a printed representation of the receiver;
fmt must be of the form: .nn, where nn is the number of digits.
To print 6 valid digits, use printStringWithFormat:'.6'
For Floats, the default used in printString, is 15 (because its a double);
for ShortFloats, it is 6 (because it is a float)
Usage example(s):
1.0 printString
1.0 printStringWithFormat:'.6'
1.1234567890123 printStringWithFormat:'.6'
1.234 printString
1e10 printString
1.2e3 printString
1.2e30 printString
(1.0 uncheckedDivide:0) printString
(0.0 uncheckedDivide:0) printString
self pi printString.
DecimalPointCharacter := $,.
1.234 printString.
1.0 printString.
1e10 printString.
1.2e3 printString.
1.2e30 printString.
(1.0 uncheckedDivide:0) printString.
(0.0 uncheckedDivide:0) printString.
DecimalPointCharacter := $.
|
-
printfPrintString: formatString
-
non-standard: return a printed representation of the receiver
as specified by formatString, which is defined by printf.
If you use this, be aware, that specifying doubles differs on
systems; on SYSV machines you have to give something like %lf,
while on BSD systems the format string has to be %F.
Also, the resulting string may not be longer than 1000 bytes -
since that's the (static) size of the buffer.
This method is NONSTANDARD and may be removed without notice.
WARNING: this goes directly to the C-printf function and may therefore me inherently unsafe.
Please use the printf: method, which is safe as it is completely implemented in Smalltalk.
Usage example(s):
Float pi printfPrintString:'%%.15lg -> %.15lg' '%.15lg -> 3.14159265358979'
'%%.15lg -> %.15lg' printf:{Float pi} '%.15lg -> 3.14159265358979'
Float pi printfPrintString:'%%lg -> %lg' '%lg -> 3.14159'
'%%lg -> %lg' printf:{Float pi} '%lg -> 3.14159'
Float pi printfPrintString:'%%lf -> %lf' '%lf -> 3.141593'
'%%lf -> %lf' printf:{Float pi} '%lf -> 3.141593'
Float pi printfPrintString:'%%7.15lg -> %7.15lg' '%7.15lg -> 3.14159265358979'
'%%7.15lg -> %7.15lg' printf:{Float pi} '%7.15lg -> 3.14159265358979'
Float pi printfPrintString:'%%7.5lf -> %7.5lf' '%7.5lf -> 3.14159'
'%%7.5lf -> %7.5lf' printf:{Float pi} '%7.5lf -> 3.14159'
Float pi printfPrintString:'%%G -> %G' '%G -> 3.14159'
'%%G -> %G' printf:{Float pi} '%G -> 3.14159'
Float pi printfPrintString:'%%F -> %F' '%F -> 3.141593'
'%%F -> %F' printf:{Float pi} '%F -> 3.141593'
Float pi printfPrintString:'%%7.5G -> %7.5G' '%7.5G -> 3.1416'
'%%7.5G -> %7.5G' printf:{Float pi} '%7.5G -> 3.1416'
Float pi printfPrintString:'%%7.5F -> %7.5F' '%7.5F -> 3.14159'
'%%7.5F -> %7.5F' printf:{Float pi} '%7.5F -> 3.14159'
Float pi printfPrintString:'%%7.5f -> %7.5f'
Float pi printfPrintString:'%%100.98f -> %100.98f'
Float pi printfPrintString:'%%100.6f -> %100.6f'
Float pi printfPrintString:'%%300.6f -> %300.6f'
Float pi printfPrintString:'%%-100.98f -> %-100.98f'
Float pi printfPrintString:'%%300.298f -> %300.298f'
Float pi printfPrintString:'%%-300.298f -> %-300.298f'
Float pi printfPrintString:'%%100.6f -> <%100.6f>'
Float pi printfPrintString:'%%300.6f -> <%300.6f>'
Float pi printfPrintString:'%%100.6f -> <%100.6>'
Float pi printfPrintString:'%%300.6f -> <%300.6>'
|
-
storeOn: aStream
-
append a printed representation of the receiver to
the argument, aStream.
I use #storeString instead of #storeOn: as basic store mechanism.
-
storeString
-
return a printed representation of the receiver;
all valid digits are printed.
LimitedPrecisonReal and its subclasses use #storeString instead of
#storeOn: as basic print mechanism.
Usage example(s):
Float pi storeString '3.1415926535897931'
Float pi printString '3.14159265358979'
1.0 storeString
0.1 storeString
((Array new:10 withAll:0.1) inject:0 into:[:v :sumSoFar| sumSoFar + v]) storeString
1.234 storeString
1e10 storeString
1.2e3 storeString
1.2e30 storeString
Float pi storeString
(1.0 uncheckedDivide:0) storeString
(0.0 uncheckedDivide:0) storeString
notice that the storeString is NOT affected by DecimalPointCharacterForPrinting:
DecimalPointCharacterForPrinting := $,.
1.234 storeString.
1.234 printString.
1.0 storeString.
1e10 storeString.
1.2e3 storeString.
1.2e30 storeString.
(1.0 uncheckedDivide:0) storeString.
(0.0 uncheckedDivide:0) storeString.
DecimalPointCharacterForPrinting := $.
|
private
-
absDecimalPrintOn: aStream digits: digits
( an extension from the stx:libbasic2 package )
-
Place a string representation of the receiver's abs value
on <aStream> using <digits> significant digits, using decimal notation.
This is a helper for printf.
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
absPrintOn: aStream digits: digits
( an extension from the stx:libbasic2 package )
-
Place a string representation of the receiver's abs value on <aStream> using
<digits> significant digits.
This is a helper for printf.
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
absScientificPrintOn: aStream digits: digits
( an extension from the stx:libbasic2 package )
-
Place a string representation of the receiver's abs value on <aStream> using <digits> significant
digits, using scientific notation.
This is a helper for printf.
** This is an obsolete interface - do not use it (it may vanish in future versions) **
private-accessing
-
basicAt: index
-
return an internal byte of the float.
The value returned here depends on byte order, float representation etc.
Therefore, this method should be used strictly private.
Notice:
the need to redefine this method here is due to the
inability of many machines to store floats in non-double aligned memory.
Therefore, on some machines, the first 4 bytes of a float are left unused,
and the actual float is stored at index 5 .. 12.
To hide this at one place, this method knows about that, and returns
values as if this filler wasnt present.
-
basicAt: index put: value
-
set an internal byte of the float.
The value to be stored here depends on byte order, float representation etc.
Therefore, this method should be used strictly private.
Notice:
the need to redefine this method here is due to the
inability of many machines to store floats in non-double aligned memory.
Therefore, on some machines, the first 4 bytes of a float are left unused,
and the actual float is stored at index 5 .. 12.
To hide this at one place, this method knows about that, and returns
values as if this filler wasnt present.
-
basicSize
-
return the size in bytes of the float.
Notice:
the need to redefine this method here is due to the
inability of many machines to store floats in non-double aligned memory.
Therefore, on some machines, the first 4 bytes of a float are left unused,
and the actual float is stored at index 5 .. 12.
To hide this at one place, this method knows about that, and returns
values as if this filler wasn't present.
-
byteAt: index
-
(comment from inherited method)
return the byte at index.
This is only allowed for non-pointer indexed objects
(i.e. byteArrays, wordArrays, floatArrays etc.).
The receiver's indexed instvars are treated as an uninterpreted
collection of bytes.
Only useful with binary storage.
-
byteAt: index put: newByte
-
(comment from inherited method)
set the byte at index.
This is only allowed for non-pointer indexed objects
(i.e. byteArrays, wordArrays, floatArrays etc.).
The receiver's indexed instvars are treated as an uninterpreted
collection of bytes.
Only useful with binary storage.
-
signBit
-
return the sign bit (1 for negative).
notice that floats can represent a negative zero,
and signBit handles that,
whereas sign would return 0 for both positive and negative zeros
Usage example(s):
1.0 signBit -> 0
-1.0 signBit -> 1
Float zero signBit -> 0
Float negativeZero signBit -> 1
|
queries
-
exponentBits
-
return the bits of my exponent.
These might be biased.
Usage example(s):
1.0 exponentBits 1023
-1.0 exponentBits 1023
10.0 exponentBits 1026
0.125 exponentBits 1020
0.1 exponentBits 1019
0.0 exponentBits 0
Float fmin exponentBits 1
Float fminDenormalized exponentBits
|
-
hasIEEEFormat
-
(comment from inherited method)
HalfFloat isIEEEFormat true
ShortFloat isIEEEFormat true
Float isIEEEFormat true
LongFloat isIEEEFormat true
QuadFloat isIEEEFormat true
OctaFloat isIEEEFormat true
QDouble isIEEEFormat false
LargeFloat isIEEEFormat false
-
mantissaBits
-
return the bits of my mantissa (excl. any hidden bit).
I.e. this returns the normalized mantissaBits as an integer
Usage example(s):
1.0 mantissaBits 0
10.0 mantissaBits 1125899906842624
0.125 mantissaBits 0
0.1 mantissaBits 2702159776422298
|
-
mantissaWithHiddenBits
-
return the bits of my mantissa (incl. any hidden bit).
I.e. this returns the denormalized mantissaBits.
Usage example(s):
7FF
0.0 mantissaWithHiddenBits 0
1.0 mantissaWithHiddenBits 4503599627370496
10.0 mantissaWithHiddenBits 5629499534213120
0.125 mantissaWithHiddenBits 4503599627370496
0.1 mantissaWithHiddenBits 7205759403792794
0.5 mantissaWithHiddenBits 4503599627370496
Float fmin mantissaWithHiddenBits 4503599627370496
Float fminDenormalized mantissaWithHiddenBits
|
-
nextFloat: countULPs
-
answer the next float count places (ulps) after (or before if count is negative) myself.
One ulp is the distance to the next/previous representable float,
and this returns the float which is countUlps away from me.
Notice that ulps depend on the receiver: an ulp away from 1e100 has a different
value than 1 ulp away from 1e-100.
Thus, ulps are perfect for 'almost equal' comparisons.
Usage example(s):
(1.0 nextFloat:1) storeString
(1.0 nextFloat:-1) storeString
(1.0 nextFloat:2) storeString
(1.0 nextFloat:100) storeString
(67329.234 nextFloat:1) storeString
(67329.234 asShortFloat nextFloat:1) storeString
(0.0 nextFloat:1) storeString '4.9406564584124654E-324'
(0.0 nextFloat:2) storeString '9.8813129168249309E-324'
(0.0 nextFloat:-1) storeString '-4.9406564584124654E-324'
(0.0 nextFloat:-2) storeString '-9.8813129168249309E-324'
(1e100 nextFloat:1) storeString
(1e38 asShortFloat nextFloat:1) storeString
(1e38 asShortFloat nextFloat:1) asFloat storeString
(Float fmax nextFloat) storeString
(Float fmax nextFloat:100) storeString
(Float fmax previousFloat) storeString
(Float fmin nextFloat) storeString
(Float fmin previousFloat) storeString -- a pseudo denormal
(Float fmin) storeString
Float NaN nextFloat
Float infinity nextFloat
Float NaN nextFloat:100000
Float infinity nextFloat:100000
Float negativeInfinity nextFloat:100000
Float negativeInfinity nextFloat
Float negativeInfinity previousFloat
Float fmin previousFloat
Float fmin previousFloat nextFloat
(0.0 nextFloat:50) nextFloat:-50
(10.0 nextFloat) - 10.0 -> 1.77635683940025e-15
(-10.0 nextFloat) - -10.0 -> 1.77635683940025e-15
(10.0 previousFloat) - 10.0 -> -1.77635683940025e-15
(-10.0 previousFloat) - -10.0 -> -1.77635683940025e-15
|
-
sign
-
return the sign of the receiver (-1, 0 or 1).
Notice: this returns 0 for a negative zero.
Reimplemented here for speed
Usage example(s):
-5.0 sign -1
5.0 sign 1
0.0 sign 0
-0.0 sign 0
|
-
ulpFrom: anotherFLoat
-
the distance in ULPs between two floats (anotherFloat - self)
Usage example(s):
(1.0 nextFloat:1) ulpFrom:1.0
1.0 ulpFrom:(1.0 nextFloat:1)
|
special access
-
exponent
-
extract a normalized float's (unbiased) exponent.
The returned value depends on the float-representation of
the underlying machine and is therefore highly unportable.
This is not for general use.
This assumes that the mantissa is normalized to
0.5 .. 1.0 and the float's value is: mantissa * 2^exp
Usage example(s):
1.0 exponent
2.0 exponent
3.0 exponent
3.0 mantissa
3.0 mantissa * (2 raisedTo:3.0 exponent)
4.0 exponent
0.5 exponent
0.4 exponent
0.25 exponent
0.2 exponent
0.00000011111 exponent
0.0 exponent
1e1000 exponent
Float fmin exponent -1021
Float fmaxDenormalized exponent -1022
Float fminDenormalized exponent -1073
Float nan exponent => error
|
-
mantissa
-
extract a normalized float's mantissa (as Float).
That is a float of the same type as the receiver,
such that:
(f mantissa) * (2 ^ f exponent) = f
The returned value depends on the float-representation of
the underlying machine and is therefore highly unportable.
This is not for general use.
This assumes that the mantissa is normalized to 0.5 .. 1.0
Usage example(s):
1.0 exponent -> 1
1.0 mantissa -> 0.5
-1.0 exponent -> 1
-1.0 mantissa -> -0.5
0.25 exponent -> -1
0.25 mantissa -> 0.5
9.0 exponent -> 4
9.0 mantissa -> 0.5625 (0.5625 * (2**4) -> 9.0)
-9.0 exponent -> 4
-9.0 mantissa -> -0.5625
9.0 asLongFloat exponent -> 4
9.0 asLongFloat mantissa -> 0.5625 (0.5625 * (2**4) -> 9.0)
9.0 asShortFloat exponent -> 4
9.0 asShortFloat mantissa -> 0.5625 (0.5625 * (2**4) -> 9.0)
9.0 asQDouble exponent -> 4
9.0 asQDouble mantissa -> 0.5625
9.0 asLargeFloat exponent -> 4
9.0 asLargeFloat mantissa -> 0.562500000000
9 exponent -> 4
9 mantissa -> (9/16) ((9/16)*(2**4) -> 9)
0.00000011111 exponent
0.00000011111 mantissa
1e1000 mantissa
self assert:(1.0 mantissa * (2 raisedTo:1.0 exponent)) = 1.0.
self assert:(100.0 mantissa * (2 raisedTo:100.0 exponent)) = 100.0.
self assert:(10e15 mantissa * (2 raisedTo:10e15 exponent)) = 10e15.
self assert:(10e-15 mantissa * (2 raisedTo:10e-15 exponent)) = 10e-15.
Float NaN mantissa -> error
Float NaN exponent -> error
Float fmin mantissa 0.5
Float fmin exponent -1021
(Float fmin mantissa * (2 raisedTo:Float fmin exponent)) = Float fmin
(Float fmin mantissa * (2.0 raisedTo:Float fmin exponent)) = Float fmin
Float fminDenormalized mantissa 0.5
Float fminDenormalized exponent -1073
(Float fminDenormalized mantissa * (2.0 raisedTo:Float fminDenormalized exponent)) = Float fminDenormalized
(Float fminDenormalized mantissa * (2 raisedTo:Float fminDenormalized exponent)) = Float fminDenormalized
|
testing
-
isFinite
-
return true, if the receiver is a finite float (not NaN and not +/-INF)
Usage example(s):
1.0 isFinite
1 isFinite
Float NaN isFinite
Float infinity isFinite
Float negativeInfinity isFinite
(0.0 uncheckedDivide: 0.0) isFinite
(1.0 uncheckedDivide: 0.0) isFinite
|
-
isFloat64
-
Answer whether the receiver is a 64bit double precision float.
Always true here.
-
isInfinite
-
return true, if the receiver is an infinite float (+Inf or -Inf).
These are not created by ST/X float operations (they raise an exception);
however, inline C-code could produce them.
Usage example(s):
1.0 isInfinite
Float NaN isInfinite
Float infinity isInfinite
Float negativeInfinity isInfinite
(0.0 uncheckedDivide: 0.0) isInfinite
(1.0 uncheckedDivide: 0.0) isInfinite
(-1.0 uncheckedDivide: 0.0) isInfinite
|
-
isLiteral
-
return true, if the receiver can be used as a literal constant in ST syntax
(i.e. can be used in constant arrays)
-
isNaN
-
return true, if the receiver is an invalid float (NaN - not a number).
These are usually not created by ST/X float operations (they raise an exception);
however, inline C-code or proceeded exceptions or reading from a stream
could produce them.
Usage example(s):
self NaN isNaN
1.0 isNaN
(0.0 uncheckedDivide: 0.0) isNaN
(1.0 uncheckedDivide: 0.0) isNaN
(-1.0 uncheckedDivide: 0.0) isNaN
|
-
isNegativeZero
-
many systems have two float.Pnt zeros
Usage example(s):
0.0 isNegativeZero
-0.0 isNegativeZero
-1.0 isNegativeZero
1.0 isNegativeZero
|
-
negative
-
return true if the receiver is less than zero.
-0.0 is positive for now.
Usage example(s):
0.0 negative
-0.0 negative
1.0 negative
-1.0 negative
(1.0 uncheckedDivide: 0.0) negative
(-1.0 uncheckedDivide: 0.0) negative
Float infinity negative
Float negativeInfinity negative
Float NaN negative
Float infinity positive
Float negativeInfinity positive
Float NaN positive
|
-
numberOfBits
-
return the size (in bits) of the real;
typically, 64 is returned here,
but who knows ...
Usage example(s):
1.2 numberOfBits
1.2 asShortFloat numberOfBits
|
-
positive
-
return true if the receiver is greater or equal to zero (not negative).
0.0 and -0.0 are positive for now.
Usage example(s):
0.0 positive
-0.0 positive
1.0 positive
-1.0 positive
(1.0 uncheckedDivide: 0.0) positive
(-1.0 uncheckedDivide: 0.0) positive
Float infinity positive
Float negativeInfinity positive
Float NaN positive
Float infinity negative
Float negativeInfinity negative
Float NaN negative
|
-
strictlyPositive
-
return true if the receiver is greater than zero
tracing
-
traceInto: aRequestor level: level from: referrer
-
double dispatch into tracer, passing my type implicitely in the selector
trigonometric
-
arcCos
-
return the arccosine of the receiver (as radians).
Raises an exception, if the receiver is not in -1..1
Usage example(s):
-
arcSin
-
return the arcsine of myself (I am interpreted as radians).
Raises an exception, if the receiver is not in -1..1
Usage example(s):
-
arcTan
-
return the arctangent of the receiver (as radians)
-
arcTan2: x
-
return the atan2(self(=y),x).
Notice that the receiver is y (as in the called C-library function)
Usage example(s):
0 arcTan2:0 -> error
1 arcTan2:0 -> 1.5707963267949 (pi/2)
-1 arcTan2:0 -> -1.5707963267949 (-pi/2)
0 arcTan2:-1 -> 3.14159265358979 (pi)
0 arcTan2:1 -> 0.0
1 arcTan2:1 -> 0.785398163397448 (pi/4)
-1 arcTan2:-1 -> -2.35619449019234 -(3/4 pi)
1 arcTan2:-1 -> 2.35619449019234 (3/4 pi)
-1 arcTan2:1 -> -0.785398163397448 -(pi/4)
2 arcTan:1 -> 1.10714871779409
1 arcTan:2 -> 0.463647609000806
|
-
arcTan: denominator
-
Evaluate the four quadrant arc tangent of the argument denominator (x) and the receiver (y).
(i.e. same as arcTan2: (which works only with float args)
Usage example(s):
0.0 arcTan2:0 -> error
0.0 arcTan:0 -> 0.0
1 arcTan2:0 -> 1.5707963267949 (pi/2)
1.0 arcTan:0 -> 1.5707963267949 (pi/2)
-1.0 arcTan2:0 -> -1.5707963267949 (-pi/2)
-1.0 arcTan:0 -> -1.5707963267949 (-pi/2)
0.0 arcTan2:-1 -> 3.14159265358979 (pi)
0.0 arcTan:-1 -> 3.14159265358979 (pi)
0.0 arcTan2:1 -> 0.0
0.0 arcTan:1 -> 0.0
1.0 arcTan2:1 -> 0.785398163397448 (pi/4)
1.0 arcTan:1 -> 0.785398163397448 (pi/4)
-1.0 arcTan2:-1 -> -2.35619449019234 -(3/4 pi)
-1.0 arcTan:-1 -> -2.35619449019234 -(3/4 pi)
1.0 arcTan2:-1 -> 2.35619449019234 (3/4 pi)
1.0 arcTan:-1 -> 2.35619449019234 (3/4 pi)
-1.0 arcTan2:1 -> -0.785398163397448 -(pi/4)
-1.0 arcTan:1 -> -0.785398163397448 -(pi/4)
2.0 arcTan2:1 -> 1.10714871779409
2.0 arcTan:1 -> 1.10714871779409
1.0 arcTan2:2 -> 0.463647609000806
1.0 arcTan:2 -> 0.463647609000806
|
-
cos
-
return the cosine of the receiver (interpreted as radians)
Usage example(s):
Float NaN cos -> error
Float infinity cos -> error
Number trapDomainError:[Float NaN cos] -> nan
Number trapDomainError:[Float infinity cos] -> nan
|
-
fastInverseSqrt
-
compute an approximatin of 1/sqrt(x).
The approx is returned as shortFloat and has an error of roughly 1%.
It does not make too much of a difference
on today's fast (FPU) machines, though.
Based on John Carmack
Usage example(s):
|n|
n := 4.0.
n fastInverseSqrt
|
Usage example(s):
Time microsecondsToRun:[
10000000 timesRepeat:[ 4.0 fastInverseSqrt]
]. -> 367797 378279 390732
Time microsecondsToRun:[
10000000 timesRepeat:[ 4.0 inverseSqrt]
]. -> 432824 431464 431809
|
-
hypot: b
-
answer sqrt(self*self + b*b)
The value computed by this function is
- the length of the hypotenuse of a right-angled triangle with sides of length x and y,
- or the distance of the point (x,y) from the origin (0,0),
- or the magnitude of a complex number x+iy.
Usage example(s):
10.0 hypot:10.0 -> 14.142135623731
10.0 hypot:20.0 -> 22.3606797749979
10.0 hypot:20 -> 22.3606797749979
10.0 asQDouble hypot:20 -> 22.36067977
10.0 hypot:20 asQDouble -> 22.36067977
|
-
inverseSqrt
-
compute 1/sqrt(x)
Usage example(s):
|n|
n := 4.0.
n inverseSqrt
|
-
sin
-
return the sine of the receiver (interpreted as radians)
-
tan
-
return the tangens of the receiver (interpreted as radians)
trigonometric - hyperbolic
-
arCosh
-
return the hyperbolic area cosine of the receiver.
Usage example(s):
0.0 arCosh -> 0.0
1.0 arCosh -> 0.0
-10.0 arCosh -> error
Number trapDomainError:[0.0 arCosh] -> nan
Number trapImaginary:[0.0 arCosh] -> (0.0+1.5707963267949i)
Number trapDomainError:[-10.0 arCosh] -> nan
Float infinity arCos -> infh
|
-
arSinh
-
return the hyperbolic area sine of the receiver.
Usage example(s):
1.0 arSinh
0.5 arSinh
-10.0 arSinh
|
-
arTanh
-
return the hyperbolic area tangent of the receiver.
Usage example(s):
1.0 arTanh
Number trapDomainError:[1.0 arTanh] -> NaN
|
-
cosh
-
return the hyperbolic cosine of the receiver (interpreted as radians)
Usage example(s):
Float NaN cosh => error
Float infinity cosh => inf
Float infinity negated cosh -> inf
Number trapDomainError:[Float NaN cosh] -> nan
1000 cosh => inf
711 cosh => inf
2 cosh => 3.76219569108363 (Float64)
2.0 cosh => 3.76219569108363 (Float64)
2.0q cosh => 3.76219569108363146 (Float80)
2.0Q cosh => 3.76219569108363145956221347777 (Float128)
2.0QD cosh => 3.76219569108363145965370777368264043616363778710365295410156 (QDouble)
2.0QL cosh => 3.7621956910836314595622134777737461082939735582307116027776 (LargeFloat)
2.0 asFloat256 cosh => 3.76219569108363145956221347777374610829397355823071160277764334758832 (OctaFloat)
|
-
sinh
-
return the hyperbolic sine of the receiver
Usage example(s):
1000 sinh
709 sinh 4.10920373077749e+307
1000 asLongFloat sinh 9.850355570085235467E+433
1000 asLongFloat cosh 9.850355570085234969E+433
1000 asLargeFloat cosh
|
-
tanh
-
return the hyperbolic tangens of the receiver
truncation & rounding
-
ceiling
-
return the smallest integer which is greater or equal to the receiver.
-
ceilingAsFloat
-
return the smallest integer-valued float greater or equal to the receiver.
This is much like #ceiling, but avoids a (possibly expensive) conversion
of the result to an integer.
It may be useful, if the result is to be further used in another float-operation.
Usage example(s):
0.5 ceilingAsFloat
-0.5 ceilingAsFloat
-1.5 ceilingAsFloat
|
-
floor
-
return the integer nearest the receiver towards negative infinity.
Usage example(s):
0.5 floor
0.5 floorAsFloat
-0.5 floor
-0.5 floorAsFloat
|
-
floorAsFloat
-
return the integer nearest the receiver towards negative infinity as a float.
This is much like #floor, but avoids a (possibly expensive) conversion
of the result to an integer.
It may be useful, if the result is to be further used in another float-operation.
Usage example(s):
0.5 floor
0.5 floorAsFloat
-0.5 floor
-0.5 floorAsFloat
|
-
fractionPart
-
extract the after-decimal fraction part.
such that:
(self truncated + self fractionPart) = self
Usage example(s):
1.6 fractionPart + 1.6 truncated
-1.6 fractionPart + -1.6 truncated
1.0 fractionPart
2.0 fractionPart
3.0 fractionPart
4.0 fractionPart
0.5 fractionPart
0.25 fractionPart
3.14159 fractionPart
12345673.14159 fractionPart
123456731231231231.14159 fractionPart
3.14159 fractionPart + 3.14159 truncated
12345673.14159 fractionPart + 12345673.14159 truncated
123456731231231231.14159 fractionPart + 123456731231231231.14159 truncated
|
-
integerAndFractionParts
-
return the integer and the fraction part of the receiver as a pair
of floats (i.e. the result of the modf function).
Adding the parts gives the original value
Usage example(s):
0.5 integerAndFractionParts
-0.5 integerAndFractionParts
12345.6789 integerAndFractionParts
-12345.6789 integerAndFractionParts
|
-
rounded
-
return the receiver rounded to the nearest integer
Usage example(s):
0.4 rounded
0.5 rounded
0.6 rounded
-0.4 rounded
-0.5 rounded
-0.6 rounded
|
-
roundedAsFloat
-
return the receiver rounded to the nearest integer as a float.
This is much like #rounded, but avoids a (possibly expensive) conversion
of the result to an integer.
It may be useful, if the result is to be further used in another
float-operation.
Usage example(s):
0.5 rounded
-0.5 rounded
0.5 roundedAsFloat
-0.5 roundedAsFloat
|
-
truncated
-
return the receiver truncated towards zero as an integer
Usage example(s):
0.5 truncated
-0.5 truncated
0.5 truncatedAsFloat
-0.5 truncatedAsFloat
|
-
truncatedAsFloat
-
return the receiver truncated towards zero as a float.
This is much like #truncated, but avoids a (possibly expensive) conversion
of the result to an integer.
It may be useful, if the result is to be further used in another
float-operation.
Usage example(s):
0.5 truncated
-0.5 truncated
0.5 truncatedAsFloat
-0.5 truncatedAsFloat
|
|