type

Float

src @Serializable { simple=true }
const class Float : Num

Float is used to represent a 64-bit floating point number.

constructors

fromStr

Parse a Str into a Float.

fields

defVal

Default value is 0f.

e

Float value for e which is the base of natural logarithms.

nan

Float value for Not-A-Number.

negInf

Float value for negative infinity.

pi

Float value for pi which is the ratio of the circumference of a circle to its diameter.

posInf

Float value for positive infinity.

methods

abs

Return the absolute value of this float.

acos

Return the arc cosine.

approx

Return if this Float is approximately equal to the given Float by the specified tolerance.

asin

Return the arc sine.

atan

Return the arc tangent.

atan2

Converts rectangular coordinates (x, y) to polar (r, theta).

bits

Return 64-bit representation according IEEE 754 floating-point double format bit layout.

bits32

Return 32-bit representation according IEEE 754 floating-point single format bit layout.

ceil

Returns the smallest whole number greater than or equal to this number.

clamp

Clamp this float between the min and max.

compare

Compare based on floating point value.

cos

Return the cosine of this angle in radians.

cosh

Return the hyperbolic cosine.

decrement

Decrement by one.

div

Divide this by b.

divDecimal

Divide this by b.

divInt

Divide this by b.

equals

Return true if same float value.

exp

Return e raised to this power.

floor

Returns the largest whole number less than or equal to this number.

hash

Return bits().

increment

Increment by one.

isNaN

Return if this is Float.nan.

isNegZero

Return if this is negative zero value.

log

Return natural logarithm of this number.

log10

Return base 10 logarithm of this number.

makeBits

Make a Float for the specified 64-bit representation according IEEE 754 floating-point double format bit layout.

makeBits32

Make a Float for the specified 32-bit representation according IEEE 754 floating-point single format bit layout.

max

Return the larger of this and the specified Float values.

min

Return the smaller of this and the specified Float values.

minus

Subtract b from this.

minusDecimal

Subtract b from this.

minusInt

Subtract b from this.

mod

Return remainder of this divided by b.

modDecimal

Return remainder of this divided by b.

modInt

Return remainder of this divided by b.

mult

Multiply this with b.

multDecimal

Multiply this with b.

multInt

Multiply this with b.

negate

Negative of this.

normNegZero

If this value is negative zero then return normalized zero, otherwise return this value.

plus

Add this with b.

plusDecimal

Add this with b.

plusInt

Add this with b.

pow

Return this value raised to the specified power.

random

Generate a random float between 0.0 inclusive and 1.0 exclusive.

round

Returns the nearest whole number to this number.

sin

Return sine of this angle in radians.

sinh

Return hyperbolic sine.

sqrt

Return square root of this value.

tan

Return tangent of this angle in radians.

tanh

Return hyperbolic tangent.

toCode

Get this Float as a Fantom code literal.

toDegrees

Convert this angle in radians to an angle in degrees.

toLocale

Format this floating point number for the current locale.

toRadians

Convert this angle in degrees to an angle in radians.

toStr

Get string representation according to the lexical representation defined by Section 3.2.5 of XML Schema Part 2.

Slot Details

abs

src Float abs()

Return the absolute value of this float. If this value is positive then return this, otherwise return the negation.

acos

src Float acos()

Return the arc cosine.

approx

src Bool approx(Float r, Float? tolerance := null)

Return if this Float is approximately equal to the given Float by the specified tolerance. If tolerance is null, then it is computed using the magnitude of the two Floats. It is useful for comparing Floats since often they lose a bit of precision during manipulation. This method is equivalent to:

if (tolerance == null) tolerance = min(abs(this/1e6), abs(r/1e6))
(this - r).abs < tolerance

asin

src Float asin()

Return the arc sine.

atan

src Float atan()

Return the arc tangent.

atan2

src static Float atan2(Float y, Float x)

Converts rectangular coordinates (x, y) to polar (r, theta).

bits

src Int bits()

Return 64-bit representation according IEEE 754 floating-point double format bit layout. This method is paired with Float.makeBits.

bits32

src Int bits32()

Return 32-bit representation according IEEE 754 floating-point single format bit layout. This method is paired with Float.makeBits32.

ceil

src Float ceil()

Returns the smallest whole number greater than or equal to this number.

clamp

src Float clamp(Float min, Float max)

Clamp this float between the min and max. If its less than min then return min, if its greater than max return max, otherwise return this float itself.

compare

src virtual override Int compare(Obj obj)

Compare based on floating point value.

NaN works as follows:

  • for the <=> operator NaN is always less than other values and equal to itself (so sort works as expected)
  • for all other comparison operators anything compared against NaN is false (normal Java semanatics)

Examples:

Float.nan <=> Float.nan  =>  0
2f <=> Float.nan         =>  1
Float.nan <=> 2f         =>  -1
2f < Float.nan           =>  false
Float.nan < 2f           =>  false
Float.nan <= Float.nan   =>  false

cos

src Float cos()

Return the cosine of this angle in radians.

cosh

src Float cosh()

Return the hyperbolic cosine.

decrement

src @Operator
Float decrement()

Decrement by one. Shortcut is --a or a--.

defVal

src const static Float defVal

Default value is 0f.

div

src @Operator
Float div(Float b)

Divide this by b. Shortcut is a/b.

divDecimal

src @Operator
Decimal divDecimal(Decimal b)

Divide this by b. Shortcut is a/b.

divInt

src @Operator
Float divInt(Int b)

Divide this by b. Shortcut is a/b.

e

src const static Float e

Float value for e which is the base of natural logarithms.

equals

src virtual override Bool equals(Obj? obj)

Return true if same float value. Like Java, NaN != NaN. Also see compare.

exp

src Float exp()

Return e raised to this power.

floor

src Float floor()

Returns the largest whole number less than or equal to this number.

fromStr

src static new fromStr(Str s, Bool checked := true)

Parse a Str into a Float. Representations for infinity and not-a-number are "-INF", "INF", "NaN". This string format matches the lexical representation of Section 3.2.5 of XML Schema Part 2. If invalid format and checked is false return null, otherwise throw ParseErr.

hash

src virtual override Int hash()

Return bits().

increment

src @Operator
Float increment()

Increment by one. Shortcut is ++a or a++.

isNaN

src Bool isNaN()

Return if this is Float.nan. Also see compare.

isNegZero

src Bool isNegZero()

Return if this is negative zero value.

log

src Float log()

Return natural logarithm of this number.

log10

src Float log10()

Return base 10 logarithm of this number.

makeBits

src static Float makeBits(Int bits)

Make a Float for the specified 64-bit representation according IEEE 754 floating-point double format bit layout. This method is paired with Float.bits.

makeBits32

src static Float makeBits32(Int bits)

Make a Float for the specified 32-bit representation according IEEE 754 floating-point single format bit layout. This method is paired with Float.bits32.

max

src Float max(Float that)

Return the larger of this and the specified Float values.

min

src Float min(Float that)

Return the smaller of this and the specified Float values.

minus

src @Operator
Float minus(Float b)

Subtract b from this. Shortcut is a-b.

minusDecimal

src @Operator
Decimal minusDecimal(Decimal b)

Subtract b from this. Shortcut is a-b.

minusInt

src @Operator
Float minusInt(Int b)

Subtract b from this. Shortcut is a-b.

mod

src @Operator
Float mod(Float b)

Return remainder of this divided by b. Shortcut is a%b.

modDecimal

src @Operator
Decimal modDecimal(Decimal b)

Return remainder of this divided by b. Shortcut is a%b.

modInt

src @Operator
Float modInt(Int b)

Return remainder of this divided by b. Shortcut is a%b.

mult

src @Operator
Float mult(Float b)

Multiply this with b. Shortcut is a*b.

multDecimal

src @Operator
Decimal multDecimal(Decimal b)

Multiply this with b. Shortcut is a*b.

multInt

src @Operator
Float multInt(Int b)

Multiply this with b. Shortcut is a*b.

nan

src const static Float nan

Float value for Not-A-Number.

negInf

src const static Float negInf

Float value for negative infinity.

negate

src @Operator
Float negate()

Negative of this. Shortcut is -a.

normNegZero

src Float normNegZero()

If this value is negative zero then return normalized zero, otherwise return this value.

pi

src const static Float pi

Float value for pi which is the ratio of the circumference of a circle to its diameter.

plus

src @Operator
Float plus(Float b)

Add this with b. Shortcut is a+b.

plusDecimal

src @Operator
Decimal plusDecimal(Decimal b)

Add this with b. Shortcut is a+b.

plusInt

src @Operator
Float plusInt(Int b)

Add this with b. Shortcut is a+b.

posInf

src const static Float posInf

Float value for positive infinity.

pow

src Float pow(Float pow)

Return this value raised to the specified power.

random

src static Float random()

Generate a random float between 0.0 inclusive and 1.0 exclusive. Also see Int.random, Range.random, List.random, and Random.

round

src Float round()

Returns the nearest whole number to this number.

sin

src Float sin()

Return sine of this angle in radians.

sinh

src Float sinh()

Return hyperbolic sine.

sqrt

src Float sqrt()

Return square root of this value.

tan

src Float tan()

Return tangent of this angle in radians.

tanh

src Float tanh()

Return hyperbolic tangent.

toCode

src Str toCode()

Get this Float as a Fantom code literal.

toDegrees

src Float toDegrees()

Convert this angle in radians to an angle in degrees.

toLocale

src Str toLocale(Str? pattern := null, Locale locale := Locale.cur())

Format this floating point number for the current locale. If pattern is null, then the locale's default pattern is used. Also see Num.localeDecimal, Num.localeGrouping, etc.

The pattern format:

#   optional digit
0   required digit
.   decimal point
,   grouping separator (only last one before decimal matters)

Examples:

12345.786f.toLocale("#,###.0")  =>  12,345.8
7.1234f.toLocale("#.000")       =>  7.123
0.1234f.toLocale("#.000")       =>  .123
0.1234f.toLocale("0.00")        =>  0.12
70.12f.toLocale("0.0000")       =>  70.1200

toRadians

src Float toRadians()

Convert this angle in degrees to an angle in radians.

toStr

src virtual override Str toStr()

Get string representation according to the lexical representation defined by Section 3.2.5 of XML Schema Part 2. Representations for infinity and not-a-number are "-INF", "INF", "NaN".